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

Initialize attribute matcher



4
5
6
7
8
9
10
11
# File 'lib/simple_attribute/matcher.rb', line 4

def initialize(record, attribute)
  @record    = record
  @attribute = attribute
  @value     = record.try(attribute)

  find_type
  find_type_name
end

Instance Method Details

#file_method?Boolean

Check if file method

Returns:

  • (Boolean)


24
25
26
27
# File 'lib/simple_attribute/matcher.rb', line 24

def file_method?
  methods = [:mounted_as, :file?, :public_filename]
  methods.any? { |m| @value.respond_to?(m) }
end

#find_file_typeObject

Find file type



30
31
32
33
34
35
36
# File 'lib/simple_attribute/matcher.rb', line 30

def find_file_type
  media_type = MiniMime.lookup_by_filename("#{@value}")
  media_type = media_type.content_type.split('/').first unless media_type.nil?
  available  = [:image, :video]

  available.find { |t| "#{t}" == media_type } || :file
end

#find_typeObject

Find attribute type



14
15
16
# File 'lib/simple_attribute/matcher.rb', line 14

def find_type
  @type ||= @record.class.attribute_types["#{@attribute}"]
end

#find_type_nameObject

Find type name



19
20
21
# File 'lib/simple_attribute/matcher.rb', line 19

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

#matchObject

Get renderer match



39
40
41
# File 'lib/simple_attribute/matcher.rb', line 39

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