Class: Cyrax::Serializers::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/cyrax/serializers/scope.rb

Instance Method Summary collapse

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, options = {}, &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_attributesObject



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, options = {})
  if resource.respond_to?(:to_a)
    resource.to_a.map{ |r| serialize_one(r, options) }
  else
    serialize_one(resource, options)
  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, options = {})
  result = {}
  if @default_attributes
    result = resource.attributes rescue {}
  end
  @dynamic_attrs.map do |attribute, block|
    result[attribute] = options[:serializer].instance_exec(resource, options, &block)
  end
  @relation_attrs.map do |attribute, scope|
    value = resource.send(attribute)
    result[attribute] = scope.serialize(value, options)
  end
  @namespace_attrs.map do |attribute, scope|
    result[attribute] = scope.serialize(resource, options)
  end
  @assigned_attrs.map do |attribute, scope|
    value = options[:assignments][attribute]
    result[attribute] = scope.serialize(value, options)
  end
  @attrs.map do |attribute, options|
    result[attribute] = resource.send(attribute)
  end
  result
end