Class: Builder::HashStructure

Inherits:
Abstract
  • Object
show all
Defined in:
lib/builder/hash_structure.rb

Direct Known Subclasses

JsonFormat

Instance Method Summary collapse

Methods inherited from Abstract

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

Constructor Details

#initialize(options = {}) ⇒ HashStructure

Returns a new instance of HashStructure.



4
5
6
7
8
9
10
11
# File 'lib/builder/hash_structure.rb', line 4

def initialize(options = {})
  # @default_content_key is used in such case: markup.key(value, :attr_key => attr_value)
  # in this case, we need some key for value.
  @default_content_key  = (options[:default_content_key] || :content).to_sym
  @include_root = options[:include_root]
  @target = {}.stackable
  @array_mode = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(key, *args, &block) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/builder/hash_structure.rb', line 79

def method_missing(key, *args, &block)
  key, args = _explore_key_and_args(key, *args, &block)
  _setup_key(key) do
    _set_args(args, &block)
  end
  target!
end

Instance Method Details

#<<(_target) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/builder/hash_structure.rb', line 49

def <<(_target)
  if @array_mode
    @target.current << _target
  elsif _target.is_a?(Hash)
    if @target.current.nil?
      @target.merge!(_target)
    else
      @target.current.merge!(_target)
    end
  else
    @target.current = _target
  end
end

#array_mode(key = nil, &block) ⇒ Object

NOTICE: you have to call this method to use array in json



14
15
16
17
18
19
20
21
22
23
# File 'lib/builder/hash_structure.rb', line 14

def array_mode(key = nil, &block)
  if @target.current.is_a?(Hash) && !@target.current.empty?
    key ||= :entry
    _setup_key(key.to_sym) do
      _array_mode(&block)
    end
  else
    _array_mode(&block)
  end
end

#content!(key, default_content_key, *attrs, &block) ⇒ Object



44
45
46
47
# File 'lib/builder/hash_structure.rb', line 44

def content!(key, default_content_key, *attrs, &block)
  @default_content_key = default_content_key.to_sym
  method_missing(key, *attrs, &block)
end

#root!(key, *attrs, &block) ⇒ Object



39
40
41
42
# File 'lib/builder/hash_structure.rb', line 39

def root!(key, *attrs, &block)
  @include_root = true
  method_missing(key, *attrs, &block)
end

#serialization_method!Object



35
36
37
# File 'lib/builder/hash_structure.rb', line 35

def serialization_method!
  :to_hash
end

#tag!(key, *attrs, &block) ⇒ Object



75
76
77
# File 'lib/builder/hash_structure.rb', line 75

def tag!(key, *attrs, &block)
  method_missing(key, *attrs, &block)
end

#target!Object



25
26
27
28
29
30
31
32
33
# File 'lib/builder/hash_structure.rb', line 25

def target!
  if @include_root || @target.is_a?(Array)
    @target
  elsif @root.nil?
    @target
  else
    @target[@root]
  end
end

#text!(text, default_content_key = nil) ⇒ Object Also known as: cdata!



63
64
65
66
67
68
69
70
71
72
# File 'lib/builder/hash_structure.rb', line 63

def text!(text, default_content_key = nil)
  if @target.current.is_a?(Array)
    @target.current << text
  elsif @target.current.is_a?(Hash) && !@target.current.empty?
    @default_content_key = default_content_key.to_sym unless default_content_key.nil?
    @target.current.merge!(@default_content_key => text)
  else
    @target.current = text
  end
end