Class: Contextuable
- Inherits:
-
Object
show all
- Defined in:
- lib/contextuable.rb,
lib/contextuable/class_methods.rb,
lib/contextuable/instance_methods.rb
Defined Under Namespace
Modules: ClassMethods, InstanceMethods
Classes: PresenceRequired, RequiredFieldNotPresent, WrongArgument
Constant Summary
collapse
- VERSION =
"0.4.0"
Instance Attribute Summary collapse
-
#attrs ⇒ Object
(also: #to_h, #to_hash)
readonly
Returns the value of attribute attrs.
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(hash = {}) ⇒ Contextuable
19
20
21
22
23
24
25
26
27
|
# File 'lib/contextuable.rb', line 19
def initialize(hash = {})
unless hash.class <= Hash
fail WrongArgument, "[Contextuable ERROR]: `#{self.class}` expects to receive a `Hash` or and object having `Hash` as ancestor."
end
@attrs = hash
@attrs.each do |k, v|
define_contextuable_method(k, v)
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
37
38
39
40
41
42
43
44
45
|
# File 'lib/contextuable.rb', line 37
def method_missing(name, *args, &block)
if name =~ /\A\w+=\z/
set_attribute_macro(name, *args, &block)
elsif out = provided_macro(name)
out[:out]
else
super
end
end
|
Instance Attribute Details
#attrs ⇒ Object
Also known as:
to_h, to_hash
Returns the value of attribute attrs.
15
16
17
|
# File 'lib/contextuable.rb', line 15
def attrs
@attrs
end
|
Class Method Details
.inherited(subclass) ⇒ Object
10
11
12
13
|
# File 'lib/contextuable.rb', line 10
def self.inherited(subclass)
subclass.extend ClassMethods
subclass.include InstanceMethods
end
|
Instance Method Details
#[](key) ⇒ Object
29
30
31
|
# File 'lib/contextuable.rb', line 29
def [](key)
attrs[key]
end
|
#[]=(key, value) ⇒ Object
33
34
35
|
# File 'lib/contextuable.rb', line 33
def []=(key, value)
set_attribute(key, value)
end
|