Class: Jserializer::Base
- Inherits:
-
Object
- Object
- Jserializer::Base
- Defined in:
- lib/jserializer/base.rb
Class Attribute Summary collapse
-
._attributes ⇒ Object
Returns the value of attribute _attributes.
-
._embed ⇒ Object
Returns the value of attribute _embed.
-
._root_key ⇒ Object
Returns the value of attribute _root_key.
Instance Attribute Summary collapse
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#scope ⇒ Object
(also: #current_user)
readonly
Returns the value of attribute scope.
Class Method Summary collapse
- ._add_attribute(name, key, association: nil) ⇒ Object
- ._build_association(name, type, key, serializer, embed, embed_key) ⇒ Object
- .attribute(name, key: nil) ⇒ Object
- .attributes(*names) ⇒ Object
-
.embed(type = :objects) ⇒ Object
Define how associations should be embedded.
-
.generate_attribute_methods(name, access_name) ⇒ Object
Generate attribute access and inclusion check methods This improves performance by avoiding method lookups like: public_send(name) if respond_to?(name).
-
.has_many(name, serializer: nil, key: nil, embed: nil, embed_key: nil) ⇒ Object
embed: :objects || :ids the embed_key only works when embed: ids.
- .has_one(name, serializer: nil, key: nil, embed: nil, embed_key: nil) ⇒ Object
- .inherited(subclass) ⇒ Object
- .root(name) ⇒ Object
Instance Method Summary collapse
-
#as_json(options = {}) ⇒ Object
Returns a hash representation with the root Available options: :root => true or false.
- #collection? ⇒ Boolean
-
#initialize(object, options = {}) ⇒ Base
constructor
supported options: root: meta: meta_key: scope or current_user: is_collection: only: [] except: [].
- #meta_key ⇒ Object
-
#reset(object) ⇒ Object
reset object to reuse the serializer instance clear any cached or memoized things.
- #root_name ⇒ Object
- #serializable_collection ⇒ Object
-
#serializable_hash ⇒ Object
Returns a hash representation without the root.
- #to_json ⇒ Object
Constructor Details
#initialize(object, options = {}) ⇒ Base
supported options:
root:
meta:
meta_key:
scope or current_user:
is_collection:
only: []
except: []
105 106 107 108 109 110 111 |
# File 'lib/jserializer/base.rb', line 105 def initialize(object, = {}) @object = object @scope = [:scope] || [:current_user] @is_collection = .delete(:is_collection) || false @options = _update_attributes_filter end |
Class Attribute Details
._attributes ⇒ Object
Returns the value of attribute _attributes.
7 8 9 |
# File 'lib/jserializer/base.rb', line 7 def _attributes @_attributes end |
._embed ⇒ Object
Returns the value of attribute _embed.
7 8 9 |
# File 'lib/jserializer/base.rb', line 7 def @_embed end |
._root_key ⇒ Object
Returns the value of attribute _root_key.
7 8 9 |
# File 'lib/jserializer/base.rb', line 7 def _root_key @_root_key end |
Instance Attribute Details
#object ⇒ Object (readonly)
Returns the value of attribute object.
93 94 95 |
# File 'lib/jserializer/base.rb', line 93 def object @object end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
93 94 95 |
# File 'lib/jserializer/base.rb', line 93 def @options end |
#scope ⇒ Object (readonly) Also known as: current_user
Returns the value of attribute scope.
93 94 95 |
# File 'lib/jserializer/base.rb', line 93 def scope @scope end |
Class Method Details
._add_attribute(name, key, association: nil) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/jserializer/base.rb', line 55 def _add_attribute(name, key, association: nil) self._attributes[name] = { key: key, include_method: "include_#{name}?".to_sym } if association self._attributes[name][:association] = association self._attributes[name][:key] = association.key end access_name = association ? association.access_name : name generate_attribute_methods(name, access_name) end |
._build_association(name, type, key, serializer, embed, embed_key) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/jserializer/base.rb', line 46 def _build_association(name, type, key, serializer, , ) id_only = == :ids || (.nil? && self. == :ids) Association.new( name, type, key: key, serializer: serializer, id_only: id_only, embed_key: ) end |
.attribute(name, key: nil) ⇒ Object
13 14 15 |
# File 'lib/jserializer/base.rb', line 13 def attribute(name, key: nil) _add_attribute(name, key) end |
.attributes(*names) ⇒ Object
9 10 11 |
# File 'lib/jserializer/base.rb', line 9 def attributes(*names) names.each { |name| _add_attribute(name, nil) } end |
.embed(type = :objects) ⇒ Object
Define how associations should be embedded.
:objects # Embed associations as full objects
:ids # Embed only the association ids
26 27 28 |
# File 'lib/jserializer/base.rb', line 26 def (type = :objects) self. = type end |
.generate_attribute_methods(name, access_name) ⇒ Object
Generate attribute access and inclusion check methods This improves performance by avoiding method lookups like:
public_send(name) if respond_to?(name)
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/jserializer/base.rb', line 71 def generate_attribute_methods(name, access_name) class_eval <<-METHOD, __FILE__, __LINE__ + 1 def #{name} if ::Hash === @object @object.fetch(:'#{access_name}') else @object.#{access_name} end end def include_#{name}? true end METHOD end |
.has_many(name, serializer: nil, key: nil, embed: nil, embed_key: nil) ⇒ Object
embed: :objects || :ids the embed_key only works when embed: ids
32 33 34 35 36 37 |
# File 'lib/jserializer/base.rb', line 32 def has_many(name, serializer: nil, key: nil, embed: nil, embed_key: nil) association = _build_association( name, :has_many, key, serializer, , ) _add_attribute(name, key, association: association) end |
.has_one(name, serializer: nil, key: nil, embed: nil, embed_key: nil) ⇒ Object
39 40 41 42 43 44 |
# File 'lib/jserializer/base.rb', line 39 def has_one(name, serializer: nil, key: nil, embed: nil, embed_key: nil) association = _build_association( name, :has_one, key, serializer, , ) _add_attribute(name, key, association: association) end |
.inherited(subclass) ⇒ Object
87 88 89 90 |
# File 'lib/jserializer/base.rb', line 87 def inherited(subclass) super(subclass) subclass._attributes = _attributes.dup end |
.root(name) ⇒ Object
17 18 19 |
# File 'lib/jserializer/base.rb', line 17 def root(name) self._root_key = name end |
Instance Method Details
#as_json(options = {}) ⇒ Object
Returns a hash representation with the root Available options: :root => true or false
157 158 159 160 161 162 163 164 165 166 |
# File 'lib/jserializer/base.rb', line 157 def as_json( = {}) root = .key?(:root) ? [:root] : true hash = if root && root_name { root_name => serializable_hash } else serializable_hash end hash[] = @options[:meta] if @options.key?(:meta) hash end |
#collection? ⇒ Boolean
137 138 139 |
# File 'lib/jserializer/base.rb', line 137 def collection? @is_collection end |
#meta_key ⇒ Object
146 147 148 |
# File 'lib/jserializer/base.rb', line 146 def @options[:meta_key] || :meta end |
#reset(object) ⇒ Object
reset object to reuse the serializer instance clear any cached or memoized things
115 116 117 |
# File 'lib/jserializer/base.rb', line 115 def reset(object) @object = object end |
#root_name ⇒ Object
141 142 143 144 |
# File 'lib/jserializer/base.rb', line 141 def root_name return nil if @options[:root] == false @options[:root] || self.class._root_key end |
#serializable_collection ⇒ Object
129 130 131 132 133 134 135 |
# File 'lib/jserializer/base.rb', line 129 def serializable_collection serializer_object = self.class.new(nil, @options) @object.map do |record| serializer_object.reset(record) serializer_object.serializable_hash end end |
#serializable_hash ⇒ Object
Returns a hash representation without the root
120 121 122 123 124 125 126 127 |
# File 'lib/jserializer/base.rb', line 120 def serializable_hash return serializable_collection if collection? self.class._attributes.each_with_object({}) do |(name, option), hash| if (name) && public_send(option[:include_method]) hash[option[:key] || name] = _set_value(name, option) end end end |
#to_json ⇒ Object
150 151 152 |
# File 'lib/jserializer/base.rb', line 150 def to_json(*) JSON.generate(as_json) end |