Class: SimpleAttribute::Matcher
- Inherits:
-
Object
- Object
- SimpleAttribute::Matcher
- Defined in:
- lib/simple_attribute/matcher.rb
Instance Method Summary collapse
-
#file_method? ⇒ Boolean
Check if file method.
-
#find_file_type ⇒ Object
Find file type.
-
#find_type ⇒ Object
Find attribute type.
-
#find_type_name ⇒ Object
Find type name.
-
#initialize(record, attribute) ⇒ Matcher
constructor
Initialize attribute matcher.
-
#match ⇒ Object
Get renderer match.
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
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_type ⇒ Object
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_type ⇒ Object
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_name ⇒ Object
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 |
#match ⇒ Object
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 |