Sha256: e4e2db66ae4e8626577eb259320466282738adcd36f519ca3ce35326f7160c09

Contents?: true

Size: 1.55 KB

Versions: 86

Compression:

Stored size: 1.55 KB

Contents

require 'itamae-mitsurin'
require 'hashie'
require 'json'
require 'schash'

module ItamaeMitsurin
  class Node
    ValidationError = Class.new(StandardError)

    attr_reader :mash

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

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

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

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

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

    private

    def _reverse_merge(other_hash)
      Hashie::Mash.new(other_hash).merge(@mash)
    end

    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

    def respond_to?(method, priv = false)
      @mash.respond_to?(method, priv) || super
    end

    def fetch_inventory_value(key)
      value = @backend.host_inventory[key]
      if value.is_a?(Hash)
        value = Hashie::Mash.new(value)
      end

      value
    rescue NotImplementedError, NameError
      nil
    end
  end
end

Version data entries

86 entries across 47 versions & 1 rubygems

Version Path
itamae-mitsurin-1.0.3 lib/itamae-mitsurin/node.rb
itamae-mitsurin-1.0.2 lib/itamae-mitsurin/node.rb
itamae-mitsurin-1.0.1 lib/itamae-mitsurin/node.rb
itamae-mitsurin-1.0.0 lib/itamae-mitsurin/node.rb
itamae-mitsurin-0.50 test/test_itamae-mitsurin/test_node.rb
itamae-mitsurin-0.49 test/test_itamae-mitsurin/test_node.rb
itamae-mitsurin-0.48 test/test_itamae-mitsurin/test_node.rb
itamae-mitsurin-0.47 test/test_itamae-mitsurin/test_node.rb
itamae-mitsurin-0.46 lib/itamae-mitsurin/node.rb
itamae-mitsurin-0.46 test/test_itamae-mitsurin/test_node.rb
itamae-mitsurin-0.45 lib/itamae-mitsurin/node.rb
itamae-mitsurin-0.45 test/test_itamae-mitsurin/test_node.rb
itamae-mitsurin-0.44 lib/itamae-mitsurin/node.rb
itamae-mitsurin-0.44 test/test_itamae-mitsurin/test_node.rb
itamae-mitsurin-0.43 lib/itamae-mitsurin/node.rb
itamae-mitsurin-0.43 test/test_itamae-mitsurin/test_node.rb
itamae-mitsurin-0.42 lib/itamae-mitsurin/node.rb
itamae-mitsurin-0.42 test/test_itamae-mitsurin/test_node.rb
itamae-mitsurin-0.41 lib/itamae-mitsurin/node.rb
itamae-mitsurin-0.41 test/test_itamae-mitsurin/test_node.rb