Class: KDirector::Builders::DomBuilder
- Inherits:
-
Object
- Object
- KDirector::Builders::DomBuilder
- Defined in:
- lib/k_director/builders/dom_builder.rb
Overview
Dom builder
Direct Known Subclasses
Instance Attribute Summary collapse
-
#dom ⇒ Object
readonly
Returns the value of attribute dom.
Instance Method Summary collapse
-
#add(*keys, value: nil, default_value: nil) ⇒ Object
add value to array add(:a, value: 1) add(:a, value: 2) add(:a, value: 3) add(:a, value: 1) add(:a, value: 2) add(:a, value: 3).
- #build ⇒ Object
- #debug ⇒ Object
-
#group_set(group = nil, **opts) ⇒ Object
Set many key/value pairs gainst a group.
-
#initialize ⇒ DomBuilder
constructor
A new instance of DomBuilder.
- #reset ⇒ Object
-
#set(*keys, value: nil, default_value: nil) ⇒ Object
set key_set/value pair, can be used for.
- #to_json(*args) ⇒ Object
Constructor Details
#initialize ⇒ DomBuilder
Returns a new instance of DomBuilder.
9 10 11 |
# File 'lib/k_director/builders/dom_builder.rb', line 9 def initialize reset end |
Instance Attribute Details
#dom ⇒ Object (readonly)
Returns the value of attribute dom.
7 8 9 |
# File 'lib/k_director/builders/dom_builder.rb', line 7 def dom @dom end |
Instance Method Details
#add(*keys, value: nil, default_value: nil) ⇒ Object
add value to array
add(:a, value: 1)
add(:a, value: 2)
add(:a, value: 3)
add(:a, value: {key: 1})
add(:a, value: {key: 2})
add(:a, value: {key: 3})
67 68 69 70 71 72 73 74 75 |
# File 'lib/k_director/builders/dom_builder.rb', line 67 def add(*keys, value: nil, default_value: nil) size = keys.size raise ArgumentError, 'add requires 1 or more keys' if size < 1 target = initialize_hierarchy(keys) add_kv(target, keys[size - 1], value, default_value: default_value) end |
#build ⇒ Object
84 85 86 87 |
# File 'lib/k_director/builders/dom_builder.rb', line 84 def build # hook into the set, add and queue_action methods form memoization KUtil.data.to_open_struct(@dom) end |
#debug ⇒ Object
77 78 79 80 81 82 |
# File 'lib/k_director/builders/dom_builder.rb', line 77 def debug puts JSON.pretty_generate(dom) # log.structure(dom) self end |
#group_set(group = nil, **opts) ⇒ Object
Set many key/value pairs gainst a group
example:
group_set(:github, repo_name: 'repo-name', organization: 'org-name')
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/k_director/builders/dom_builder.rb', line 21 def group_set(group = nil, **opts) return if group.nil? && opts.empty? if group.nil? opts.each do |key, value| set(key, value: value) end else dom[group] = {} if opts.empty? # initialize the group name if no options are provided opts.each do |key, value| set(group, key, value: value) end end end |
#reset ⇒ Object
13 14 15 |
# File 'lib/k_director/builders/dom_builder.rb', line 13 def reset @dom = {} end |
#set(*keys, value: nil, default_value: nil) ⇒ Object
set key_set/value pair, can be used for
-
simple key/value pairs
-
initialize key/array pairs
example:
set(:a, value: 1)
set(:a, value: [])
set(:a, value: [1, 2, 3])
set(:a, value: { a: 1, b: 2 })
set(:a, value: some_value, default_value: 'use if not supplied')
set(:a, value: :b, 'nested value')
set(:a, value: :b, :c, 'deeply nested value')
set(:a, value: :b, :c, :d, :e, :f, 'depth is no barrier')
50 51 52 53 54 55 56 57 58 |
# File 'lib/k_director/builders/dom_builder.rb', line 50 def set(*keys, value: nil, default_value: nil) size = keys.size raise ArgumentError, 'set requires 1 or more keys' if size < 1 target = initialize_hierarchy(keys) set_kv(target, keys[size - 1], value, default_value: default_value) end |
#to_json(*args) ⇒ Object
89 90 91 |
# File 'lib/k_director/builders/dom_builder.rb', line 89 def to_json(*args) JSON.pretty_generate(dom, *args) end |