Class: Fabrication::Generator::Base
- Inherits:
-
Object
- Object
- Fabrication::Generator::Base
show all
- Defined in:
- lib/fabrication/generator/base.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(resolved_class) ⇒ Base
Returns a new instance of Base.
77
78
79
|
# File 'lib/fabrication/generator/base.rb', line 77
def initialize(resolved_class)
self.resolved_class = resolved_class
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
85
86
87
|
# File 'lib/fabrication/generator/base.rb', line 85
def method_missing(method_name, *args, &block)
_attributes.fetch(method_name) { super }
end
|
Class Method Details
.supports?(_resolved_class) ⇒ Boolean
4
5
6
|
# File 'lib/fabrication/generator/base.rb', line 4
def self.supports?(_resolved_class)
true
end
|
Instance Method Details
#_klass ⇒ Object
89
90
91
92
93
94
95
|
# File 'lib/fabrication/generator/base.rb', line 89
def _klass
Fabrication::Support.log_deprecation(
'The `_klass` method in fabricator definitions has been replaced by `resolved_class`'
)
resolved_class
end
|
#build(attributes = [], callbacks = {}) ⇒ Object
8
9
10
11
12
13
14
15
16
17
18
19
20
|
# File 'lib/fabrication/generator/base.rb', line 8
def build(attributes = [], callbacks = {})
process_attributes(attributes)
if callbacks[:initialize_with]
build_instance_with_constructor_override(callbacks[:initialize_with])
elsif callbacks[:on_init]
build_instance_with_init_callback(callbacks[:on_init])
else
build_instance
end
execute_callbacks(callbacks[:after_build])
_instance
end
|
#build_instance ⇒ Object
66
67
68
69
|
# File 'lib/fabrication/generator/base.rb', line 66
def build_instance
self._instance = resolved_class.new
set_attributes
end
|
#build_instance_with_constructor_override(callback) ⇒ Object
56
57
58
59
|
# File 'lib/fabrication/generator/base.rb', line 56
def build_instance_with_constructor_override(callback)
self._instance = instance_exec(_transient_attributes, &callback)
set_attributes
end
|
#build_instance_with_init_callback(callback) ⇒ Object
61
62
63
64
|
# File 'lib/fabrication/generator/base.rb', line 61
def build_instance_with_init_callback(callback)
self._instance = resolved_class.new(*callback.call(_transient_attributes))
set_attributes
end
|
#create(attributes = [], callbacks = []) ⇒ Object
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/fabrication/generator/base.rb', line 22
def create(attributes = [], callbacks = [])
build(attributes, callbacks)
execute_callbacks(callbacks[:before_validation])
execute_callbacks(callbacks[:after_validation])
execute_callbacks(callbacks[:before_save])
execute_callbacks(callbacks[:before_create])
persist
execute_callbacks(callbacks[:after_create])
execute_callbacks(callbacks[:after_save])
_instance
end
|
#execute_callbacks(callbacks) ⇒ Object
34
35
36
|
# File 'lib/fabrication/generator/base.rb', line 34
def execute_callbacks(callbacks)
callbacks&.each { |callback| _instance.instance_exec(_instance, _transient_attributes, &callback) }
end
|
#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean
81
82
83
|
# File 'lib/fabrication/generator/base.rb', line 81
def respond_to_missing?(method_name, _include_private = false)
_attributes.key?(method_name)
end
|
#set_attributes ⇒ Object
71
72
73
74
75
|
# File 'lib/fabrication/generator/base.rb', line 71
def set_attributes
_attributes.each do |k, v|
_instance.send("#{k}=", v)
end
end
|
#to_hash(attributes = [], _callbacks = []) ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/fabrication/generator/base.rb', line 43
def to_hash(attributes = [], _callbacks = [])
process_attributes(attributes)
Fabrication::Support.hash_class.new.tap do |hash|
_attributes.map do |name, value|
if value.respond_to?(:id)
hash["#{name}_id"] = value.id
else
hash[name] = value
end
end
end
end
|
#to_params(attributes = []) ⇒ Object
38
39
40
41
|
# File 'lib/fabrication/generator/base.rb', line 38
def to_params(attributes = [])
process_attributes(attributes)
_attributes.respond_to?(:with_indifferent_access) ? _attributes.with_indifferent_access : _attributes
end
|