Class: JsonSerializer
- Inherits:
-
Object
- Object
- JsonSerializer
- Defined in:
- lib/json_serializer.rb
Defined Under Namespace
Modules: Utils
Instance Attribute Summary collapse
-
#object ⇒ Object
readonly
Returns the value of attribute object.
Class Method Summary collapse
Instance Method Summary collapse
- #decorate ⇒ Object
-
#initialize(object) ⇒ JsonSerializer
constructor
A new instance of JsonSerializer.
- #to_json(root: nil) ⇒ Object
Constructor Details
#initialize(object) ⇒ JsonSerializer
Returns a new instance of JsonSerializer.
32 33 34 |
# File 'lib/json_serializer.rb', line 32 def initialize(object) @object = object end |
Instance Attribute Details
#object ⇒ Object (readonly)
Returns the value of attribute object.
30 31 32 |
# File 'lib/json_serializer.rb', line 30 def object @object end |
Class Method Details
.attribute(name, serializer = nil) ⇒ Object
22 23 24 |
# File 'lib/json_serializer.rb', line 22 def self.attribute(name, serializer = nil) attributes[name] ||= serializer end |
.attributes ⇒ Object
26 27 28 |
# File 'lib/json_serializer.rb', line 26 def self.attributes @attributes ||= {} end |
.inherited(subclass) ⇒ Object
16 17 18 19 20 |
# File 'lib/json_serializer.rb', line 16 def self.inherited(subclass) attributes.each do |name, serializer| subclass.attribute(name, serializer) end end |
Instance Method Details
#decorate ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/json_serializer.rb', line 43 def decorate return nil unless object if object.respond_to?(:to_a) to_arry else to_hash end end |
#to_json(root: nil) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/json_serializer.rb', line 36 def to_json(root: nil) result = decorate result = { root => result } if root result.to_json end |