Class: ScopedSerializer::Serializer

Inherits:
BaseSerializer show all
Defined in:
lib/scoped_serializer/serializer.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseSerializer

#as_json, #data_hash, #default_root_key, #meta, #meta_hash, #set_scope, #to_csv, #to_xls

Constructor Details

#initialize(resource, options = {}) ⇒ Serializer

Returns a new instance of Serializer.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/scoped_serializer/serializer.rb', line 64

def initialize(resource, options={})
  @resource = resource
  @options  = options || {}
  scope = options[:scope] || :default

  if scope.is_a?(Symbol)
    @scope_name = scope
    @scope = self.class.find_scope(scope)
  elsif scope.is_a?(Hash)
    @scope_name = :custom
    @scope = Scope.from_hash(scope)
  else
    @scope_name = scope.name
    @scope = scope
  end

  set_scope(@scope)

  # Inherit options from scope
  @options = {}.merge(@scope.options).merge(@options)

  if @resource
    @options[:root] = default_root_key(@resource.class) unless @options.key?(:root)
  end
end

Class Attribute Details

.default_scopeObject

Returns the value of attribute default_scope.



6
7
8
# File 'lib/scoped_serializer/serializer.rb', line 6

def default_scope
  @default_scope
end

.scopesObject

Returns the value of attribute scopes.



6
7
8
# File 'lib/scoped_serializer/serializer.rb', line 6

def scopes
  @scopes
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



62
63
64
# File 'lib/scoped_serializer/serializer.rb', line 62

def options
  @options
end

#resourceObject (readonly)

Returns the value of attribute resource.



62
63
64
# File 'lib/scoped_serializer/serializer.rb', line 62

def resource
  @resource
end

#scopeObject (readonly)

Returns the value of attribute scope.



62
63
64
# File 'lib/scoped_serializer/serializer.rb', line 62

def scope
  @scope
end

Class Method Details

.find_scope(name) ⇒ Object

Finds a defined scope by name.



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

def find_scope(name)
  self.scopes ||= {}
  self.scopes[name] || self.default_scope || Scope.new(:default)
end

.inherited(base) ⇒ Object

Set default values.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/scoped_serializer/serializer.rb', line 11

def inherited(base)
  base.scopes = {}

  if scopes.present?
    # Inheritance from a serializer that has scoped defined
    scopes.each do |name, scope|
      base.scopes[name] = Scope.new(name, scope)
    end

    base.default_scope = base.find_scope(:default)
  else
    # Nothing to inherit, set defaults
    base.default_scope = Scope.new(:default)
    base.scopes = { :default => base.default_scope }
  end
end

.methodObject

Define available default scope methods. These are default values, thus define them on every scope.



52
53
54
55
56
57
58
# File 'lib/scoped_serializer/serializer.rb', line 52

Scope::METHODS.each do |method|
  define_method method do |*args|
    self.scopes.each do |name, scope|
      scope.send(method, *args)
    end
  end
end

.scope(name, &block) ⇒ Object

Defines a scope. In this scope all scopes methods are available. See ScopedSerializer::Scope

Examples:

scope :resource do
  association :notes, :employee
end


36
37
38
# File 'lib/scoped_serializer/serializer.rb', line 36

def scope(name, &block)
  self.scopes[name] = Scope.new(name, self.default_scope, &block)
end

Instance Method Details

#associations_hashHash

Collects associations for serialization. Associations can be overwritten in the serializer.

Returns:

  • (Hash)


116
117
118
119
120
121
122
# File 'lib/scoped_serializer/serializer.rb', line 116

def associations_hash
  hash = {}
  @scope.associations.each do |association, options|
    hash.merge!(render_association(association, options))
  end
  hash
end

#attributes_hashHash

Collects attributes for serialization. Attributes can be overwritten in the serializer.

Returns:

  • (Hash)


96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/scoped_serializer/serializer.rb', line 96

def attributes_hash
  attributes = @scope.attributes.collect do |attr|
    value = fetch_property(attr)

    if value.kind_of?(BigDecimal)
      value = value.to_f
    end

    [attr, value]
  end

  Hash[attributes]
end

#fetch_association(name, includes = nil) ⇒ Object

Fetches association and eager loads data. Doesn’t eager load when includes is empty or when the association has already been loaded.

Examples:

fetch_association(:comments, :user)


182
183
184
185
186
187
188
189
190
# File 'lib/scoped_serializer/serializer.rb', line 182

def fetch_association(name, includes=nil)
  association = fetch_property(name)

  if includes.present? && ! @resource.association(name).loaded?
    association.includes(includes)
  else
    association
  end
end

#fetch_property(property) ⇒ Object

Fetches property from the serializer or resource. This method makes it possible to overwrite defined attributes or associations.



165
166
167
168
169
170
171
172
173
# File 'lib/scoped_serializer/serializer.rb', line 165

def fetch_property(property)
  return nil unless property

  unless respond_to?(property)
    object = @resource.send(property)
  else
    object = send(property)
  end
end

#render_association(association_data, options = {}) ⇒ Hash

Renders a specific association.

Examples:

render_association(:employee)
render_association([:employee, :company])
render_association({ :employee => :address })

Returns:

  • (Hash)


134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/scoped_serializer/serializer.rb', line 134

def render_association(association_data, options={})
  hash = {}

  if association_data.is_a?(Hash)
    association_data.each do |association, association_options|
      data = render_association(association, options.merge(:include => association_options))
      hash.merge!(data) if data
    end
  elsif association_data.is_a?(Array)
    association_data.each do |option|
      data = render_association(option)
      hash.merge!(data) if data
    end
  else
    if options[:preload]
      includes = options[:preload] == true ? options[:include] : options[:preload]
    end

    if (object = fetch_association(association_data, includes))
      data = ScopedSerializer.for(object, options.merge(:associations => options[:include])).as_json
      hash.merge!(data) if data
    end
  end

  hash
end

#serializable_hash(options = {}) ⇒ Object

The serializable hash returned.



195
196
197
# File 'lib/scoped_serializer/serializer.rb', line 195

def serializable_hash(options={})
  {}.merge(attributes_hash).merge(associations_hash)
end