Class: Itamae::Node

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

Constant Summary collapse

ValidationError =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash, backend) ⇒ Node

Returns a new instance of Node.



12
13
14
15
# File 'lib/itamae/node.rb', line 12

def initialize(hash, backend)
  @mash = Itamae::Mash.new(hash)
  @backend = backend
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object (private)



49
50
51
52
53
54
55
56
57
# File 'lib/itamae/node.rb', line 49

def method_missing(method, *args)
  if @mash.respond_to?(method)
    return @mash.public_send(method, *args)
  elsif args.empty? && value = fetch_inventory_value(method)
    return value
  end

  super
end

Instance Attribute Details

#mashObject (readonly)

Returns the value of attribute mash.



10
11
12
# File 'lib/itamae/node.rb', line 10

def mash
  @mash
end

Instance Method Details

#[](key) ⇒ Object



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

def [](key)
  if @mash.has_key?(key)
    @mash[key]
  else
    fetch_inventory_value(key)
  end
end

#reverse_merge(other_hash) ⇒ Object



17
18
19
# File 'lib/itamae/node.rb', line 17

def reverse_merge(other_hash)
  self.class.new(_reverse_merge(other_hash), @backend)
end

#reverse_merge!(other_hash) ⇒ Object



21
22
23
# File 'lib/itamae/node.rb', line 21

def reverse_merge!(other_hash)
  @mash.replace(_reverse_merge(other_hash))
end

#validate!(&block) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/itamae/node.rb', line 33

def validate!(&block)
  errors = Schash::Validator.new(&block).validate(@mash)
  unless errors.empty?
    errors.each do |error|
      Itamae.logger.error "'#{error.position.join('->')}' #{error.message}"
    end
    raise ValidationError
  end
end