Class: KDirector::Builders::DomBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/k_director/builders/dom_builder.rb

Overview

Dom builder

Direct Known Subclasses

ActionsBuilder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDomBuilder

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

#domObject (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(*keyset_value, default_value: nil) ⇒ Object

add value to array

set(:a, [])
add(:a, 1])
add(:a, 2])
add(:a, 3])

Raises:

  • (ArgumentError)


64
65
66
67
68
69
70
71
72
# File 'lib/k_director/builders/dom_builder.rb', line 64

def add(*keyset_value, default_value: nil)
  size = keyset_value.size

  raise ArgumentError, 'add requires 2 or more arguments' if size < 2

  target = initialize_hierarchy(keyset_value)

  add_kv(target, keyset_value[size - 2], keyset_value[size - 1], default_value: default_value)
end

#buildObject



81
82
83
84
# File 'lib/k_director/builders/dom_builder.rb', line 81

def build
  # hook into the set, add and queue_action methods form memoization
  KUtil.data.to_open_struct(@dom)
end

#debugObject



74
75
76
77
78
79
# File 'lib/k_director/builders/dom_builder.rb', line 74

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:

set_many(: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)
    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)
    end
  end
end

#resetObject



13
14
15
# File 'lib/k_director/builders/dom_builder.rb', line 13

def reset
  @dom = {}
end

#set(*keyset_value, default_value: nil) ⇒ Object

set key_set/value pair, can be used for

  • simple key/value pairs

  • initialize key/array pairs

example:

set(:a, 1)
set(:a, [])
set(:a, [1, 2, 3])
set(:a, some_value, default_value: 'use if not supplied')
set(:a, :b, 'nested value')
set(:a, :b, :c, 'deeply nested value')
set(:a, :b, :c, :d, :e, :f, 'depth is no barrier')

Raises:

  • (ArgumentError)


49
50
51
52
53
54
55
56
57
# File 'lib/k_director/builders/dom_builder.rb', line 49

def set(*keyset_value, default_value: nil)
  size = keyset_value.size

  raise ArgumentError, 'set requires 2 or more arguments' if size < 2

  target = initialize_hierarchy(keyset_value)

  set_kv(target, keyset_value[size - 2], keyset_value[size - 1], default_value: default_value)
end