Sha256: 60f6299ea2f2244be33b8fcf34d0480d08d48e03426511ff36c09e979bc42f9a

Contents?: true

Size: 524 Bytes

Versions: 6

Compression:

Stored size: 524 Bytes

Contents

module Rpub
  # Delegate missing methods to keys in a Hash atribute on the current object.
  module HashDelegation
    def self.included(base)
      base.extend ClassMethods
    end

    module ClassMethods
      def delegate_to_hash(attr)
        define_method(:delegated_hash) { send attr }
      end
    end

    def respond_to?(m)
      super || delegated_hash.has_key?(m.to_s)
    end

    def method_missing(m, *args, &block)
      return super unless respond_to? m
      delegated_hash.fetch m.to_s
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rpub-0.5.0 lib/rpub/hash_delegation.rb
rpub-0.4.0 lib/rpub/hash_delegation.rb
rpub-0.3.0 lib/rpub/hash_delegation.rb
rpub-0.2.1 lib/rpub/hash_delegation.rb
rpub-0.2.0 lib/rpub/hash_delegation.rb
rpub-0.1.0 lib/rpub/hash_delegation.rb