Class: Cyrax::Serializers::Scope
- Inherits:
-
Object
- Object
- Cyrax::Serializers::Scope
- Defined in:
- lib/cyrax/serializers/scope.rb
Instance Method Summary collapse
- #assigned(name, &block) ⇒ Object
- #attribute(attribute, options = {}, &block) ⇒ Object
- #attributes(*attrs) ⇒ Object
- #default_attributes ⇒ Object
-
#initialize(&block) ⇒ Scope
constructor
A new instance of Scope.
- #namespace(name, &block) ⇒ Object
- #relation(attribute, &block) ⇒ Object (also: #has_many, #has_one)
- #serialize(resource, options = {}) ⇒ Object
- #serialize_one(resource, options = {}) ⇒ Object
Constructor Details
#initialize(&block) ⇒ Scope
Returns a new instance of Scope.
3 4 5 6 7 8 9 10 11 |
# File 'lib/cyrax/serializers/scope.rb', line 3 def initialize(&block) @attrs = {} @dynamic_attrs = {} @relation_attrs = {} @namespace_attrs = {} @assigned_attrs = {} @default_attributes = false instance_eval(&block) if block_given? end |
Instance Method Details
#assigned(name, &block) ⇒ Object
17 18 19 |
# File 'lib/cyrax/serializers/scope.rb', line 17 def assigned(name, &block) @assigned_attrs[name] = self.class.new(&block) end |
#attribute(attribute, options = {}, &block) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/cyrax/serializers/scope.rb', line 41 def attribute(attribute, = {}, &block) if block_given? @dynamic_attrs[attribute] = block else @attrs[attribute] = attribute end end |
#attributes(*attrs) ⇒ Object
35 36 37 38 39 |
# File 'lib/cyrax/serializers/scope.rb', line 35 def attributes(*attrs) attrs.map do |attribute| attribute(attribute) end end |
#default_attributes ⇒ Object
31 32 33 |
# File 'lib/cyrax/serializers/scope.rb', line 31 def default_attributes @default_attributes = true end |
#namespace(name, &block) ⇒ Object
13 14 15 |
# File 'lib/cyrax/serializers/scope.rb', line 13 def namespace(name, &block) @namespace_attrs[name] = self.class.new(&block) end |
#relation(attribute, &block) ⇒ Object Also known as: has_many, has_one
21 22 23 24 25 26 27 |
# File 'lib/cyrax/serializers/scope.rb', line 21 def relation(attribute, &block) if block_given? @relation_attrs[attribute] = self.class.new(&block) else @attrs[attribute] = attribute end end |
#serialize(resource, options = {}) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/cyrax/serializers/scope.rb', line 49 def serialize(resource, = {}) if resource.respond_to?(:to_a) resource.to_a.map{ |r| serialize_one(r, ) } else serialize_one(resource, ) end end |
#serialize_one(resource, options = {}) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/cyrax/serializers/scope.rb', line 57 def serialize_one(resource, = {}) result = {} if @default_attributes result = resource.attributes rescue {} end @dynamic_attrs.map do |attribute, block| result[attribute] = [:serializer].instance_exec(resource, , &block) end @relation_attrs.map do |attribute, scope| value = resource.send(attribute) result[attribute] = scope.serialize(value, ) end @namespace_attrs.map do |attribute, scope| result[attribute] = scope.serialize(resource, ) end @assigned_attrs.map do |attribute, scope| value = [:assignments][attribute] result[attribute] = scope.serialize(value, ) end @attrs.map do |attribute, | result[attribute] = resource.send(attribute) end result end |