Sha256: 10fdb746c2a8f490db6c0c457a7b04c73900bf555e804ef295b761477baef598

Contents?: true

Size: 1.27 KB

Versions: 7

Compression:

Stored size: 1.27 KB

Contents

# -*- encoding: utf-8 -*-
#
# Author:: Fletcher Nichol (<fnichol@nichol.ca>)
#
# Copyright (C) 2013, Fletcher Nichol
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require 'delegate'

module Kitchen

  # A modifed Hash object that may contain procs as a value which must be
  # executed in the context of another object.
  #
  # @author Fletcher Nichol <fnichol@nichol.ca>
  class LazyHash < SimpleDelegator

    def initialize(obj, context)
      @context = context
      super(obj)
    end

    def [](key)
      proc_or_val = __getobj__[key]

      if proc_or_val.respond_to?(:call)
        proc_or_val.call(@context)
      else
        proc_or_val
      end
    end

    def to_hash
      hash = Hash.new
      __getobj__.keys.each { |key| hash[key] = self[key] }
      hash
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
test-kitchen-1.2.1 lib/kitchen/lazy_hash.rb
test-kitchen-1.2.0 lib/kitchen/lazy_hash.rb
test-kitchen-1.1.1 lib/kitchen/lazy_hash.rb
test-kitchen-1.1.0 lib/kitchen/lazy_hash.rb
test-kitchen-1.0.0 lib/kitchen/lazy_hash.rb
test-kitchen-1.0.0.rc.2 lib/kitchen/lazy_hash.rb
test-kitchen-1.0.0.rc.1 lib/kitchen/lazy_hash.rb