Class: Blackbriar::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/blackbriar/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash, value_provider) ⇒ Node

Returns a new instance of Node.



3
4
5
6
# File 'lib/blackbriar/node.rb', line 3

def initialize(hash, value_provider)
  @key, @value = hash.to_a.first
  @value_provider = value_provider
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



8
9
10
# File 'lib/blackbriar/node.rb', line 8

def key
  @key
end

#valueObject (readonly)

Returns the value of attribute value.



8
9
10
# File 'lib/blackbriar/node.rb', line 8

def value
  @value
end

#value_providerObject (readonly)

Returns the value of attribute value_provider.



8
9
10
# File 'lib/blackbriar/node.rb', line 8

def value_provider
  @value_provider
end

Instance Method Details

#resolveObject



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/blackbriar/node.rb', line 10

def resolve
  if value.is_a? String
    {key => resolve_string(value)}
  elsif value.is_a? Numeric
    {key => value}
  elsif value.is_a? Time
    {key => value}
  elsif value.is_a? Hash
    {key => resolve_hash(value)}
  elsif value.is_a? Array
    {key => resolve_array(value)}
  end
end

#resolve_array(array) ⇒ Object



46
47
48
# File 'lib/blackbriar/node.rb', line 46

def resolve_array(array)
  array.map {|item| resolve_array_item(item)}
end

#resolve_array_item(item) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/blackbriar/node.rb', line 24

def resolve_array_item(item)
  if item.is_a? String
    resolve_string(item)
  elsif value.is_a? Numeric
    {key => value}
  elsif value.is_a? Time
    {key => value}
  elsif item.is_a? Hash
    resolve_hash item
  elsif item.is_a? Array
    resolve_array(item)
  end
end

#resolve_hash(hash) ⇒ Object



42
43
44
# File 'lib/blackbriar/node.rb', line 42

def resolve_hash(hash)
  hash.to_a.map {|item| self.class.new([item].to_h, value_provider).resolve}.reduce(&:merge)
end

#resolve_string(string) ⇒ Object



38
39
40
# File 'lib/blackbriar/node.rb', line 38

def resolve_string(string)
  value_provider.resolve(string)
end