Class: ActiveInteractor::Context::Base
Overview
The base class for all context objects
Constant Summary
Type::DeclerationMethods::Boolean
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attributes = {}) ⇒ Base
Returns a new instance of Base.
23
24
25
26
27
28
29
|
# File 'lib/active_interactor/context/base.rb', line 23
def initialize(attributes = {})
attribute_set.attributes.each do |attribute|
next unless attributes.with_indifferent_access.key?(attribute.name)
assign_attribute_value(attribute.name, attributes.with_indifferent_access[attribute.name])
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *arguments) ⇒ Object
61
62
63
64
65
66
|
# File 'lib/active_interactor/context/base.rb', line 61
def method_missing(method_name, *arguments)
return super unless respond_to_missing?(method_name)
return assignment_method_missing(method_name, *arguments) if method_name.to_s.end_with?('=')
read_attribute_value(method_name)
end
|
Class Method Details
.attribute_set ⇒ Object
18
19
20
|
# File 'lib/active_interactor/context/base.rb', line 18
def attribute_set
@attribute_set ||= AttributeSet.new(self)
end
|
.method_defined?(method_name) ⇒ Boolean
12
13
14
|
# File 'lib/active_interactor/context/base.rb', line 12
def method_defined?(method_name)
attribute_set.attribute_names.include?(method_name.to_s.delete('=').to_sym) || super
end
|
Instance Method Details
#[](attribute_name) ⇒ Object
31
32
33
|
# File 'lib/active_interactor/context/base.rb', line 31
def [](attribute_name)
read_attribute_value(attribute_name)
end
|
#[]=(attribute_name, value) ⇒ Object
35
36
37
|
# File 'lib/active_interactor/context/base.rb', line 35
def []=(attribute_name, value)
assign_attribute_value(attribute_name, value)
end
|
#assign_attribute_value(attribute_name, value) ⇒ Object
41
42
43
44
45
46
|
# File 'lib/active_interactor/context/base.rb', line 41
def assign_attribute_value(attribute_name, value)
attribute = attribute_set.find(attribute_name)
raise NoMethodError, "unknown attribute '#{attribute_name}' for #{self.class.name}" unless attribute
attribute.assign_value(value)
end
|
#assignment_method_missing(method_name, *arguments) ⇒ Object
48
49
50
51
52
53
54
55
|
# File 'lib/active_interactor/context/base.rb', line 48
def assignment_method_missing(method_name, *arguments)
if arguments.length != 1
raise ArgumentError,
"wrong number of arguments (given #{arguments.length}, expected 1)"
end
assign_attribute_value(method_name.to_s.delete('=').to_sym, arguments.first)
end
|
#attribute_set ⇒ Object
57
58
59
|
# File 'lib/active_interactor/context/base.rb', line 57
def attribute_set
@attribute_set ||= AttributeSet.new(self, *self.class.send(:attribute_set).attributes.map(&:dup))
end
|
#read_attribute_value(attribute_name) ⇒ Object
68
69
70
71
72
73
|
# File 'lib/active_interactor/context/base.rb', line 68
def read_attribute_value(attribute_name)
attribute = attribute_set.find(attribute_name)
raise NoMethodError, "unknown attribute '#{attribute_name}' for #{self.class.name}" unless attribute
attribute.value
end
|
#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean
75
76
77
78
79
80
|
# File 'lib/active_interactor/context/base.rb', line 75
def respond_to_missing?(method_name, _include_private = false)
return true if attribute_set.attribute_names.include?(method_name.to_sym)
return true if attribute_set.attribute_names.include?(method_name.to_s.delete('=').to_sym)
super
end
|