Module: Contextuable::InstanceMethods
- Defined in:
- lib/contextuable/instance_methods.rb
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/contextuable/instance_methods.rb', line 12
def method_missing(name, *args, &block)
if ary = find_in_equivalents(name)
_from_equivalents(ary)
elsif name =~ /\A\w+=\z/
set_attribute_macro(name, *args, &block)
else
out = provided_macro(name)
return out[:out] if out
if _no_method_error
super
end
end
end
|
Instance Method Details
#_defaults ⇒ Object
65
66
67
|
# File 'lib/contextuable/instance_methods.rb', line 65
def _defaults
_get_config[:defaults] || {}
end
|
#_equivalents ⇒ Object
53
54
55
|
# File 'lib/contextuable/instance_methods.rb', line 53
def _equivalents
_get_config[:equivalents] || []
end
|
#_from_equivalents(ary) ⇒ Object
36
37
38
39
40
41
42
43
|
# File 'lib/contextuable/instance_methods.rb', line 36
def _from_equivalents(ary)
out = nil
ary.each do |method|
out = attrs[method.to_sym]
break if out
end
out
end
|
#_get_config ⇒ Object
73
74
75
|
# File 'lib/contextuable/instance_methods.rb', line 73
def _get_config
self.class.settings
end
|
#_no_method_error ⇒ Object
61
62
63
|
# File 'lib/contextuable/instance_methods.rb', line 61
def _no_method_error
_get_config[:no_method_error].nil? ? true : _get_config[:no_method_error]
end
|
#_only_permitted? ⇒ Boolean
45
46
47
|
# File 'lib/contextuable/instance_methods.rb', line 45
def _only_permitted?
_permitted.any?
end
|
#_permitted ⇒ Object
49
50
51
|
# File 'lib/contextuable/instance_methods.rb', line 49
def _permitted
_get_config[:permitted] || []
end
|
#_presence_required ⇒ Object
57
58
59
|
# File 'lib/contextuable/instance_methods.rb', line 57
def _presence_required
_get_config[:presence_required] || []
end
|
#_required_args ⇒ Object
69
70
71
|
# File 'lib/contextuable/instance_methods.rb', line 69
def _required_args
_get_config[:required] || []
end
|
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/contextuable/instance_methods.rb', line 77
def check_input_errors(hash)
unless hash.class <= Hash
fail WrongArgument, "[Contextuable ERROR]: `#{self.class}` expects to receive a `Hash` or and object having `Hash` as ancestor."
end
_required_args.map(&:to_sym).each do |r|
unless hash.keys.map(&:to_sym).include?(r)
fail RequiredFieldNotPresent, "[Contextuable ERROR]: `#{self.class}` expect to be initialized with `#{r}` as an attribute."
end
end
_presence_required.map(&:to_sym).each do |r|
if hash[r].nil?
fail PresenceRequired, "[Contextuable ERROR]: `#{self.class}` expects to receive an attribute named `#{r}` not beeing `nil`"
end
end
end
|
#find_in_equivalents(name) ⇒ Object
27
28
29
30
31
32
33
34
|
# File 'lib/contextuable/instance_methods.rb', line 27
def find_in_equivalents(name)
found = nil
_equivalents.each do |ary|
found = ary if ary.include?(name.to_sym)
break if found
end
found
end
|
#initialize(hash = {}) ⇒ Object
3
4
5
6
7
8
9
10
|
# File 'lib/contextuable/instance_methods.rb', line 3
def initialize(hash = {})
check_input_errors(hash)
hash = hash.select{|k, v| _permitted.include?(k.to_sym) } if _only_permitted?
@attrs = _defaults.merge(hash)
attrs.each do |k, v|
define_contextuable_method(k, v)
end
end
|