Class: JsonSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/json_serializer.rb

Defined Under Namespace

Modules: Utils

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ JsonSerializer

Returns a new instance of JsonSerializer.



30
31
32
# File 'lib/json_serializer.rb', line 30

def initialize(object)
  @object = object
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



28
29
30
# File 'lib/json_serializer.rb', line 28

def object
  @object
end

Class Method Details

.attribute(name, serializer = nil) ⇒ Object



20
21
22
# File 'lib/json_serializer.rb', line 20

def self.attribute(name, serializer = nil)
  attributes[name] ||= serializer
end

.attributesObject



24
25
26
# File 'lib/json_serializer.rb', line 24

def self.attributes
  @attributes ||= {}
end

.inherited(subclass) ⇒ Object



14
15
16
17
18
# File 'lib/json_serializer.rb', line 14

def self.inherited(subclass)
  attributes.each do |name, serializer|
    subclass.attribute(name, serializer)
  end
end

Instance Method Details

#to_json(options = {}) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/json_serializer.rb', line 34

def to_json(options={})
  if root = options[:root]
    { root => serializable_object }.to_json
  else
    serializable_object.to_json
  end
end