active_mocker 2.0.0 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -3
- data/README.md +9 -3
- data/lib/active_mocker.rb +1 -2
- data/lib/active_mocker/display_errors.rb +9 -0
- data/lib/active_mocker/file_path_to_ruby_class.rb +16 -0
- data/lib/active_mocker/generate.rb +11 -4
- data/lib/active_mocker/mock/base.rb +3 -4
- data/lib/active_mocker/mock/queries.rb +2 -2
- data/lib/active_mocker/mock_creator.rb +16 -2
- data/lib/active_mocker/null_progress.rb +7 -2
- data/lib/active_mocker/progress.rb +17 -5
- data/lib/active_mocker/task.rake +5 -3
- data/lib/active_mocker/template_creator.rb +5 -4
- data/lib/active_mocker/version.rb +1 -1
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7cb66180d5ec42fc5a0971815560364088008ced
|
4
|
+
data.tar.gz: 61b57337f4569854453c417d84323816995fc841
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ee31dab1abe47dff3fd80b11fa28b5767008d04b1c3eb266feae2ecc486a7b4bd62eaf093d97cc6b85a6c09e2b25f59ebb706982146f69a6681f2f88c7f397ed
|
7
|
+
data.tar.gz: 30ac184f5e8b5f42bc79ace88aaca7475a8cdd9a6c696d3aa11a51232a0b389365c0b1396eacc475fba916f87d5f29d09d6a044cb4cf841f71cc00346ad24269
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,11 @@
|
|
1
1
|
# Changelog
|
2
2
|
All notable changes to this project will be documented in this file.
|
3
3
|
|
4
|
+
## 2.1.0 - 2106-02-05
|
5
|
+
### Enhancement
|
6
|
+
- Support for module nested models.
|
7
|
+
- Adding support for ruby 2.3
|
8
|
+
|
4
9
|
## 2.0.0 - 2015-12-22
|
5
10
|
### Enhancement
|
6
11
|
- The mock append name is now changeable using `ActiveMocker::Config.mock_append_name=`. The default still being `Mock`.
|
@@ -18,9 +23,9 @@ All notable changes to this project will be documented in this file.
|
|
18
23
|
- Reduce restriction on Virtus gem to 1.0.any
|
19
24
|
- When id is not a fixnum and make it match ActiveRecord behavior of calling `#to_i`.
|
20
25
|
|
21
|
-
###
|
22
|
-
- Moved `ActiveMocker::MockAbilities` into "active_mocker/deprecated_components/mock_abilities" if a project is still dependent on it this file it can be required and used at least until version
|
23
|
-
- `#mock_class("Mock")` method has been moved to "active_mocker/deprecated_components/rspec_helper" if a project is still dependent on it this file it can be required and used at least until version
|
26
|
+
### Deprecated
|
27
|
+
- Moved `ActiveMocker::MockAbilities` into "active_mocker/deprecated_components/mock_abilities" if a project is still dependent on it this file it can be required and used at least until version 3.0. The alternative is to use `RSpec` verified doubles.
|
28
|
+
- `#mock_class("Mock")` method has been moved to "active_mocker/deprecated_components/rspec_helper" if a project is still dependent on it this file it can be required and used at least until version 3.0. The alternative is to use the new api that is accessible by instead `active_mocker.mocks.find('ClassName')`.
|
24
29
|
|
25
30
|
### Removed
|
26
31
|
- `log/active_mocker.log` is replaced env `ERROR_VERBOSITY=[0,1,2,3] rake active_mocker:build` or in Ruby `ActiveMocker::Config.error_verbosity=`
|
data/README.md
CHANGED
@@ -267,9 +267,15 @@ PersonMock::CONSTANT_VALUE
|
|
267
267
|
|
268
268
|
### Managing Mocks
|
269
269
|
|
270
|
-
Deletes All Records for Loaded Mocks - (Useful in after(:each) to clean up state between examples)
|
271
270
|
```ruby
|
272
|
-
|
271
|
+
require "active_mocker/rspec_helper"
|
272
|
+
|
273
|
+
active_mocker.delete_all # Delete all records from loaded mocks
|
274
|
+
|
275
|
+
active_mocker.find("User") # Find a mock by model name. Useful in before(:all)/after(:all) where automatic constant stubbing is unavailable.
|
276
|
+
|
277
|
+
active_mocker.mocks.except("User").delete_all # Delete all loaded mock expect the User mock.
|
278
|
+
|
273
279
|
```
|
274
280
|
### ActiveRecord supported methods
|
275
281
|
|
@@ -356,7 +362,7 @@ See [Documentation](http://rdoc.info/github/zeisler/active_mocker/master/ActiveM
|
|
356
362
|
* Validation/Callbacks are not supported.
|
357
363
|
* Sql queries, joins, etc will never be supported.
|
358
364
|
* A record that has been created and then is modified will persist changes without calling `#save`, beware of this difference.
|
359
|
-
* This is not a full replacement for ActiveRecord.
|
365
|
+
* This is not a full replacement for ActiveRecord.
|
360
366
|
* Primary key will always default to `id`. If this is an causes a problem open an issue.
|
361
367
|
|
362
368
|
## Inspiration
|
data/lib/active_mocker.rb
CHANGED
@@ -10,7 +10,6 @@
|
|
10
10
|
require "rubygems"
|
11
11
|
require 'active_mocker/version'
|
12
12
|
require 'active_mocker/railtie' if defined?(Rails)
|
13
|
-
require 'ruby-progressbar'
|
14
13
|
require 'forwardable'
|
15
14
|
require 'active_support/all'
|
16
15
|
require 'active_mocker/public_methods'
|
@@ -18,7 +17,7 @@ require 'active_mocker/config'
|
|
18
17
|
require "reverse_parameters"
|
19
18
|
require "active_record_schema_scrapper"
|
20
19
|
require "dissociated_introspection"
|
21
|
-
require "
|
20
|
+
require "active_mocker/file_path_to_ruby_class"
|
22
21
|
require "active_mocker/hash_new_style"
|
23
22
|
require "active_mocker/null_progress"
|
24
23
|
require "active_mocker/progress"
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module ActiveMocker
|
2
|
+
class FilePathToRubyClass
|
3
|
+
|
4
|
+
attr_reader :class_path, :base_path
|
5
|
+
|
6
|
+
def initialize(base_path:, class_path:)
|
7
|
+
@base_path = base_path
|
8
|
+
@class_path = class_path
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_s
|
12
|
+
File.basename((class_path.gsub(base_path + "/", "")).split("/").map(&:camelize).join("::"), '.rb')
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
@@ -16,6 +16,7 @@ module ActiveMocker
|
|
16
16
|
model = get_model_const(model_name)
|
17
17
|
mock_file_name = "#{model_name.underscore}_#{config.mock_append_name.underscore}.rb"
|
18
18
|
mock_file_path = File.join(Config.mock_dir, mock_file_name)
|
19
|
+
assure_dir_path_exists(mock_file_path)
|
19
20
|
schema_scrapper = ActiveRecordSchemaScrapper.new(model: model)
|
20
21
|
File.open(mock_file_path, 'w') do |file_out|
|
21
22
|
begin
|
@@ -34,7 +35,7 @@ module ActiveMocker
|
|
34
35
|
|
35
36
|
def get_model_const(model_name)
|
36
37
|
model_name.constantize
|
37
|
-
rescue => e
|
38
|
+
rescue StandardError, LoadError => e
|
38
39
|
display_errors.wrap_an_exception(e, model_name)
|
39
40
|
end
|
40
41
|
|
@@ -73,7 +74,7 @@ module ActiveMocker
|
|
73
74
|
end
|
74
75
|
|
75
76
|
def model_name(file)
|
76
|
-
|
77
|
+
FilePathToRubyClass.new(base_path: config.model_dir, class_path: file).to_s
|
77
78
|
end
|
78
79
|
|
79
80
|
def model_names
|
@@ -85,11 +86,17 @@ module ActiveMocker
|
|
85
86
|
end
|
86
87
|
|
87
88
|
def progress_init
|
88
|
-
@progress = config.progress_class.
|
89
|
+
@progress = config.progress_class.create(models_paths.count)
|
89
90
|
end
|
90
91
|
|
91
92
|
def models_paths
|
92
|
-
@models_paths ||= Dir.glob(config.single_model_path || File.join(config.model_dir, "
|
93
|
+
@models_paths ||= Dir.glob(config.single_model_path || File.join(config.model_dir, "**/*.rb"))
|
94
|
+
end
|
95
|
+
|
96
|
+
def assure_dir_path_exists(file)
|
97
|
+
unless File.exists?(File.dirname(file))
|
98
|
+
FileUtils::mkdir_p(File.dirname(file))
|
99
|
+
end
|
93
100
|
end
|
94
101
|
|
95
102
|
def config
|
@@ -161,13 +161,12 @@ module ActiveMocker
|
|
161
161
|
# @private
|
162
162
|
attr_accessor :_create_caller_locations
|
163
163
|
# New objects can be instantiated as either empty (pass no construction parameter) or pre-set with
|
164
|
-
# attributes
|
165
|
-
# In both instances, valid attribute keys are determined by the column names of the associated table --
|
166
|
-
# hence you can't have attributes that aren't part of the table columns.
|
164
|
+
# attributes.
|
167
165
|
#
|
168
166
|
# ==== Example:
|
169
167
|
# # Instantiates a single new object
|
170
168
|
# UserMock.new(first_name: 'Jamie')
|
169
|
+
|
171
170
|
def initialize(attributes = {}, &block)
|
172
171
|
if self.class.abstract_class?
|
173
172
|
raise NotImplementedError, "#{self.class.name} is an abstract class and cannot be instantiated."
|
@@ -336,4 +335,4 @@ module ActiveMocker
|
|
336
335
|
module Scopes
|
337
336
|
end
|
338
337
|
end
|
339
|
-
end
|
338
|
+
end
|
@@ -199,7 +199,7 @@ module ActiveMocker
|
|
199
199
|
end
|
200
200
|
|
201
201
|
# Like <tt>find_by</tt>, except that if no record is found, raises
|
202
|
-
# an <tt>
|
202
|
+
# an <tt>ActiveMocker::RecordNotFound</tt> error.
|
203
203
|
def find_by!(conditions={})
|
204
204
|
result = find_by(conditions)
|
205
205
|
if result.nil?
|
@@ -360,4 +360,4 @@ module ActiveMocker
|
|
360
360
|
end
|
361
361
|
|
362
362
|
end
|
363
|
-
end
|
363
|
+
end
|
@@ -62,7 +62,13 @@ module ActiveMocker
|
|
62
62
|
def template_creator_default(file_out)
|
63
63
|
TemplateCreator.new(file_out: file_out,
|
64
64
|
erb_template: File.new(File.join(File.dirname(__FILE__), "mock_template.erb"), 'r'),
|
65
|
-
binding: binding
|
65
|
+
binding: binding,
|
66
|
+
post_process: -> (str) {
|
67
|
+
ruby_code = DissociatedIntrospection::RubyCode.build_from_source(str, parse_with_comments: true)
|
68
|
+
DissociatedIntrospection::WrapInModules.new(ruby_code: ruby_code)
|
69
|
+
.call(modules: nested_modules)
|
70
|
+
.source_from_ast.gsub(/end\n/, "end\n\n")
|
71
|
+
})
|
66
72
|
end
|
67
73
|
|
68
74
|
def class_introspector_default
|
@@ -104,7 +110,13 @@ module ActiveMocker
|
|
104
110
|
end
|
105
111
|
|
106
112
|
def class_name
|
107
|
-
class_introspector.parsed_source.class_name
|
113
|
+
@class_name ||= class_introspector.parsed_source.class_name.split("::").last
|
114
|
+
end
|
115
|
+
|
116
|
+
def nested_modules
|
117
|
+
@nested_modules ||= begin
|
118
|
+
class_introspector.parsed_source.module_nesting.join("::")
|
119
|
+
end
|
108
120
|
end
|
109
121
|
|
110
122
|
def parent_class
|
@@ -216,6 +228,7 @@ module ActiveMocker
|
|
216
228
|
class_introspector
|
217
229
|
.get_class
|
218
230
|
.public_instance_methods(false)
|
231
|
+
.sort
|
219
232
|
.map { |m| create_method(m, :instance_method) }
|
220
233
|
end
|
221
234
|
|
@@ -223,6 +236,7 @@ module ActiveMocker
|
|
223
236
|
class_introspector
|
224
237
|
.get_class
|
225
238
|
.methods(false)
|
239
|
+
.sort
|
226
240
|
.map { |m| create_method(m, :method) }
|
227
241
|
end
|
228
242
|
|
@@ -1,13 +1,25 @@
|
|
1
1
|
module ActiveMocker
|
2
2
|
class Progress
|
3
|
+
|
4
|
+
def self.create(count)
|
5
|
+
require 'ruby-progressbar'
|
6
|
+
self.new(count)
|
7
|
+
rescue LoadError
|
8
|
+
NullProgress.new
|
9
|
+
end
|
10
|
+
|
3
11
|
def initialize(count)
|
4
|
-
@
|
5
|
-
total: count,
|
6
|
-
format: '%t |%b>>%i| %p%%')
|
12
|
+
@count = count
|
7
13
|
end
|
8
14
|
|
9
15
|
def increment
|
10
|
-
|
16
|
+
progress.increment
|
17
|
+
end
|
18
|
+
|
19
|
+
def progress
|
20
|
+
@progress ||= ProgressBar.create(title: 'Generating Mocks',
|
21
|
+
total: @count,
|
22
|
+
format: '%t |%b>>%i| %p%%')
|
11
23
|
end
|
12
24
|
end
|
13
|
-
end
|
25
|
+
end
|
data/lib/active_mocker/task.rake
CHANGED
@@ -3,9 +3,11 @@ namespace :active_mocker do
|
|
3
3
|
desc('Rebuild mocks.')
|
4
4
|
task :build => :environment do
|
5
5
|
ActiveMocker.configure do |c|
|
6
|
-
c.single_model_path
|
7
|
-
c.
|
8
|
-
c.
|
6
|
+
c.single_model_path = ENV["MODEL"] if ENV["MODEL"]
|
7
|
+
c.model_dir = ENV["MODEL_DIR"] if ENV["MODEL_DIR"]
|
8
|
+
c.mock_dir = ENV["MOCK_DIR"] if ENV["MOCK_DIR"]
|
9
|
+
c.progress_bar = false if ENV["MUTE_PROGRESS_BAR"]
|
10
|
+
c.error_verbosity = ENV["ERROR_VERBOSITY"].to_i if ENV["ERROR_VERBOSITY"]
|
9
11
|
c.disable_modules_and_constants = false
|
10
12
|
end.create_mocks
|
11
13
|
end
|
@@ -3,20 +3,21 @@ require 'forwardable'
|
|
3
3
|
module ActiveMocker
|
4
4
|
class TemplateCreator
|
5
5
|
|
6
|
-
def initialize(erb_template:, file_out: nil, binding:)
|
6
|
+
def initialize(erb_template:, file_out: nil, binding:, post_process: -> (str){str})
|
7
7
|
@erb_template = erb_template
|
8
8
|
@binding = binding
|
9
9
|
@file_out = file_out || Tempfile.new('TemplateModel')
|
10
|
+
@post_process = post_process
|
10
11
|
end
|
11
12
|
|
12
13
|
def render
|
13
14
|
template = ERB.new(erb_template.read, nil, '>')
|
14
|
-
file_out.write template.result(binding)
|
15
|
+
file_out.write post_process.call(template.result(binding))
|
15
16
|
file_out
|
16
17
|
end
|
17
18
|
|
18
19
|
private
|
19
20
|
|
20
|
-
attr_reader :erb_template, :binding, :file_out
|
21
|
+
attr_reader :erb_template, :binding, :file_out, :post_process
|
21
22
|
end
|
22
|
-
end
|
23
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_mocker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dustin Zeisler
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-02-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -114,14 +114,14 @@ dependencies:
|
|
114
114
|
requirements:
|
115
115
|
- - '='
|
116
116
|
- !ruby/object:Gem::Version
|
117
|
-
version: 0.1
|
117
|
+
version: 0.4.1
|
118
118
|
type: :runtime
|
119
119
|
prerelease: false
|
120
120
|
version_requirements: !ruby/object:Gem::Requirement
|
121
121
|
requirements:
|
122
122
|
- - '='
|
123
123
|
- !ruby/object:Gem::Version
|
124
|
-
version: 0.1
|
124
|
+
version: 0.4.1
|
125
125
|
- !ruby/object:Gem::Dependency
|
126
126
|
name: bundler
|
127
127
|
requirement: !ruby/object:Gem::Requirement
|
@@ -175,6 +175,7 @@ files:
|
|
175
175
|
- lib/active_mocker/deprecated_components/rspec.rb
|
176
176
|
- lib/active_mocker/display_errors.rb
|
177
177
|
- lib/active_mocker/error_object.rb
|
178
|
+
- lib/active_mocker/file_path_to_ruby_class.rb
|
178
179
|
- lib/active_mocker/generate.rb
|
179
180
|
- lib/active_mocker/hash_new_style.rb
|
180
181
|
- lib/active_mocker/loaded_mocks.rb
|