Sha256: 7d55e4b69bd161e71567e5658ae4b8ad4c632d503f6f87c6cc79259e4f3c1a88

Contents?: true

Size: 764 Bytes

Versions: 7

Compression:

Stored size: 764 Bytes

Contents

module Plate
  # Just a blank class that allows you to call methods that return hash values
  class HashProxy
    # Pass in a hash object to use this class as a proxy for its keys.
    def initialize(source = {})
      @source = source
    end
    
    # Pass through for getting the hash's value at the given key.
    def [](key)
      @source[key]
    end
    
    # Handle method missing calls and proxy methods to hash values.
    #
    # Allows for checking to see if a key exists and returning a key value.
    def method_missing(method, *args)
      if @source.has_key?(method)
        self[method]
      elsif method.to_s =~ /^[a-zA-Z0-9_]*\?$/
        @source.has_key?(method.to_s.gsub!(/\?/, '').to_sym)
      else
        nil
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
plate-0.7.0.pre3 lib/plate/hash_proxy.rb
plate-0.7.0.pre2 lib/plate/hash_proxy.rb
plate-0.7.0.pre lib/plate/hash_proxy.rb
plate-0.6.3 lib/plate/hash_proxy.rb
plate-0.6.2 lib/plate/hash_proxy.rb
plate-0.6.1 lib/plate/hash_proxy.rb
plate-0.6.0 lib/plate/hash_proxy.rb