Class: Toil::Attributes
- Inherits:
-
Object
show all
- Defined in:
- lib/toil/attributes.rb
Instance Method Summary
collapse
Constructor Details
#initialize(hash = {}, &blk) ⇒ Attributes
Returns a new instance of Attributes.
7
8
9
10
11
12
|
# File 'lib/toil/attributes.rb', line 7
def initialize(hash = {}, &blk)
@attributes = hash.each_with_object({}) do |(k, v), h|
h[k] = v.is_a?(DynamicValue) ? v : DynamicValue.new(v)
end
instance_eval(&blk) if block_given?
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &blk) ⇒ Object
14
15
16
|
# File 'lib/toil/attributes.rb', line 14
def method_missing(m, *args, &blk)
__set__(m, args.first, &blk)
end
|
Instance Method Details
#[](key) ⇒ Object
29
30
31
|
# File 'lib/toil/attributes.rb', line 29
def [](key)
(v = @attributes[key]) && v.call
end
|
#__set__(key, value = nil, &blk) ⇒ Object
33
34
35
|
# File 'lib/toil/attributes.rb', line 33
def __set__(key, value = nil, &blk)
@attributes[key] = DynamicValue.new(value, &blk)
end
|
#dup(&blk) ⇒ Object
18
19
20
|
# File 'lib/toil/attributes.rb', line 18
def dup(&blk)
self.class.new(@attributes, &blk)
end
|
#to_h(overrides = {}) ⇒ Object
Also known as:
to_hash
22
23
24
25
26
|
# File 'lib/toil/attributes.rb', line 22
def to_h(overrides = {})
@attributes.each_with_object({}) do |(k, v), h|
h[k] = v.call unless overrides.key?(k)
end.merge(overrides)
end
|