Sha256: 37a7caab7fd60be8d506cc74aba26c464a4b9b38709d11223c962a6d067104a4

Contents?: true

Size: 1.42 KB

Versions: 17

Compression:

Stored size: 1.42 KB

Contents

require 'itamae'
require 'hashie'
require 'json'
require 'schash'

module Itamae
  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|
          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)
      @backend.host_inventory[key]
    rescue NotImplementedError, NameError
      nil
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
itamae-1.2.19 lib/itamae/node.rb
itamae-1.2.18 lib/itamae/node.rb
itamae-1.2.17 lib/itamae/node.rb
itamae-1.2.16 lib/itamae/node.rb
itamae-1.2.15 lib/itamae/node.rb
itamae-1.2.14 lib/itamae/node.rb
itamae-1.2.13 lib/itamae/node.rb
itamae-1.2.12 lib/itamae/node.rb
itamae-1.2.11 lib/itamae/node.rb
itamae-1.2.10 lib/itamae/node.rb
itamae-1.2.9 lib/itamae/node.rb
itamae-1.2.8 lib/itamae/node.rb
itamae-1.2.7 lib/itamae/node.rb
itamae-1.2.6 lib/itamae/node.rb
itamae-1.2.5 lib/itamae/node.rb
itamae-1.2.4 lib/itamae/node.rb
itamae-1.2.3 lib/itamae/node.rb