Class: ActiveMocker::Base
- Inherits:
-
Object
- Object
- ActiveMocker::Base
show all
- Extended by:
- Config, Forwardable
- Defined in:
- lib/active_mocker/base.rb
Constant Summary
collapse
- @@_self =
self
Instance Attribute Summary collapse
Attributes included from Config
#clear_cache, #model_attributes, #model_dir, #model_file_reader, #schema_attributes, #schema_file, #schema_file_reader
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Config
active_hash_as_base=, check_required_settings, config, log_level=, mass_assignment=, model_methods=, model_relationships=, reload_default, require_active_hash
Constructor Details
#initialize(model_name) ⇒ Base
Returns a new instance of Base.
18
19
20
21
|
# File 'lib/active_mocker/base.rb', line 18
def initialize(model_name)
@model_name = model_name
active_hash_mock_class
end
|
Instance Attribute Details
#klass ⇒ Object
Returns the value of attribute klass.
16
17
18
|
# File 'lib/active_mocker/base.rb', line 16
def klass
@klass
end
|
#model_name ⇒ Object
Returns the value of attribute model_name.
16
17
18
|
# File 'lib/active_mocker/base.rb', line 16
def model_name
@model_name
end
|
Class Method Details
23
24
25
|
# File 'lib/active_mocker/base.rb', line 23
def self.configure(&block)
config(&block)
end
|
.mock(model_name) ⇒ Object
27
28
29
|
# File 'lib/active_mocker/base.rb', line 27
def self.mock(model_name)
self.send(:new, model_name).klass
end
|
Instance Method Details
#active_hash_mock_class ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/active_mocker/base.rb', line 43
def active_hash_mock_class
fill_templates
add_method_mock_of
if schema_attributes
klass = create_klass
fields = table_definition.column_names
klass.class_eval do
klass.fields(*fields)
end
add_column_names_method
end
if model_attributes
add_class_methods
add_instance_methods
add_single_relationships
add_collections_relationships
end
end
|
#add_class_methods ⇒ Object
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
# File 'lib/active_mocker/base.rb', line 143
def add_class_methods
klass = create_klass
model_definition.class_methods_with_arguments.each do |method|
m = method.keys.first
params = Reparameterize.call(method.values.first)
params_pass = Reparameterize.call(method.values.first, true)
klass.send(:model_class_methods)[m] = eval_lambda(params, %Q[raise "::#{m} is not Implemented for Class: #{klass.name}"])
klass.class_eval " def self.\#{m}(\#{params})\n block = model_class_methods[\#{m.inspect}].to_proc\n instance_exec(*[\#{params_pass}], &block)\n end\n eos\n end\nend\n", __FILE__, __LINE__+1
|
#add_collections_relationships ⇒ Object
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
# File 'lib/active_mocker/base.rb', line 85
def add_collections_relationships
klass = create_klass
model_definition.collections.each do |m|
klass.send(:association_template)[m] = nil
begin
klass.class_eval " def \#{m}\n read_association(\#{m.inspect})\n end\n\n def \#{m}=(value)\n write_association(\#{m.inspect}, CollectionAssociation.new(value))\n end\n eos\n rescue SyntaxError\n Logger_.warn \"ActiveMocker :: Can't create accessor methods for \#{m}.\\n \#{caller}\"\n end\n end\nend\n", __FILE__, __LINE__+1
|
#add_column_names_method ⇒ Object
163
164
165
166
167
168
169
170
171
|
# File 'lib/active_mocker/base.rb', line 163
def add_column_names_method
klass = create_klass
table = table_definition
klass.singleton_class.class_eval do
define_method(:column_names) do
table.column_names
end
end
end
|
#add_instance_methods ⇒ Object
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
# File 'lib/active_mocker/base.rb', line 122
def add_instance_methods
klass = create_klass
model_definition.instance_methods_with_arguments.each do |method|
m = method.keys.first
if m == :attributes
Logger_.warn "ActiveMocker Depends on the #attributes method. It will not be redefined for the mock."
next
end
params = Reparameterize.call(method.values.first)
params_pass = Reparameterize.call(method.values.first, true)
klass.send(:model_methods_template)[m] = eval_lambda(params, %Q[raise "##{m} is not Implemented for Class: #{klass.name}"])
klass.class_eval " def \#{m}(\#{params})\n block = model_instance_methods[\#{m.inspect}].to_proc\n instance_exec(*[\#{params_pass}], &block)\n end\n eos\n end\nend\n", __FILE__, __LINE__+1
|
#add_method_mock_of ⇒ Object
68
69
70
71
72
73
74
75
|
# File 'lib/active_mocker/base.rb', line 68
def add_method_mock_of
klass = create_klass
m_name = model_name
klass.instance_variable_set(:@model_class, model_definition.klass)
klass.instance_eval do
define_method(:mock_of) {m_name}
end
end
|
#add_single_relationships ⇒ Object
77
78
79
80
81
82
83
|
# File 'lib/active_mocker/base.rb', line 77
def add_single_relationships
klass = create_klass
model_definition.single_relationships.each do |m|
klass.send(:association_template)[m] = nil
create_association(m)
end
end
|
#class_exists?(class_name) ⇒ Boolean
190
191
192
193
194
195
|
# File 'lib/active_mocker/base.rb', line 190
def class_exists?(class_name)
klass = Module.const_get(class_name)
return klass.is_a?(Class)
rescue NameError
return false
end
|
#const_class ⇒ Object
177
178
179
180
181
182
183
184
|
# File 'lib/active_mocker/base.rb', line 177
def const_class
remove_const(mock_class_name) if class_exists? mock_class_name
klass = Object.const_set(mock_class_name, Class.new(::ActiveHash::Base))
klass.send(:include, ActiveHash::ARApi)
klass.send(:prepend, ModelInstanceMethods)
klass.extend ModelClassMethods
klass
end
|
#create_association(m) ⇒ Object
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
# File 'lib/active_mocker/base.rb', line 105
def create_association(m)
begin
klass.class_eval " def \#{m}\n read_association(\#{m.inspect})\n end\n\n def \#{m}=(value)\n write_association(\#{m.inspect}, value)\n end\n eos\n rescue SyntaxError\n Logger_.warn \"ActiveMocker :: Can't create accessor methods for \#{m}.\\n \#{caller}\"\n end\nend\n", __FILE__, __LINE__+1
|
#create_klass ⇒ Object
173
174
175
|
# File 'lib/active_mocker/base.rb', line 173
def create_klass
@klass ||= const_class
end
|
#eval_lambda(arguments, block) ⇒ Object
159
160
161
|
# File 'lib/active_mocker/base.rb', line 159
def eval_lambda(arguments, block)
eval(%Q[ ->(#{arguments}){ #{block} }],binding, __FILE__, __LINE__)
end
|
#fill_templates ⇒ Object
62
63
64
65
66
|
# File 'lib/active_mocker/base.rb', line 62
def fill_templates
klass = create_klass
klass.send(:association_names=, model_definition.relationships)
klass.send(:attribute_names=, table_definition.column_names)
end
|
#mock_class_name ⇒ Object
197
198
199
|
# File 'lib/active_mocker/base.rb', line 197
def mock_class_name
"#{model_name}Mock"
end
|
#model_definition ⇒ Object
31
32
33
|
# File 'lib/active_mocker/base.rb', line 31
def model_definition
@model_definition ||= ModelReader.new({model_dir: model_dir, file_reader: model_file_reader}).parse(model_file_name)
end
|
#model_file_name ⇒ Object
35
36
37
|
# File 'lib/active_mocker/base.rb', line 35
def model_file_name
model_name.tableize.singularize
end
|
#remove_const(class_name) ⇒ Object
186
187
188
|
# File 'lib/active_mocker/base.rb', line 186
def remove_const(class_name)
Object.send(:remove_const, class_name)
end
|
#table_definition ⇒ Object
39
40
41
|
# File 'lib/active_mocker/base.rb', line 39
def table_definition
@table_definition ||= SchemaReader.new({schema_file: schema_file, file_reader: schema_file_reader, clear_cache: clear_cache}).search(model_name.tableize)
end
|