Class: FlexiView::Association
- Inherits:
-
Object
- Object
- FlexiView::Association
- Defined in:
- lib/flexi_view/association.rb
Instance Attribute Summary collapse
-
#data_block ⇒ Object
Returns the value of attribute data_block.
-
#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(type, name, options, &data_block) ⇒ Association
constructor
A new instance of Association.
- #serializer_class ⇒ Object
- #to_data(parent_serializer, namespace, view) ⇒ Object
Constructor Details
#initialize(type, name, options, &data_block) ⇒ Association
Returns a new instance of Association.
7 8 9 10 11 12 13 14 |
# File 'lib/flexi_view/association.rb', line 7 def initialize(type, name, , &data_block) @type = type @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/association.rb', line 5 def data_block @data_block end |
#name ⇒ Object
Returns the value of attribute name.
5 6 7 |
# File 'lib/flexi_view/association.rb', line 5 def name @name end |
#options ⇒ Object
Returns the value of attribute options.
5 6 7 |
# File 'lib/flexi_view/association.rb', line 5 def @options end |
#serialized_name ⇒ Object
Returns the value of attribute serialized_name.
5 6 7 |
# File 'lib/flexi_view/association.rb', line 5 def serialized_name @serialized_name end |
Instance Method Details
#condition?(serializer) ⇒ Boolean
20 21 22 23 24 25 26 27 28 |
# File 'lib/flexi_view/association.rb', line 20 def condition?(serializer) return true if [:if].nil? if [:if].is_a?(Proc) serializer.instance_exec(&[:if]) else serializer.public_send([:if]) end end |
#serializer_class ⇒ Object
16 17 18 |
# File 'lib/flexi_view/association.rb', line 16 def serializer_class @serializer_class ||= Object.const_get([:serializer_class].to_s) end |
#to_data(parent_serializer, namespace, view) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/flexi_view/association.rb', line 30 def to_data(parent_serializer, namespace, view) parent_obj = parent_serializer.object objects = if data_block parent_serializer.instance_exec(parent_obj, &data_block) else parent_obj.public_send(name) end serializer = serializer_class.new(objects, options: parent_serializer.) serializer.serializable_hash(namespace: namespace, view: view) end |