Class: SimpleAttribute::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_attribute/matcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(record, attribute) ⇒ Matcher

Returns a new instance of Matcher.



3
4
5
6
7
8
9
10
# 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

Instance Method Details

#file_method?Boolean

Returns:

  • (Boolean)


20
21
22
23
# 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_typeObject



25
26
27
28
29
30
31
# 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_typeObject



12
13
14
# File 'lib/simple_attribute/matcher.rb', line 12

def find_type
  @type ||= @record.class.attribute_types[@attribute.to_s]
end

#find_type_nameObject



16
17
18
# File 'lib/simple_attribute/matcher.rb', line 16

def find_type_name
  @type_name ||= @type.class.name.demodulize.downcase unless @type.nil?
end

#matchObject



33
34
35
# File 'lib/simple_attribute/matcher.rb', line 33

def match
  file_method? ? find_file_type : @type_name.to_sym
end