Class: Fars::BaseObjectSerializer
- Inherits:
-
Object
- Object
- Fars::BaseObjectSerializer
- Defined in:
- lib/fars/base_object_serializer.rb
Overview
Class: BaseObjectSerializer
Direct Known Subclasses
Class Attribute Summary collapse
-
.model ⇒ Object
Returns: Class of serialized model (can be nil).
-
.root_key ⇒ Object
Returns: Symbol instance hash root key, as an underscored Model name.
Class Method Summary collapse
- .all_attributes ⇒ Object
-
.api_version ⇒ Object
Returns: String capitalized API version (can be nil).
- .attributes(*attrs) ⇒ Object
-
.serializer_methods ⇒ Object
Returns: Array with names of this serializer instance methods.
Instance Method Summary collapse
- #as_json ⇒ Object
- #call(obj) ⇒ Object
-
#initialize(object, opts = {}) ⇒ BaseObjectSerializer
constructor
Initialize new instance.
- #to_json ⇒ Object
- #with_object(object) ⇒ Object
Constructor Details
#initialize(object, opts = {}) ⇒ BaseObjectSerializer
Initialize new instance
Params:
- object {ActiveRecord::Base} or any {Object} to serialize
- opts {Hash} of options:
- fields {Array} of attributes to serialize. Can be {NilClass}.
If so - will use all available.
- scope {Object} context of request. Usually current user
or current ability. Can be passed as a {Proc}. If so -
evaluated only when actually called.
- :add_metadata {Boolean} if to add a node '_metadata'
- :root_key {Symbol} overwrites the default one from serializer's Class
64 65 66 67 68 69 70 71 72 |
# File 'lib/fars/base_object_serializer.rb', line 64 def initialize(object, opts = {}) @object = object @scope = opts[:scope] @fields = opts[:fields] @add_metadata = opts.fetch(:add_metadata, self.respond_to?(:meta)) @root_key = opts.fetch(:root_key, self.class.root_key) @api_version = opts.fetch(:api_version, self.class.api_version) @params = opts[:params] || {} end |
Class Attribute Details
.model ⇒ Object
Returns: Class of serialized model (can be nil)
17 18 19 20 21 22 |
# File 'lib/fars/base_object_serializer.rb', line 17 def model @model ||= begin @class_name ||= self.to_s.sub(/^V\d+::/, '').sub(/Serializer$/, '') @class_name.constantize if Object.const_defined?(@class_name) end end |
.root_key ⇒ Object
Returns: Symbol instance hash root key, as an underscored Model name
28 29 30 |
# File 'lib/fars/base_object_serializer.rb', line 28 def root_key @root_key ||= (model ? model.to_s.underscore.to_sym : nil) end |
Class Method Details
.all_attributes ⇒ Object
10 11 12 |
# File 'lib/fars/base_object_serializer.rb', line 10 def all_attributes @attributes end |
.api_version ⇒ Object
Returns: String capitalized API version (can be nil)
36 37 38 39 |
# File 'lib/fars/base_object_serializer.rb', line 36 def api_version namespace_array = name.split('::') namespace_array.size > 1 ? namespace_array[0] : nil end |
.attributes(*attrs) ⇒ Object
6 7 8 |
# File 'lib/fars/base_object_serializer.rb', line 6 def attributes(*attrs) @attributes = attrs.map(&:to_sym) end |
.serializer_methods ⇒ Object
Returns: Array with names of this serializer instance methods. Consists of Symbols Filtrated by #all_attributes
45 46 47 |
# File 'lib/fars/base_object_serializer.rb', line 45 def serializer_methods @serializer_methods ||= self.instance_methods & all_attributes end |
Instance Method Details
#as_json ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/fars/base_object_serializer.rb', line 79 def as_json item = {} ((requested_attributes - requested_serializer_methods) & available_attributes).each do |m| next if m == :id && !object.respond_to?(:id) item[m] = object.public_send(m) end (requested_serializer_methods & available_attributes).each do |m| item[m] = self.public_send(m) end hash = { root_key => item } hash[:_metadata] = if hash end |
#call(obj) ⇒ Object
97 98 99 |
# File 'lib/fars/base_object_serializer.rb', line 97 def call(obj) with_object(obj).as_json end |
#to_json ⇒ Object
93 94 95 |
# File 'lib/fars/base_object_serializer.rb', line 93 def to_json MultiJson.dump(as_json) end |
#with_object(object) ⇒ Object
74 75 76 77 |
# File 'lib/fars/base_object_serializer.rb', line 74 def with_object(object) @object = object self end |