active_mocker 1.8.4 → 2.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +13 -0
  3. data/README.md +4 -2
  4. data/lib/active_mocker.rb +9 -25
  5. data/lib/active_mocker/config.rb +26 -46
  6. data/lib/active_mocker/generate.rb +115 -110
  7. data/lib/active_mocker/loaded_mocks.rb +76 -65
  8. data/lib/active_mocker/mock/base.rb +283 -287
  9. data/lib/active_mocker/mock/has_many.rb +2 -0
  10. data/lib/active_mocker/mock_creator.rb +262 -0
  11. data/lib/active_mocker/mock_template.erb +9 -186
  12. data/lib/active_mocker/mock_template/_associations.erb +82 -0
  13. data/lib/active_mocker/mock_template/_attributes.erb +11 -0
  14. data/lib/active_mocker/mock_template/_class_methods.erb +41 -0
  15. data/lib/active_mocker/mock_template/_defined_methods.erb +10 -0
  16. data/lib/active_mocker/mock_template/_modules_constants.erb +10 -0
  17. data/lib/active_mocker/mock_template/_scopes.erb +23 -0
  18. data/lib/active_mocker/null_progress.rb +9 -0
  19. data/lib/active_mocker/output_capture.rb +32 -0
  20. data/lib/active_mocker/parent_class.rb +64 -0
  21. data/lib/active_mocker/progress.rb +13 -0
  22. data/lib/active_mocker/public_methods.rb +15 -23
  23. data/lib/active_mocker/rspec.rb +16 -0
  24. data/lib/active_mocker/rspec_helper.rb +10 -8
  25. data/lib/active_mocker/task.rake +6 -1
  26. data/lib/active_mocker/template_creator.rb +22 -0
  27. data/lib/active_mocker/version.rb +1 -1
  28. metadata +43 -103
  29. data/lib/active_mocker/active_record.rb +0 -74
  30. data/lib/active_mocker/active_record/field.rb +0 -39
  31. data/lib/active_mocker/active_record/relationships.rb +0 -110
  32. data/lib/active_mocker/active_record/schema.rb +0 -81
  33. data/lib/active_mocker/active_record/scope.rb +0 -22
  34. data/lib/active_mocker/active_record/table.rb +0 -26
  35. data/lib/active_mocker/active_record/unknown_class_method.rb +0 -17
  36. data/lib/active_mocker/active_record/unknown_module.rb +0 -30
  37. data/lib/active_mocker/db_to_ruby_type.rb +0 -29
  38. data/lib/active_mocker/file_reader.rb +0 -11
  39. data/lib/active_mocker/model_reader.rb +0 -191
  40. data/lib/active_mocker/model_schema.rb +0 -285
  41. data/lib/active_mocker/model_schema/assemble.rb +0 -220
  42. data/lib/active_mocker/reparameterize.rb +0 -41
  43. data/lib/active_mocker/ruby_parse.rb +0 -68
  44. data/lib/active_mocker/schema_reader.rb +0 -30
  45. data/lib/active_mocker/string_reader.rb +0 -18
@@ -1,220 +0,0 @@
1
- module ActiveMocker
2
-
3
- module ModelLoadError
4
-
5
- class General < Exception
6
-
7
- def initialize(msg)
8
- @msg = msg
9
- super
10
- end
11
-
12
- def class_name
13
- @msg
14
- end
15
-
16
- end
17
-
18
- class HasNoParentClass < General
19
- end
20
-
21
- class LoadingModelInRails < General
22
-
23
- def initialize(error, class_name)
24
- @msg = "#{class_name}\n #{error.message}\n #{error.backtrace.join("\n ")}"
25
- super(@msg)
26
- end
27
-
28
- end
29
- end
30
-
31
- class ModelSchema
32
-
33
- class Assemble
34
-
35
- attr_reader :progress
36
-
37
- def initialize(progress: nil)
38
- @progress = progress
39
- end
40
-
41
- def increment_progress
42
- progress.increment unless progress.nil?
43
- end
44
-
45
- def tables
46
- @tables ||= SchemaReader.new.tables
47
- end
48
-
49
- def get_table(model, model_name)
50
- if model.parent_class.present?
51
- model = get_model(model_name)
52
- end
53
- selected_table = tables.select{|table| table.name == model.table_name}.first
54
- if selected_table.nil?
55
- Logger.info "Table: `#{model.table_name}`, can not be found for model #{model_name.camelize}.\n"
56
- end
57
- selected_table
58
- end
59
-
60
- def run
61
- model_schemas = models.map do |model_name|
62
- begin
63
- model = get_model(model_name)
64
-
65
- table = get_table(model, model_name)
66
- attributes = []
67
- attributes = build_attributes(table.fields, primary_key(table.fields, model)) unless table.nil?
68
-
69
- increment_progress
70
-
71
- ModelSchema.new(class_name: -> { model_name.camelize },
72
- table_name: -> { model.try(:table_name) },
73
- attributes: -> { attributes },
74
- _methods: -> { build_methods(model) },
75
- relationships: -> { build_relationships(model) },
76
- constants: -> { model.constants },
77
- modules: -> { model.modules },
78
- parent_class: -> { model.parent_class },
79
- abstract_class: -> { model.abstract_class}
80
- )
81
- rescue Exception => e
82
- e
83
- end
84
- end
85
- ModelSchemaCollection.new(model_schemas.compact)
86
- end
87
-
88
- def build_attributes(attributes, primary_attribute)
89
- attributes.map do |attr|
90
- attribute = ModelSchema::Attributes
91
- .new(name: attr.name,
92
- type: attr.type,
93
- precision: attr.precision,
94
- scale: attr.scale,
95
- default_value: attr.default)
96
- if primary_attribute == attr
97
- attribute.primary_key = true
98
- end
99
- attribute
100
- end
101
- end
102
-
103
- def build_methods(model)
104
- result = []
105
- {scope: model.scopes_with_arguments,
106
- instance: model.instance_methods_with_arguments,
107
- class: model.class_methods_with_arguments}.each do |type,methods|
108
- methods.map do |method|
109
- method_name = method.keys.first.to_s
110
- arguments = method.values.first
111
-
112
- result.push(ModelSchema::Methods.new(name: method_name,
113
- arguments: arguments,
114
- type: type,
115
- proc: method[:proc]))
116
- end
117
- end
118
- result
119
- end
120
-
121
- def build_relationships(model)
122
- relations_by_type(model).map do |type, relations|
123
- relations.map do |relation|
124
- Relationships.new(name: relation.name,
125
- class_name: relation.class_name,
126
- type: type,
127
- through: nil,
128
- source: nil,
129
- foreign_key: relation.foreign_key,
130
- join_table: nil)
131
- end
132
- end.flatten
133
- end
134
-
135
- def relations_by_type(model)
136
- {belongs_to: model.belongs_to,
137
- has_one: model.has_one,
138
- has_many: model.has_many,
139
- has_and_belongs_to_many: model.has_and_belongs_to_many
140
- }
141
- end
142
-
143
- def find_join_table(relation, model)
144
- return relation.join_table if relation.respond_to?(:join_table) && relation.join_table
145
- tables.select do |table|
146
-
147
- "#{model.table_name}_#{relation.name}" == table.name.to_s || "#{relation.name}_#{model.table_name}" == table.name.to_s
148
- end.first
149
- end
150
-
151
- def primary_key(attributes, model)
152
- result = model_primary_key_attribute(attributes, model)
153
- return result unless result.nil?
154
-
155
- result = find_primary_key(attributes)
156
- return result unless result.nil?
157
-
158
- find_id_attribute(attributes)
159
- end
160
-
161
- private
162
-
163
- def model_primary_key_attribute(attributes, model)
164
- if model.respond_to?(:primary_key) && model.primary_key
165
- attributes.select do |attr|
166
- model.primary_key.to_s == attr.name.to_s
167
- end.first
168
- end
169
- end
170
-
171
- def find_primary_key(attributes)
172
- attributes.select do |attr|
173
- attr.try(:primary_key)
174
- end.first
175
- end
176
-
177
- def find_id_attribute(attributes)
178
- attributes.select do |attr|
179
- attr.name.to_sym == :id
180
- end.first
181
- end
182
-
183
- public
184
-
185
- def models
186
- models = '*'
187
- models = Config.generate_for_mock.underscore if Config.generate_for_mock
188
- Dir["#{Config.model_dir}/#{models}.rb"].map do |file|
189
- Pathname.new(file).basename.to_s.sub('.rb', '')
190
- end
191
- end
192
-
193
- def get_model(model_file_name)
194
- model = ModelReader.new.parse(model_file_name)
195
- raise ModelLoadError::General.new(model_file_name) unless model
196
- model
197
- end
198
-
199
- def get_table_name(model_table_name, model_name)
200
- return model_name.tableize if model_table_name.nil?
201
- return model_table_name
202
- end
203
-
204
- def table_to_model_file(table_name)
205
- table_name.singularize
206
- end
207
-
208
- def table_to_class_name(table)
209
- table.camelize.singularize
210
- end
211
-
212
- def get_belongs_to(class_name, foreign_key)
213
- get_table(nil, class_name)
214
- end
215
-
216
- end
217
-
218
- end
219
-
220
- end
@@ -1,41 +0,0 @@
1
- module ActiveMocker
2
- # @api private
3
- class Reparameterize
4
-
5
- def self.call(params, method_parameters: nil)
6
- return method_parameters(params) unless method_parameters.nil?
7
- method_arguments(params)
8
- end
9
-
10
- # Method arguments are the real values passed to (and received by) the function.
11
- def self.method_arguments(params)
12
- params.map do |state, param|
13
- case state
14
- when :req
15
- param
16
- when :rest
17
- "*#{param}"
18
- when :opt
19
- "#{param}=nil"
20
- when :keyreq
21
- "#{param}:"
22
- when :key
23
- "#{param}: nil"
24
- end
25
- end.join(', ')
26
- end
27
-
28
- # Method parameters are the names listed in the function definition.
29
- def self.method_parameters(params)
30
- params.map do |state, param|
31
- case state
32
- when :key, :keyreq
33
- "#{param}: #{param}"
34
- else
35
- param
36
- end
37
- end.join(', ')
38
- end
39
-
40
- end
41
- end
@@ -1,68 +0,0 @@
1
- module ActiveMocker
2
-
3
- class RubyParse
4
-
5
- attr_reader :source
6
-
7
- def initialize(source)
8
- @source = source
9
- end
10
-
11
- def is_class?
12
- ast.type == :class
13
- end
14
-
15
- def class_name
16
- Unparser.unparse(find_class.to_a[0])
17
- end
18
-
19
- def parent_class_name
20
- Unparser.unparse(find_class.to_a[1])
21
- end
22
-
23
- def has_parent_class?
24
- return false if find_class.nil?
25
- find_class.to_a[1].try(:type) == :const
26
- end
27
-
28
- def change_class_name(class_name)
29
- reset_nodes
30
- nodes[0] = Parser::CurrentRuby.parse(class_name)
31
- new_ast = ast.updated(nil, nodes, nil)
32
- Unparser.unparse(new_ast)
33
- end
34
-
35
- def modify_parent_class(parent_class)
36
- reset_nodes
37
- if has_parent_class?
38
- class_node = find_class.to_a.dup
39
- class_node[1] = Parser::CurrentRuby.parse(parent_class)
40
- new_ast = find_class.updated(nil, class_node, nil)
41
- else
42
- nodes[1] = nodes[0].updated(:const, [nil, parent_class.to_sym])
43
- new_ast = ast.updated(nil, nodes, nil)
44
- end
45
- Unparser.unparse(new_ast)
46
- end
47
-
48
- def find_class
49
- return ast if ast.try(:type) == :class
50
- ast.to_a.select {|n| n.try(:type) == :class}.first
51
- end
52
-
53
- def ast
54
- @ast ||= Parser::CurrentRuby.parse(source)
55
- end
56
-
57
- def nodes
58
- @nodes ||= ast.to_a.dup
59
- end
60
-
61
- def reset_nodes
62
- @nodes = nil
63
- end
64
-
65
- end
66
-
67
- end
68
-
@@ -1,30 +0,0 @@
1
- require 'pathname'
2
- module ActiveMocker
3
- # @api private
4
- class SchemaReader
5
-
6
- def tables
7
- load_table
8
- end
9
-
10
- private
11
-
12
- def load_table
13
- eval_file
14
- raise "#{table_name} table not found." unless @tables
15
- @tables
16
- end
17
-
18
- def eval_file
19
- m = Module.new
20
- @tables = m.module_eval(read_file).tables
21
- end
22
-
23
- def read_file
24
- Config.file_reader.read(Config.schema_file)
25
- end
26
-
27
- end
28
-
29
- end
30
-
@@ -1,18 +0,0 @@
1
- module ActiveMocker
2
- # @api private
3
- class StringReader
4
- def initialize(file)
5
- @read = file
6
- end
7
-
8
- def read(path)
9
- if @read.is_a?(Hash)
10
- @read[path.sub('/','').sub('.rb','')]
11
- else
12
- @read
13
- end
14
- end
15
- end
16
-
17
- end
18
-