Class: Blackbriar::Node
- Inherits:
-
Object
- Object
- Blackbriar::Node
- Defined in:
- lib/blackbriar/node.rb
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
-
#value_provider ⇒ Object
readonly
Returns the value of attribute value_provider.
Instance Method Summary collapse
-
#initialize(hash, value_provider) ⇒ Node
constructor
A new instance of Node.
- #resolve ⇒ Object
- #resolve_array(array) ⇒ Object
- #resolve_array_item(item) ⇒ Object
- #resolve_hash(hash) ⇒ Object
- #resolve_string(string) ⇒ Object
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
#key ⇒ Object (readonly)
Returns the value of attribute key.
8 9 10 |
# File 'lib/blackbriar/node.rb', line 8 def key @key end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
8 9 10 |
# File 'lib/blackbriar/node.rb', line 8 def value @value end |
#value_provider ⇒ Object (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
#resolve ⇒ Object
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 |