Class: FlexiView::Attribute
- Inherits:
-
Object
- Object
- FlexiView::Attribute
- Defined in:
- lib/flexi_view/attribute.rb
Instance Attribute Summary collapse
-
#data_block ⇒ Object
Returns the value of attribute data_block.
-
#formatter ⇒ Object
Returns the value of attribute formatter.
-
#name ⇒ Object
Returns the value of attribute name.
-
#options ⇒ Object
Returns the value of attribute options.
-
#serialized_name ⇒ Object
Returns the value of attribute serialized_name.
Instance Method Summary collapse
- #condition?(serializer) ⇒ Boolean
-
#initialize(name, options, &data_block) ⇒ Attribute
constructor
A new instance of Attribute.
- #to_data(serializer) ⇒ Object
Constructor Details
#initialize(name, options, &data_block) ⇒ Attribute
Returns a new instance of Attribute.
7 8 9 10 11 12 13 |
# File 'lib/flexi_view/attribute.rb', line 7 def initialize(name, , &data_block) @name = name @options = || {} @serialized_name = (@options[:as] || name).to_sym @formatter = FlexiView.formatters[@options[:format]] @data_block = data_block if block_given? end |
Instance Attribute Details
#data_block ⇒ Object
Returns the value of attribute data_block.
5 6 7 |
# File 'lib/flexi_view/attribute.rb', line 5 def data_block @data_block end |
#formatter ⇒ Object
Returns the value of attribute formatter.
5 6 7 |
# File 'lib/flexi_view/attribute.rb', line 5 def formatter @formatter end |
#name ⇒ Object
Returns the value of attribute name.
5 6 7 |
# File 'lib/flexi_view/attribute.rb', line 5 def name @name end |
#options ⇒ Object
Returns the value of attribute options.
5 6 7 |
# File 'lib/flexi_view/attribute.rb', line 5 def @options end |
#serialized_name ⇒ Object
Returns the value of attribute serialized_name.
5 6 7 |
# File 'lib/flexi_view/attribute.rb', line 5 def serialized_name @serialized_name end |
Instance Method Details
#condition?(serializer) ⇒ Boolean
15 16 17 18 19 20 21 22 23 |
# File 'lib/flexi_view/attribute.rb', line 15 def condition?(serializer) return true if [:if].nil? if [:if].is_a?(Proc) serializer.instance_exec(&[:if]) else serializer.public_send([:if]) end end |
#to_data(serializer) ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/flexi_view/attribute.rb', line 25 def to_data(serializer) value = if data_block serializer.instance_exec(serializer.object, &data_block) else serializer.object.public_send(name) end formatter ? formatter.format(value) : value end |