Class: AdequateSerializer::Base

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/adequate_serializer/base.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helper

#serialize

Constructor Details

#initialize(object, options = {}) ⇒ Base

Returns a new instance of Base.



34
35
36
37
38
39
# File 'lib/adequate_serializer/base.rb', line 34

def initialize(object, options = {})
  @object = object
  @includes = options[:includes]
  @root = options[:root]
  @scope = options[:scope]
end

Class Attribute Details

._associationsObject

Returns the value of attribute _associations.



9
10
11
# File 'lib/adequate_serializer/base.rb', line 9

def _associations
  @_associations
end

._attributesObject

Returns the value of attribute _attributes.



10
11
12
# File 'lib/adequate_serializer/base.rb', line 10

def _attributes
  @_attributes
end

Instance Attribute Details

#includesObject

Returns the value of attribute includes.



32
33
34
# File 'lib/adequate_serializer/base.rb', line 32

def includes
  @includes
end

#objectObject

Returns the value of attribute object.



32
33
34
# File 'lib/adequate_serializer/base.rb', line 32

def object
  @object
end

#rootObject

Returns the value of attribute root.



32
33
34
# File 'lib/adequate_serializer/base.rb', line 32

def root
  @root
end

#scopeObject

Returns the value of attribute scope.



32
33
34
# File 'lib/adequate_serializer/base.rb', line 32

def scope
  @scope
end

Class Method Details

.associations(*associations) ⇒ Object



26
27
28
29
30
# File 'lib/adequate_serializer/base.rb', line 26

def self.associations(*associations)
  associations.each do |association|
    @_associations << association
  end
end

.attributes(*attributes) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/adequate_serializer/base.rb', line 18

def self.attributes(*attributes)
  attributes.each do |attribute|
    @_attributes << attribute

    define_method_for(attribute)
  end
end

.inherited(base) ⇒ Object



13
14
15
16
# File 'lib/adequate_serializer/base.rb', line 13

def self.inherited(base)
  base._associations = (_associations || []).dup
  base._attributes = (_attributes || []).dup
end

Instance Method Details

#as_json(options = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/adequate_serializer/base.rb', line 41

def as_json(options = {})
  hash = attributes
  hash.merge!(associations)

  if root == false
    hash
  else
    { root_name => hash }
  end
end

#associationsObject



59
60
61
# File 'lib/adequate_serializer/base.rb', line 59

def associations
  serialize_multiple_associations(normalized_associations)
end

#attributesObject



52
53
54
55
56
57
# File 'lib/adequate_serializer/base.rb', line 52

def attributes
  self.class._attributes.each_with_object({}) do |name, hash|
    clean_name = clean_name(name)
    hash[clean_name] = send(name)
  end
end