module Hashe::Mixin

A mixin used for including into a class

@example
  class MyCustomHash
    include Mixin
  end

hash = MyCustomHash.new
hash.foo = 'bar' # 'bar'
hash[:baz] = 'qux' # 'qux'
hash # {foo: 'bar', baz: 'qux'}

Constants

SETTER_PATTERN

Public Class Methods

new(default = {}) click to toggle source

@param default [Hash] default hash

# File lib/hashe/mixin.rb, line 19
def initialize(default = {})
  @data = default
end

Public Instance Methods

[](key) click to toggle source
# File lib/hashe/mixin.rb, line 27
def [](key)
  @data[key.to_sym]
end
[]=(key, value) click to toggle source
# File lib/hashe/mixin.rb, line 31
def []=(key, value)
  @data[key.to_sym] = value
end
each(&blk) click to toggle source
# File lib/hashe/mixin.rb, line 23
def each(&blk)
  @data.each(&blk)
end
inspect() click to toggle source
# File lib/hashe/mixin.rb, line 35
def inspect
  @data.to_s
end
Also aliased as: to_s
method_missing(name, *args, &blk) click to toggle source
Calls superclass method
# File lib/hashe/mixin.rb, line 41
def method_missing(name, *args, &blk)
  return @data.send(name, *args, &blk) if @data.respond_to?(name)
  if SETTER_PATTERN =~ name
    new_attribute(name.to_s.delete('='))
    return send(name, args[0])
  end
  super
end
respond_to_missing?(name, *args) click to toggle source
Calls superclass method
# File lib/hashe/mixin.rb, line 50
def respond_to_missing?(name, *args)
  @data.respond_to?(name) || super
end
to_s()
Alias for: inspect

Private Instance Methods

new_attribute(key) click to toggle source
# File lib/hashe/mixin.rb, line 56
def new_attribute(key)
  key = key.to_sym
  raise 'method already defined' if @data.respond_to?(key)
  setter = "#{key}="
  define_singleton_method(setter) { |v| @data[key] = v }
  define_singleton_method(key) { @data[key] }
end