module ActiveFile::Adapter
Data Source Storage Adapter
Constants
- BASE_FOLDER
- RAISE_FALSE
- RAISE_TRUE
Public Instance Methods
attach_my_name!()
click to toggle source
Main rule to link attaches (css) with their targets (layouts, contents). Special filename format used for this purpose;
# File lib/active_file/adapter.rb, line 34 def attach_my_name! # Is we an attached instance? unless self.target.nil? #@ext = CSS_EXT if self.type == SourceType::CSS self.name = target.type.to_s + TARGET_DIVIDER + target.name #+ @ext.to_s end end
data()
click to toggle source
# File lib/active_file/adapter.rb, line 26 def data if @self_data == nil @self_data = ::File.read(self.get_source_path) end @self_data || "" end
data=(data_arg)
click to toggle source
# File lib/active_file/adapter.rb, line 22 def data= data_arg @self_data = data_arg end
delete()
click to toggle source
Delete the source: if successful returns true, else return false
# File lib/active_file/adapter.rb, line 144 def delete delete_method(RAISE_FALSE) end
delete!()
click to toggle source
Delete the source: if successful returns true, else raise an error
# File lib/active_file/adapter.rb, line 148 def delete! raise StandardError, "Unable to delete file if object hasn't been saved yet" if new_record? delete_method(RAISE_TRUE) end
get_attach(attach_type=SourceType::CSS)
click to toggle source
Get attached object
# File lib/active_file/adapter.rb, line 175 def get_attach attach_type=SourceType::CSS Adapter.where(:type => attach_type, :name => get_attached_name).first end
get_attach_or_create(attach_type=SourceType::CSS)
click to toggle source
def get_attached_filename
attached_file_extension = SOURCE_TYPE_EXTENSIONS[attach_type.to_i] attached_file_extension = "." + attached_file_extension unless attached_file_extension.empty? type.to_s + TARGET_DIVIDER + name + attached_file_extension
end
# File lib/active_file/adapter.rb, line 186 def get_attach_or_create attach_type=SourceType::CSS attach = get_attach(attach_type) if attach.nil? attach = Source.new(:type => attach_type, :extension => SOURCE_TYPE_EXTENSIONS[attach_type]) attach.target = self attach.save! end attach end
get_attached_name()
click to toggle source
# File lib/active_file/adapter.rb, line 178 def get_attached_name type.to_s + TARGET_DIVIDER + name end
get_extension()
click to toggle source
# File lib/active_file/adapter.rb, line 65 def get_extension return ".scss" if type == SourceType::CSS return extension.blank? ? "" : "."+extension type_ext = SOURCE_TYPE_EXTENSIONS[type.to_i] || "" return "" if type_ext == "*" if type_ext == "*" unless new_record? # Custom extension, from filename Dir.glob(dir+"*").each do |f| name_with_extension = f.split('/').last name, ext = name_with_extension.split('.') if name == get_name type_ext = ext break end end else _name, _ext = name.split(".") unless _name.empty? && _ext.empty? type_ext = _ext end end end type_ext = "." + type_ext unless type_ext.empty? type_ext end
get_filename()
click to toggle source
Get source filename
# File lib/active_file/adapter.rb, line 51 def get_filename #get_name + get_extension get_name end
get_filepath()
click to toggle source
Alias for get_source_path
# File lib/active_file/adapter.rb, line 56 def get_filepath get_source_path end
get_id()
click to toggle source
Get unique id of the source
# File lib/active_file/adapter.rb, line 93 def get_id ID_PREFIX + type.to_s + ID_DIVIDER + get_name end
get_name()
click to toggle source
# File lib/active_file/adapter.rb, line 46 def get_name attach_my_name! return name end
get_source_folder()
click to toggle source
Get source folder. Create, if not exists.
# File lib/active_file/adapter.rb, line 43 def get_source_folder Adapter.get_source_folder(type) end
get_source_path()
click to toggle source
Get source path. For targeted objects, target name + ‘–’ + target type appends
# File lib/active_file/adapter.rb, line 60 def get_source_path raise ArgumentError, 'Name can not be blank!' if name.blank? && target.nil? raise ArgumentError, 'Target name can not be blank!' if target && target.name.blank? get_source_folder + get_filename end
get_target()
click to toggle source
Get target object
# File lib/active_file/adapter.rb, line 159 def get_target return nil unless name.include?(TARGET_DIVIDER) #target_type, target_name = name[0..-2].split(TARGET_DIVIDER) target_type = get_target_type target_name = get_target_name target_type_extension = SOURCE_TYPE_EXTENSIONS[target_type.to_i] unless target_type_extension.empty? target_type_extension = "." + target_type_extension end target = Adapter.get_source_folder(target_type) + target_name + target_type_extension return nil unless ::File.exists?(target) return Source.new({ :type => target_type.to_i, :name => target_name, :data => nil }) end
get_target_name()
click to toggle source
# File lib/active_file/adapter.rb, line 155 def get_target_name name.split(TARGET_DIVIDER)[1] end
get_target_type()
click to toggle source
# File lib/active_file/adapter.rb, line 152 def get_target_type name.split(TARGET_DIVIDER)[0] end
load!()
click to toggle source
touch file to read!
# File lib/active_file/adapter.rb, line 17 def load! self.data self end
new_record?()
click to toggle source
# File lib/active_file/adapter.rb, line 116 def new_record? !::File.exists?(get_source_path) end
rename(new_file_name)
click to toggle source
Rename the source file, return boolean operation result
# File lib/active_file/adapter.rb, line 97 def rename(new_file_name) raise "Source is new record, call the save! method before rename." if new_record? # rename attached file, if present () attach = get_attach # later.. old_file_path = get_source_path new_file_path = get_source_folder + new_file_name + get_extension b_result = !!::File.rename(old_file_path, new_file_path) raise "Unable to rename source" unless b_result self.name = new_file_name if attach # to rename attach, create new copy of attached object with new target name, and delete old attach_source = attach.clone attach_source.target = self b_result = attach_source.save! && attach.delete! raise 'Attached file rename failed' unless b_result end b_result end
save()
click to toggle source
Save the source, return boolean operation result
# File lib/active_file/adapter.rb, line 137 def save save_method(RAISE_FALSE) end
save!()
click to toggle source
# File lib/active_file/adapter.rb, line 140 def save! save_method(RAISE_TRUE) end
type()
click to toggle source
# File lib/active_file/adapter.rb, line 11 def type self.class.to_s.split("::")[1].downcase #raise "type to be done #{self.class}" end
Private Instance Methods
delete_method(raise_exception_on_error)
click to toggle source
# File lib/active_file/adapter.rb, line 128 def delete_method(raise_exception_on_error) delete_file_name = get_source_path ::File.delete(delete_file_name) return true rescue => e return raise_exception_on_error == RAISE_TRUE ? raise(e) : false end
save_method(raise_exception_on_error)
click to toggle source
# File lib/active_file/adapter.rb, line 120 def save_method(raise_exception_on_error) ::File.open(get_source_path, "w") do |file| file.write(data.force_encoding('utf-8')) end true rescue => e return raise_exception_on_error == RAISE_TRUE ? raise(e) : false end