Class: AdequateSerializer::Base
- Inherits:
-
Object
- Object
- AdequateSerializer::Base
- Includes:
- Helper
- Defined in:
- lib/adequate_serializer/base.rb
Class Attribute Summary collapse
-
._associations ⇒ Object
Returns the value of attribute _associations.
-
._attributes ⇒ Object
Returns the value of attribute _attributes.
Instance Attribute Summary collapse
-
#includes ⇒ Object
Returns the value of attribute includes.
-
#object ⇒ Object
Returns the value of attribute object.
-
#root ⇒ Object
Returns the value of attribute root.
-
#scope ⇒ Object
Returns the value of attribute scope.
Class Method Summary collapse
Instance Method Summary collapse
- #as_json(options = {}) ⇒ Object
- #associations ⇒ Object
- #attributes ⇒ Object
-
#initialize(object, options = {}) ⇒ Base
constructor
A new instance of Base.
Methods included from Helper
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, = {}) @object = object @includes = [:includes] @root = [:root] @scope = [:scope] end |
Class Attribute Details
._associations ⇒ Object
Returns the value of attribute _associations.
9 10 11 |
# File 'lib/adequate_serializer/base.rb', line 9 def _associations @_associations end |
._attributes ⇒ Object
Returns the value of attribute _attributes.
10 11 12 |
# File 'lib/adequate_serializer/base.rb', line 10 def _attributes @_attributes end |
Instance Attribute Details
#includes ⇒ Object
Returns the value of attribute includes.
32 33 34 |
# File 'lib/adequate_serializer/base.rb', line 32 def includes @includes end |
#object ⇒ Object
Returns the value of attribute object.
32 33 34 |
# File 'lib/adequate_serializer/base.rb', line 32 def object @object end |
#root ⇒ Object
Returns the value of attribute root.
32 33 34 |
# File 'lib/adequate_serializer/base.rb', line 32 def root @root end |
#scope ⇒ Object
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( = {}) hash = attributes hash.merge!(associations) if root == false hash else { root_name => hash } end end |
#associations ⇒ Object
59 60 61 |
# File 'lib/adequate_serializer/base.rb', line 59 def associations serialize_multiple_associations(normalized_associations) end |
#attributes ⇒ Object
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 |