Class: ActiveMocker::Generate
- Inherits:
-
Object
- Object
- ActiveMocker::Generate
show all
- Extended by:
- Config, Forwardable
- Defined in:
- lib/active_mocker/generate.rb
Defined Under Namespace
Classes: Method, Methods, MockTemplate
Constant Summary
collapse
- @@_self =
self
Instance Attribute Summary
Attributes included from Config
#clear_cache, #log_level, #logger, #migration_dir, #mock_dir, #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
check_required_settings, config, reload_default
Constructor Details
Returns a new instance of Generate.
17
18
19
|
# File 'lib/active_mocker/generate.rb', line 17
def initialize
create_template
end
|
Class Method Details
21
22
23
|
# File 'lib/active_mocker/generate.rb', line 21
def self.configure(&block)
config(&block)
end
|
.load_mock(model_name) ⇒ Object
29
30
31
32
|
# File 'lib/active_mocker/generate.rb', line 29
def self.load_mock(model_name)
load File.join(mock_dir, "#{model_name.tableize.singularize}_mock.rb")
"#{model_name}Mock".constantize
end
|
.mock(model_name, force_reload: false) ⇒ Object
25
26
27
|
# File 'lib/active_mocker/generate.rb', line 25
def self.mock(model_name, force_reload: false)
load_mock(model_name)
end
|
Instance Method Details
#associations(names) ⇒ Object
125
126
127
128
129
130
131
|
# File 'lib/active_mocker/generate.rb', line 125
def associations(names)
hash = {}
names.each do |name|
hash[name] = nil
end
hash
end
|
#class_methods(table_name) ⇒ Object
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
# File 'lib/active_mocker/generate.rb', line 171
def class_methods(table_name)
model_class_methods = []
class_methods = Methods.new
class_methods.methods = model_definition(table_name).class_methods_with_arguments.map do |method|
m = method.keys.first.to_s
params = Reparameterize.call(method.values.first)
params_pass = Reparameterize.call(method.values.first, true)
class_method = Method.new
class_method.method = m
class_method.params = params
class_method.params_pass = params_pass
model_class_methods << m
class_method
end
class_methods.model_class_methods = {}
model_class_methods.each { |meth| class_methods.model_class_methods[meth] = :not_implemented }
class_methods
end
|
#create_template ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/active_mocker/generate.rb', line 56
def create_template
mocks_created = 0
tables.each do |table|
begin
mock_template = MockTemplate.new
mock_template.class_name = mock_class_name(table.name)
mock_template.attribute_names = table.column_names
mock_template.attributes = field_type_to_class(table.fields)
mock_template.default_attributes =default_attr_values(table.fields)
mock_template.single_associations = model_definition(table.name).single_relationships
mock_template.collection_associations = model_definition(table.name).collections
mock_template.association_names = [*model_definition(table.name).single_relationships, *model_definition(table.name).collections]
mock_template.associations = associations(mock_template.association_names)
mock_template.instance_methods = instance_methods(table.name).methods
mock_template.model_instance_methods = instance_methods(table.name).model_instance_methods
mock_template.class_methods = class_methods(table.name).methods
mock_template.model_class_methods = class_methods(table.name).model_class_methods
klass_str = mock_template.render( File.open(File.join(File.expand_path('../', __FILE__), 'mock_template.erb')).read)
FileUtils::mkdir_p mock_dir unless File.directory? mock_dir
File.open(File.join(mock_dir,"#{table.name.singularize}_mock.rb"), 'w').write(klass_str)
logger.info "saving mock #{table_to_model_file(table.name)} to #{mock_dir}"
rescue Exception => exception
logger.debug $!.backtrace
logger.debug exception
logger.info "failed to load #{table_to_model_file(table.name)} model"
next
end
mocks_created += 1
end
logger.info "Generated #{mocks_created} of #{tables.count} mocks"
end
|
#default_attr_values(fields) ⇒ Object
133
134
135
136
137
138
139
140
141
142
143
144
|
# File 'lib/active_mocker/generate.rb', line 133
def default_attr_values(fields)
attributes = {}
fields.each do |f|
if f.options[:default].nil?
attributes[f.name] = nil
else
value = f.default.class == String ? f.default : f.default
attributes[f.name] = value
end
end
attributes
end
|
#field_type_to_class(fields) ⇒ Object
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
# File 'lib/active_mocker/generate.rb', line 96
def field_type_to_class(fields)
fields.map do |field|
field.type = case field.type
when :integer then
Fixnum
when :float then
Float
when :decimal then
BigDecimal
when :timestamp, :time then
Time
when :datetime then
DateTime
when :date then
Date
when :text, :string, :binary then
String
when :boolean then
::Virtus::Attribute::Boolean
end
field
end
end
|
#instance_methods(table_name) ⇒ Object
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
# File 'lib/active_mocker/generate.rb', line 146
def instance_methods(table_name)
model_instance_methods = []
instance_methods = Methods.new
instance_methods.methods = model_definition(table_name).instance_methods_with_arguments.map do |method|
m = method.keys.first.to_s
if m == :attributes
logger.warn "ActiveMocker Depends on the #attributes method. It will not be redefined for the model."
next
end
params = Reparameterize.call(method.values.first)
params_pass = Reparameterize.call(method.values.first, true)
instance_method = Method.new
instance_method.method = m
instance_method.params = params
instance_method.params_pass = params_pass
model_instance_methods << m
instance_method
end
instance_methods.model_instance_methods = {}
model_instance_methods.each{|meth| instance_methods.model_instance_methods[meth] = :not_implemented }
instance_methods
end
|
#mock_class_name(table_name) ⇒ Object
121
122
123
|
# File 'lib/active_mocker/generate.rb', line 121
def mock_class_name(table_name)
"#{table_to_class_name(table_name)}Mock"
end
|
#model_definition(table) ⇒ Object
34
35
36
37
38
|
# File 'lib/active_mocker/generate.rb', line 34
def model_definition(table)
return @model_definition if @model_definition_table == table
@model_definition_table = table
@model_definition = ModelReader.new({model_dir: model_dir, file_reader: model_file_reader}).parse(table_to_model_file(table))
end
|
#schema_reader ⇒ Object
48
49
50
|
# File 'lib/active_mocker/generate.rb', line 48
def schema_reader
SchemaReader.new({schema_file: schema_file, file_reader: schema_file_reader}).search(nil)
end
|
#table_to_class_name(table) ⇒ Object
44
45
46
|
# File 'lib/active_mocker/generate.rb', line 44
def table_to_class_name(table)
table.camelize.singularize
end
|
#table_to_model_file(table) ⇒ Object
40
41
42
|
# File 'lib/active_mocker/generate.rb', line 40
def table_to_model_file(table)
table.singularize
end
|
#tables ⇒ Object
52
53
54
|
# File 'lib/active_mocker/generate.rb', line 52
def tables
schema_reader
end
|