Sha256: 269c0c35c5ca4b222dfab1ea79c757f031b672930b7c22b29c9a341a6889117e

Contents?: true

Size: 777 Bytes

Versions: 11

Compression:

Stored size: 777 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
      @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

11 entries across 11 versions & 1 rubygems

Version Path
plate-0.7.8 lib/plate/hash_proxy.rb
plate-0.7.7 lib/plate/hash_proxy.rb
plate-0.7.6 lib/plate/hash_proxy.rb
plate-0.7.5 lib/plate/hash_proxy.rb
plate-0.7.4 lib/plate/hash_proxy.rb
plate-0.7.3 lib/plate/hash_proxy.rb
plate-0.7.2 lib/plate/hash_proxy.rb
plate-0.7.1 lib/plate/hash_proxy.rb
plate-0.7.0 lib/plate/hash_proxy.rb
plate-0.7.0.pre5 lib/plate/hash_proxy.rb
plate-0.7.0.pre4 lib/plate/hash_proxy.rb