Class: FlexiView::View
- Inherits:
-
Object
- Object
- FlexiView::View
- Defined in:
- lib/flexi_view/view.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
- #association(name, namespace: nil, view: nil) ⇒ Object
- #associations(*names, namespace: nil, view: nil) ⇒ Object
- #fields(*names) ⇒ Object
-
#initialize(name, serializer_class) ⇒ View
constructor
A new instance of View.
- #meta(name) ⇒ Object
- #serializable_hash(serializer) ⇒ Object
- #serialize_object(serializer, object) ⇒ Object
Constructor Details
#initialize(name, serializer_class) ⇒ View
Returns a new instance of View.
7 8 9 10 11 12 13 |
# File 'lib/flexi_view/view.rb', line 7 def initialize(name, serializer_class) @name = name @serializer_class = serializer_class @fields = {} @associations = {} @meta = nil end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
5 6 7 |
# File 'lib/flexi_view/view.rb', line 5 def name @name end |
Instance Method Details
#association(name, namespace: nil, view: nil) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/flexi_view/view.rb', line 35 def association(name, namespace: nil, view: nil) assoc = @serializer_class.association_map[name.to_sym] raise FlexiView::Error, "#{name} association not defined for #{@serializer_class}" unless assoc @associations[name.to_sym] = { association: assoc, namespace: namespace, view: view } self end |
#associations(*names, namespace: nil, view: nil) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/flexi_view/view.rb', line 27 def associations(*names, namespace: nil, view: nil) names.each do |name| association(name, namespace: namespace, view: view) end self end |
#fields(*names) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/flexi_view/view.rb', line 15 def fields(*names) names.each do |n| attr = @serializer_class.attribute_map[n.to_sym] raise FlexiView::Error, "#{n} attribute not defined for #{@serializer_class}" unless attr @fields[attr.name] = attr end self end |
#meta(name) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/flexi_view/view.rb', line 45 def (name) @meta = @serializer_class.[name.to_sym] raise FlexiView::Error, "#{name} meta defination found for #{@serializer_class}" unless @meta self end |
#serializable_hash(serializer) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/flexi_view/view.rb', line 53 def serializable_hash(serializer) data = if serializer.objects.is_a?(Enumerable) serializer.objects.map { |object| serialize_object(serializer, object) } else serialize_object(serializer, serializer.objects) end return data unless @meta { @meta.name => @meta.to_data(serializer), @meta.root_key => data } end |
#serialize_object(serializer, object) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/flexi_view/view.rb', line 68 def serialize_object(serializer, object) serializer.object = object result = {} @fields.each_value do |attr| result[attr.serialized_name] = attr.to_data(serializer) if attr.condition?(serializer) end @associations.each_value do |params| assoc = params[:association] next unless assoc.condition?(serializer) result[assoc.serialized_name] = assoc.to_data( serializer, params[:namespace], params[:view] ) end result end |