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.



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

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



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

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



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

def serialization_method!
  :to_json
end

#target!Object



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

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

#to_sObject



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

def to_s
  target!.to_json
end