Class: USerializer::BaseSerializer
- Inherits:
-
Object
- Object
- USerializer::BaseSerializer
- Defined in:
- lib/userializer/base_serializer.rb
Class Attribute Summary collapse
-
.attrs ⇒ Object
Returns the value of attribute attrs.
-
.relations ⇒ Object
Returns the value of attribute relations.
Instance Attribute Summary collapse
-
#meta ⇒ Object
readonly
Returns the value of attribute meta.
-
#obj ⇒ Object
(also: #object)
readonly
Returns the value of attribute obj.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
Class Method Summary collapse
- .attributes(*attrs, &block) ⇒ Object
- .has_many(*attrs) ⇒ Object
- .has_one(*attrs) ⇒ Object
- .inherited(subclass) ⇒ Object
Instance Method Summary collapse
-
#initialize(obj, opts = {}) ⇒ BaseSerializer
constructor
A new instance of BaseSerializer.
- #merge_root(res, key, single, opts) ⇒ Object
- #method_missing(mth) ⇒ Object
- #serializable_hash(opts) ⇒ Object
- #to_hash ⇒ Object
- #to_json ⇒ Object
Constructor Details
#initialize(obj, opts = {}) ⇒ BaseSerializer
Returns a new instance of BaseSerializer.
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/userializer/base_serializer.rb', line 43 def initialize(obj, opts = {}) @obj = obj @opts = opts @meta = opts[:meta] @except = Set.new([opts[:except]].flatten.compact) @only = Set.new([opts[:only]].flatten.compact) @root_key = (opts[:root] || ActiveSupport::Inflector.underscore( obj.class.name ).split('/').last).to_sym end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(mth) ⇒ Object
98 |
# File 'lib/userializer/base_serializer.rb', line 98 def method_missing(mth); @obj.send(mth); end |
Class Attribute Details
.attrs ⇒ Object
Returns the value of attribute attrs.
36 37 38 |
# File 'lib/userializer/base_serializer.rb', line 36 def attrs @attrs end |
.relations ⇒ Object
Returns the value of attribute relations.
36 37 38 |
# File 'lib/userializer/base_serializer.rb', line 36 def relations @relations end |
Instance Attribute Details
#meta ⇒ Object (readonly)
Returns the value of attribute meta.
39 40 41 |
# File 'lib/userializer/base_serializer.rb', line 39 def @meta end |
#obj ⇒ Object (readonly) Also known as: object
Returns the value of attribute obj.
39 40 41 |
# File 'lib/userializer/base_serializer.rb', line 39 def obj @obj end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
39 40 41 |
# File 'lib/userializer/base_serializer.rb', line 39 def opts @opts end |
Class Method Details
.attributes(*attrs, &block) ⇒ Object
15 16 17 18 19 20 |
# File 'lib/userializer/base_serializer.rb', line 15 def attributes(*attrs, &block) attrs = attrs.first if attrs.first.class.is_a?(Array) opts = attrs.last.is_a?(Hash) ? attrs.pop : {} attrs.each { |attr| @attrs[attr] = Attribute.new(attr, opts, block) } end |
.has_many(*attrs) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/userializer/base_serializer.rb', line 29 def has_many(*attrs) attrs = attrs.first if attrs.first.class.is_a?(Array) opts = attrs.last.is_a?(Hash) ? attrs.pop : {} attrs.each { |attr| @relations[attr] = HasMany.new(attr, opts) } end |
Instance Method Details
#merge_root(res, key, single, opts) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/userializer/base_serializer.rb', line 66 def merge_root(res, key, single, opts) if single res[key] = serializable_hash(opts) else res[key] ||= [] id = @obj.id if res[key].detect { |v| id && v[:id] == id } return else res[key] << serializable_hash(opts) end end relations.each { |rel| rel.merge_root(res, self, opts) } end |
#serializable_hash(opts) ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/userializer/base_serializer.rb', line 55 def serializable_hash(opts) res = {} attributes.each { |attr| attr.merge_attributes(res, self, opts) } relations.each do |rel| rel.merge_attributes(res, self, opts) end res end |
#to_hash ⇒ Object
84 85 86 87 88 89 90 91 92 |
# File 'lib/userializer/base_serializer.rb', line 84 def to_hash res = {} merge_root(res, @root_key, true, @opts.slice(:scope)) res[:meta] = @meta if @meta res end |
#to_json ⇒ Object
94 95 96 |
# File 'lib/userializer/base_serializer.rb', line 94 def to_json Oj.dump(to_hash, mode: :compat) end |