Class: Builder::JsonFormat

Inherits:
HashStructure show all
Defined in:
lib/builder/json_format.rb

Instance Method Summary collapse

Methods inherited from HashStructure

#array_mode, #content!, #method_missing, #root!, #tag!, #text!

Methods inherited from Abstract

#comment!, #declare!, #instruct!, #new!, #nil?

Constructor Details

#initialize(options = {}) ⇒ JsonFormat

Returns a new instance of JsonFormat.



7
8
9
# File 'lib/builder/json_format.rb', line 7

def initialize(options = {})
  super(options)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Builder::HashStructure

Instance Method Details

#<<(_target) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/builder/json_format.rb', line 19

def <<(_target)
  if _target.is_a?(String)
    _target = ::ActiveSupport::JSON.decode(_target)
    _target.symbolize_keys! if _target.is_a?(Hash)
  end

  if @array_mode
    key = @path.pop
    eval("#{_current} << _target")
    @path.push(key)
  else
    if _target.is_a?(String)
      eval("#{_current} = _target")
    else
      eval("#{_current} ||= {}")
      eval("#{_current}.merge!(_target)")
    end
  end
end

#serialization_method!Object



11
12
13
# File 'lib/builder/json_format.rb', line 11

def serialization_method!
  :to_json
end

#target!Object



39
40
41
42
43
44
45
# File 'lib/builder/json_format.rb', line 39

def target!
  if @include_root
    @target
  else
    @target[@root]
  end
end

#to_sObject



15
16
17
# File 'lib/builder/json_format.rb', line 15

def to_s
  target!.to_json
end