module ActiveFile::Adapter::ClassMethods
STATIC METHODS MODULE
Public Instance Methods
all()
click to toggle source
# File lib/active_file/adapter.rb, line 243 def all (Rails.env == 'test' ? TEST_SOURCE_FOLDERS : SOURCE_FOLDERS).map {|key_type, val| Adapter.all_by_type(key_type) }.reject { |ar| ar.empty? }.flatten end
all_by_type(source_type)
click to toggle source
Get names array of all sources with specified type
# File lib/active_file/adapter.rb, line 218 def all_by_type(source_type) files = Array.new dir = Adapter.get_source_folder(source_type) source_extension = SOURCE_TYPE_EXTENSIONS[source_type.to_i] Dir.glob(dir+"*").each do |f| name_with_extension = f.split('/').last extension = name_with_extension.split('.').size > 1 ? name_with_extension.split('.').last : "" name_without_extension = nil name_without_extension = name_with_extension if source_extension.blank? name_without_extension = name_with_extension else name_without_extension = source_extension == "*" ? name_with_extension.split('.').first : name_with_extension[0..-source_extension.length-2] end s = Source.new({ :type => source_type, :name => name_without_extension, :extension => extension, :data => nil }) target_object = s.get_target s.target = target_object unless target_object.nil? files.push(s) end return files end
base_folder(arg)
click to toggle source
# File lib/active_file/adapter.rb, line 198 def base_folder arg puts "BaseFolder is #{arg}" end
create(attributes={})
click to toggle source
Creates a new source instance and saves it to disk. Returns the newly created source. If a failure has occurred or source already exists - an exception will be raised.
# File lib/active_file/adapter.rb, line 203 def create(attributes={}) raise ArgumentError, "expected an attributes Hash, got #{attributes.inspect}" unless attributes.is_a?(Hash) source_instance = Source.new(attributes) raise 'Source file with such name already exists!' if ::File.exists?(source_instance.get_source_path) source_instance.save! return source_instance end
find_by_id(id)
click to toggle source
# File lib/active_file/adapter.rb, line 257 def find_by_id(id) id = id[ID_PREFIX.size .. -1] type, name = id.include?(TARGET_DIVIDER) ? (id).split(ID_DIVIDER) : id.split(ID_DIVIDER) Adapter.find_by_name_and_type(name, type.to_i).first end
get_source_folder(type)
click to toggle source
Get source folder for any source type. Create, if not exists.
# File lib/active_file/adapter.rb, line 211 def get_source_folder(type) #source_folder = Rails.env == 'test' ? TEST_SOURCE_FOLDERS[type.to_i || SourceType::UNDEFINED] : SOURCE_FOLDERS[type.to_i || SourceType::UNDEFINED] source_folder = "activefiles/" + type.to_s + "/" FileUtils.mkpath(source_folder) unless ::File.exists?(source_folder) return source_folder end
method_missing(m, *args, &block)
click to toggle source
Complex finders:
# File lib/active_file/adapter.rb, line 263 def method_missing(m, *args, &block) if m.to_s.index("find_by_") == 0 attributes = m["find_by_".size..-1].split("_and_") raise "Attributes count expected: #{attributes.size}, got: #{args.size}" unless attributes.size == args.size match_hash = {} attributes.each_with_index {|attr, index| match_hash[attr.to_sym] = args[index]} return Adapter.where(match_hash) else puts "There's no method called #{m} here -- please try again with args #{args}" end end
where(attributes)
click to toggle source
Find the source
# File lib/active_file/adapter.rb, line 247 def where(attributes) raise ArgumentError, "expected an attributes Hash, got #{attributes.inspect}" unless attributes.is_a?(Hash) Adapter.all.select do |source| match = true attributes.each{|key, val| match = false if source.send(key) != val } match end end