Class: Jserializer::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/jserializer/base.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, options = {})
  @object = object
  @scope = options[:scope] || options[:current_user]
  @is_collection = options.delete(:is_collection) || false
  @options = options
  _update_attributes_filter
end

Class Attribute Details

._attributesObject

Returns the value of attribute _attributes.



7
8
9
# File 'lib/jserializer/base.rb', line 7

def _attributes
  @_attributes
end

._embedObject

Returns the value of attribute _embed.



7
8
9
# File 'lib/jserializer/base.rb', line 7

def _embed
  @_embed
end

._root_keyObject

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

#objectObject (readonly)

Returns the value of attribute object.



93
94
95
# File 'lib/jserializer/base.rb', line 93

def object
  @object
end

#optionsObject (readonly)

Returns the value of attribute options.



93
94
95
# File 'lib/jserializer/base.rb', line 93

def options
  @options
end

#scopeObject (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, embed, embed_key)
  id_only = embed == :ids || (embed.nil? && self._embed == :ids)
  Association.new(
    name, type,
    key: key, serializer: serializer,
    id_only: id_only, embed_key: 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.

embed :objects # Embed associations as full objects
embed :ids     # Embed only the association ids


26
27
28
# File 'lib/jserializer/base.rb', line 26

def embed(type = :objects)
  self._embed = 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, embed, embed_key
  )
  _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, embed, embed_key
  )
  _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(options = {})
  root = options.key?(:root) ? options[:root] : true
  hash = if root && root_name
           { root_name => serializable_hash }
         else
           serializable_hash
         end
  hash[meta_key] = @options[:meta] if @options.key?(:meta)
  hash
end

#collection?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/jserializer/base.rb', line 137

def collection?
  @is_collection
end

#meta_keyObject



146
147
148
# File 'lib/jserializer/base.rb', line 146

def meta_key
  @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_nameObject



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_collectionObject



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_hashObject

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 _include_from_options?(name) && public_send(option[:include_method])
      hash[option[:key] || name] = _set_value(name, option)
    end
  end
end

#to_jsonObject



150
151
152
# File 'lib/jserializer/base.rb', line 150

def to_json(*)
  JSON.generate(as_json)
end