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 = StackableHash.new
  @array_mode = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



73
74
75
76
77
78
79
# File 'lib/builder/hash_structure.rb', line 73

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

Instance Method Details

#<<(_target) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/builder/hash_structure.rb', line 47

def <<(_target)
  if @array_mode
    @target.current << _target
  elsif _target.is_a?(Hash)
    @target.current.merge!(_target)
  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



42
43
44
45
# File 'lib/builder/hash_structure.rb', line 42

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



37
38
39
40
# File 'lib/builder/hash_structure.rb', line 37

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

#serialization_method!Object



33
34
35
# File 'lib/builder/hash_structure.rb', line 33

def serialization_method!
  :to_hash
end

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



69
70
71
# File 'lib/builder/hash_structure.rb', line 69

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

#target!Object



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

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

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



57
58
59
60
61
62
63
64
65
66
# File 'lib/builder/hash_structure.rb', line 57

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!(StackableHash.new.replace(@default_content_key => text))
  else
    @target.current = text
  end
end