class SimpleAttribute::Matcher

Public Class Methods

new(record, attribute) click to toggle source
# File lib/simple_attribute/matcher.rb, line 3
def initialize(record, attribute)
  @record    = record
  @attribute = attribute
  @value     = record.try(attribute)

  find_type
  find_type_name
end

Public Instance Methods

file_method?() click to toggle source
# File lib/simple_attribute/matcher.rb, line 20
def file_method?
  methods = [:mounted_as, :file?, :public_filename]
  methods.any? { |m| @value.respond_to?(m) }
end
find_file_type() click to toggle source
# File lib/simple_attribute/matcher.rb, line 25
def find_file_type
  media_type = MiniMime.lookup_by_filename(@value.to_s)
  media_type = media_type.content_type.split('/').first unless media_type.nil?
  available  = [:image, :video]

  available.find { |t| t.to_s == media_type } || :file
end
find_type() click to toggle source
# File lib/simple_attribute/matcher.rb, line 12
def find_type
  @type ||= @record.class.attribute_types[@attribute.to_s]
end
find_type_name() click to toggle source
# File lib/simple_attribute/matcher.rb, line 16
def find_type_name
  @type_name ||= @type.class.name.demodulize.downcase unless @type.nil?
end
match() click to toggle source
# File lib/simple_attribute/matcher.rb, line 33
def match
  file_method? ? find_file_type : @type_name.to_sym
end