Class: ScopedSerializer::Serializer
- Inherits:
-
BaseSerializer
- Object
- BaseSerializer
- ScopedSerializer::Serializer
- Defined in:
- lib/scoped_serializer/serializer.rb
Class Attribute Summary collapse
-
.default_scope ⇒ Object
Returns the value of attribute default_scope.
-
.scopes ⇒ Object
Returns the value of attribute scopes.
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
Class Method Summary collapse
-
.find_scope(name) ⇒ Object
Finds a defined scope by name.
-
.inherited(base) ⇒ Object
Set default values.
-
.method ⇒ Object
Define available default scope methods.
-
.scope(name, &block) ⇒ Object
Defines a scope.
Instance Method Summary collapse
-
#associations_hash ⇒ Hash
Collects associations for serialization.
-
#attributes_hash ⇒ Hash
Collects attributes for serialization.
-
#fetch_association(name, includes = nil) ⇒ Object
Fetches association and eager loads data.
-
#fetch_property(property) ⇒ Object
Fetches property from the serializer or resource.
-
#initialize(resource, options = {}) ⇒ Serializer
constructor
A new instance of Serializer.
-
#render_association(association_data, options = {}) ⇒ Hash
Renders a specific association.
-
#serializable_hash(options = {}) ⇒ Object
The serializable hash returned.
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, ={}) @resource = resource @options = || {} scope = [: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.).merge(@options) if @resource @options[:root] = default_root_key(@resource.class) unless @options.key?(:root) end end |
Class Attribute Details
.default_scope ⇒ Object
Returns the value of attribute default_scope.
6 7 8 |
# File 'lib/scoped_serializer/serializer.rb', line 6 def default_scope @default_scope end |
.scopes ⇒ Object
Returns the value of attribute scopes.
6 7 8 |
# File 'lib/scoped_serializer/serializer.rb', line 6 def scopes @scopes end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
62 63 64 |
# File 'lib/scoped_serializer/serializer.rb', line 62 def @options end |
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
62 63 64 |
# File 'lib/scoped_serializer/serializer.rb', line 62 def resource @resource end |
#scope ⇒ Object (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 |
.method ⇒ Object
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
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_hash ⇒ Hash
Collects associations for serialization. Associations can be overwritten in the serializer.
116 117 118 119 120 121 122 |
# File 'lib/scoped_serializer/serializer.rb', line 116 def associations_hash hash = {} @scope.associations.each do |association, | hash.merge!(render_association(association, )) end hash end |
#attributes_hash ⇒ Hash
Collects attributes for serialization. Attributes can be overwritten in the serializer.
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.
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.
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, ={}) hash = {} if association_data.is_a?(Hash) association_data.each do |association, | data = render_association(association, .merge(:include => )) 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 [:preload] includes = [:preload] == true ? [:include] : [:preload] end if (object = fetch_association(association_data, includes)) data = ScopedSerializer.for(object, .merge(:associations => [: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(={}) {}.merge(attributes_hash).merge(associations_hash) end |