Module: Hashe::Mixin
- Includes:
- Enumerable
- Included in:
- Hash
- Defined in:
- lib/hashe/mixin.rb
Overview
A mixin used for including into a class
@example
class MyCustomHash
include Mixin
end
hash = MyCustomHash.new
hash.foo = 'bar' hash[:baz] = 'qux' hash
Constant Summary
collapse
- SETTER_PATTERN =
/^([a-zA-Z_]+|\[\])=$/
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &blk) ⇒ Object
41
42
43
44
45
46
47
48
|
# 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
|
Instance Method Details
#[](key) ⇒ Object
27
28
29
|
# File 'lib/hashe/mixin.rb', line 27
def [](key)
@data[key.to_sym]
end
|
#[]=(key, value) ⇒ Object
31
32
33
|
# File 'lib/hashe/mixin.rb', line 31
def []=(key, value)
@data[key.to_sym] = value
end
|
#each(&blk) ⇒ Object
23
24
25
|
# File 'lib/hashe/mixin.rb', line 23
def each(&blk)
@data.each(&blk)
end
|
#initialize(default = {}) ⇒ Object
19
20
21
|
# File 'lib/hashe/mixin.rb', line 19
def initialize(default = {})
@data = default
end
|
#inspect ⇒ Object
Also known as:
to_s
35
36
37
|
# File 'lib/hashe/mixin.rb', line 35
def inspect
@data.to_s
end
|
#respond_to_missing?(name, *args) ⇒ Boolean
50
51
52
|
# File 'lib/hashe/mixin.rb', line 50
def respond_to_missing?(name, *args)
@data.respond_to?(name) || super
end
|