Class: Cyrax::Serializer

Inherits:
Wrapper show all
Defined in:
lib/cyrax/serializer.rb

Instance Attribute Summary

Attributes inherited from Wrapper

#accessor, #options, #resource

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Wrapper

#initialize

Constructor Details

This class inherits a constructor from Cyrax::Wrapper

Class Method Details

.assigned(name, &block) ⇒ Object



39
40
41
# File 'lib/cyrax/serializer.rb', line 39

def assigned(name, &block)
  scope.assigned(name, &block)
end

.attribute(attribute, options = {}, &block) ⇒ Object



57
58
59
# File 'lib/cyrax/serializer.rb', line 57

def attribute(attribute, options = {}, &block)
  scope.attribute(attribute, options, &block)
end

.attributes(*attrs) ⇒ Object



53
54
55
# File 'lib/cyrax/serializer.rb', line 53

def attributes(*attrs)
  scope.attributes(*attrs)
end

.default_attributesObject



49
50
51
# File 'lib/cyrax/serializer.rb', line 49

def default_attributes
  scope.default_attributes
end

.namespace(name, &block) ⇒ Object



35
36
37
# File 'lib/cyrax/serializer.rb', line 35

def namespace(name, &block)
  scope.namespace(name, &block)
end

.relation(name, &block) ⇒ Object Also known as: has_many, has_one



43
44
45
# File 'lib/cyrax/serializer.rb', line 43

def relation(name, &block)
  scope.relation(name, &block)
end

.scopeObject



23
24
25
# File 'lib/cyrax/serializer.rb', line 23

def scope
  @scope ||= Cyrax::Serializers::Scope.new()
end

.total_count(&block) ⇒ Object



27
28
29
# File 'lib/cyrax/serializer.rb', line 27

def total_count(&block)
  @total_count_block = block
end

.total_count_blockObject



31
32
33
# File 'lib/cyrax/serializer.rb', line 31

def total_count_block
  @total_count_block
end

Instance Method Details

#serializeObject



2
3
4
5
6
7
8
9
10
# File 'lib/cyrax/serializer.rb', line 2

def serialize
  options[:serializer] = self
  
  if block = self.class.total_count_block
    serialize_wrapped(options, &block)
  else
    serialize_simple(options)
  end
end

#serialize_simple(options = {}) ⇒ Object



12
13
14
# File 'lib/cyrax/serializer.rb', line 12

def serialize_simple(options = {})
  self.class.scope.serialize(resource, options)
end

#serialize_wrapped(options = {}, &counter_block) ⇒ Object



16
17
18
19
20
# File 'lib/cyrax/serializer.rb', line 16

def serialize_wrapped(options = {}, &counter_block)
  total_count = self.instance_eval(&counter_block)
  data = self.class.scope.serialize(resource, options)
  {total_count: total_count, data: data}
end