active_mocker 1.8.4 → 2.0.0.beta1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +13 -0
- data/README.md +4 -2
- data/lib/active_mocker.rb +9 -25
- data/lib/active_mocker/config.rb +26 -46
- data/lib/active_mocker/generate.rb +115 -110
- data/lib/active_mocker/loaded_mocks.rb +76 -65
- data/lib/active_mocker/mock/base.rb +283 -287
- data/lib/active_mocker/mock/has_many.rb +2 -0
- data/lib/active_mocker/mock_creator.rb +262 -0
- data/lib/active_mocker/mock_template.erb +9 -186
- data/lib/active_mocker/mock_template/_associations.erb +82 -0
- data/lib/active_mocker/mock_template/_attributes.erb +11 -0
- data/lib/active_mocker/mock_template/_class_methods.erb +41 -0
- data/lib/active_mocker/mock_template/_defined_methods.erb +10 -0
- data/lib/active_mocker/mock_template/_modules_constants.erb +10 -0
- data/lib/active_mocker/mock_template/_scopes.erb +23 -0
- data/lib/active_mocker/null_progress.rb +9 -0
- data/lib/active_mocker/output_capture.rb +32 -0
- data/lib/active_mocker/parent_class.rb +64 -0
- data/lib/active_mocker/progress.rb +13 -0
- data/lib/active_mocker/public_methods.rb +15 -23
- data/lib/active_mocker/rspec.rb +16 -0
- data/lib/active_mocker/rspec_helper.rb +10 -8
- data/lib/active_mocker/task.rake +6 -1
- data/lib/active_mocker/template_creator.rb +22 -0
- data/lib/active_mocker/version.rb +1 -1
- metadata +43 -103
- data/lib/active_mocker/active_record.rb +0 -74
- data/lib/active_mocker/active_record/field.rb +0 -39
- data/lib/active_mocker/active_record/relationships.rb +0 -110
- data/lib/active_mocker/active_record/schema.rb +0 -81
- data/lib/active_mocker/active_record/scope.rb +0 -22
- data/lib/active_mocker/active_record/table.rb +0 -26
- data/lib/active_mocker/active_record/unknown_class_method.rb +0 -17
- data/lib/active_mocker/active_record/unknown_module.rb +0 -30
- data/lib/active_mocker/db_to_ruby_type.rb +0 -29
- data/lib/active_mocker/file_reader.rb +0 -11
- data/lib/active_mocker/model_reader.rb +0 -191
- data/lib/active_mocker/model_schema.rb +0 -285
- data/lib/active_mocker/model_schema/assemble.rb +0 -220
- data/lib/active_mocker/reparameterize.rb +0 -41
- data/lib/active_mocker/ruby_parse.rb +0 -68
- data/lib/active_mocker/schema_reader.rb +0 -30
- data/lib/active_mocker/string_reader.rb +0 -18
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fb2357b680e38a103a603361d01ca9ed9a67f62f
|
4
|
+
data.tar.gz: 0f82aa8aa703d9919650d19d5ff02eac1f4ad6ae
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ed04ff66eb99a25b4930262cdda6e124b26df1db6e2177e8f80ea20ef55d81ef8233dbdfda54b5c99dc9edb8454a8f5210ea57fe33cc5a4c76c2cc5b66604cd5
|
7
|
+
data.tar.gz: 5417b116dc8afda6894b717e90c2fbfc212a44291aa92a2c717a39887e86e3842301ccd1c813290b9f536851064e4fe0fe4301abc286b451697a2b45ff22986c
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,19 @@
|
|
1
1
|
# Changelog
|
2
2
|
All notable changes to this project will be documented in this file.
|
3
3
|
|
4
|
+
## 2.0.0.beta1 - 2015-12-01
|
5
|
+
### Enhancement
|
6
|
+
- db/schema.rb is no longer required to generate mocks.
|
7
|
+
- `ActiveMocker::Config.disable_modules_and_constants=` Non locally defineded Modules are included/extended by default as well as constant declaration. To Disable to feature set to `true`.
|
8
|
+
- `MODEL=User rake active_mocker:build` no longer takes a model name to generate just that mock. It now takes a path to a model file. `MODEL=[path-to-model] rake active_mocker:build`
|
9
|
+
|
10
|
+
### Removed
|
11
|
+
- `log/active_mocker.log` is replaced env `ERROR_VERBOSITY=[0,1,2] rake active_mocker:build` or in Ruby `ActiveMocker::Config.error_verbosity=`
|
12
|
+
- Removing undocumented feature of treating children of mocks differently. Remove the ability to sub class a mock and have it for use in that context when creating/finding association.
|
13
|
+
|
14
|
+
### Depracating
|
15
|
+
- `mock_class` for rspec_helper, use `instead active_mocker.mocks.find('ClassName')`. Will be removed in v2.1
|
16
|
+
|
4
17
|
## 1.8.4 - 2015-10-06
|
5
18
|
### Fix
|
6
19
|
- Calling scoped method that has not been stubbed raises incorrect error. https://github.com/zeisler/active_mocker/issues/22
|
data/README.md
CHANGED
@@ -4,8 +4,10 @@
|
|
4
4
|
[](https://codeclimate.com/github/zeisler/active_mocker)
|
5
5
|
[](https://gemnasium.com/zeisler/active_mocker)
|
6
6
|
[](https://gitter.im/zeisler/active_mocker)
|
7
|
+
[](https://www.gittip.com/zeisler/)
|
7
8
|
|
8
|
-
|
9
|
+
## Description
|
10
|
+
ActiveMocker creates mock classes from ActiveRecord models, allowing your test suite to run at breakneck speed. This can be done by not loading Rails or hitting a database. The models are read dynamically and statically so that ActiveMocker can generate a Ruby file to require within a test. The mock file can be run by itself and comes with a partial implementation of ActiveRecord. Attributes and associations can be used the same as in ActiveRecord. Methods have the same argument signature but raise a NotImplementedError when called, allowing you to stub it with a mocking framework, like RSpec. Mocks are regenerated when the schema is modified so your mocks won't go stale, preventing the case where your units tests pass but production code fails.
|
9
11
|
|
10
12
|
Examples from a real apps
|
11
13
|
|
@@ -254,7 +256,7 @@ end
|
|
254
256
|
```
|
255
257
|
### Constants and Modules
|
256
258
|
|
257
|
-
* Any locally defined modules will not be included or extended.
|
259
|
+
* Any locally defined modules will not be included or extended. It can be disabled by `ActiveMocker::Config.disable_modules_and_constants = true`
|
258
260
|
|
259
261
|
---------------
|
260
262
|
```ruby
|
data/lib/active_mocker.rb
CHANGED
@@ -1,32 +1,16 @@
|
|
1
1
|
require 'active_mocker/version'
|
2
|
-
|
3
|
-
require 'logger'
|
4
2
|
require 'active_mocker/railtie' if defined?(Rails)
|
5
3
|
require 'ruby-progressbar'
|
6
4
|
require 'forwardable'
|
7
5
|
require 'active_support/all'
|
8
|
-
require 'singleton'
|
9
|
-
require 'parser/current'
|
10
|
-
require 'unparser'
|
11
|
-
require 'virtus'
|
12
|
-
require 'attr_permit'
|
13
|
-
require 'active_mocker/version'
|
14
|
-
require 'active_mocker/ruby_parse'
|
15
6
|
require 'active_mocker/public_methods'
|
16
7
|
require 'active_mocker/config'
|
17
|
-
require
|
18
|
-
require
|
19
|
-
require
|
20
|
-
require
|
21
|
-
require
|
22
|
-
require
|
23
|
-
require
|
24
|
-
require
|
25
|
-
require
|
26
|
-
require 'active_mocker/active_record/schema'
|
27
|
-
require 'active_mocker/active_record'
|
28
|
-
require 'active_mocker/model_reader'
|
29
|
-
require 'active_mocker/reparameterize'
|
30
|
-
require 'active_mocker/db_to_ruby_type'
|
31
|
-
require 'active_mocker/model_schema'
|
32
|
-
require 'active_mocker/model_schema/assemble'
|
8
|
+
require "reverse_parameters"
|
9
|
+
require "active_record_schema_scrapper"
|
10
|
+
require "dissociated_introspection"
|
11
|
+
require "active_mocker/null_progress"
|
12
|
+
require "active_mocker/progress"
|
13
|
+
require "active_mocker/parent_class"
|
14
|
+
require "active_mocker/template_creator"
|
15
|
+
require "active_mocker/mock_creator"
|
16
|
+
require "active_mocker/generate"
|
data/lib/active_mocker/config.rb
CHANGED
@@ -3,73 +3,53 @@ module ActiveMocker
|
|
3
3
|
class Config
|
4
4
|
class << self
|
5
5
|
|
6
|
-
attr_accessor :
|
7
|
-
:model_dir,
|
6
|
+
attr_accessor :model_dir,
|
8
7
|
:mock_dir,
|
9
|
-
:
|
10
|
-
:
|
11
|
-
:
|
12
|
-
:
|
13
|
-
|
14
|
-
|
8
|
+
:single_model_path,
|
9
|
+
:progress_bar,
|
10
|
+
:error_verbosity,
|
11
|
+
:disable_modules_and_constants
|
12
|
+
#TODO add disable include/extend of modules
|
13
|
+
#TODO disable scope methods
|
14
|
+
#disable instance methods, class methods
|
15
|
+
# can't disable attribute as of yet
|
15
16
|
|
16
17
|
def model_base_classes=(val)
|
17
18
|
@model_base_classes = val
|
18
19
|
end
|
19
20
|
|
20
|
-
def set
|
21
|
+
def set(&block)
|
21
22
|
load_defaults
|
22
23
|
yield self
|
23
24
|
end
|
24
25
|
|
25
26
|
def load_defaults
|
26
|
-
@
|
27
|
-
@
|
28
|
-
@
|
29
|
-
@
|
30
|
-
@
|
31
|
-
@log_location = 'log/active_mocker.log' unless @log_location
|
32
|
-
@clear_log_on_build = true
|
33
|
-
@generate_for_mock = ENV['MODEL']
|
27
|
+
@error_verbosity = 1
|
28
|
+
@progress_bar = true
|
29
|
+
@disable_modules_and_constants = false
|
30
|
+
@model_dir = nil unless @model_dir
|
31
|
+
@mock_dir = nil unless @mock_dir
|
34
32
|
rails_defaults if Object.const_defined?('Rails')
|
35
33
|
end
|
36
34
|
|
37
|
-
def logger
|
38
|
-
default_logger
|
39
|
-
end
|
40
|
-
|
41
35
|
def reset_all
|
42
|
-
[
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
:@model_dir,
|
50
|
-
:@mock_dir].each{|ivar| instance_variable_set(ivar, nil)}
|
51
|
-
end
|
52
|
-
|
53
|
-
def default_logger
|
54
|
-
FileUtils.mkdir_p(File.dirname(@log_location)) unless File.directory?(File.dirname(@log_location))
|
55
|
-
::Logger.new(@log_location)
|
36
|
+
[:model_dir,
|
37
|
+
:mock_dir,
|
38
|
+
:log_location,
|
39
|
+
:single_model_path,
|
40
|
+
:progress_bar,
|
41
|
+
:error_verbosity
|
42
|
+
].each { |ivar| instance_variable_set("@#{ivar}", nil) }
|
56
43
|
end
|
57
44
|
|
58
45
|
def rails_defaults
|
59
|
-
@
|
60
|
-
@
|
61
|
-
@mock_dir = File.join(Rails.root, 'spec/mocks') unless @mock_dir
|
46
|
+
@model_dir = File.join(Rails.root, 'app/models') unless @model_dir
|
47
|
+
@mock_dir = File.join(Rails.root, 'spec/mocks') unless @mock_dir
|
62
48
|
end
|
63
49
|
|
64
|
-
def
|
65
|
-
|
66
|
-
FileUtils.rm(@log_location)
|
67
|
-
end
|
50
|
+
def progress_class
|
51
|
+
@progress_bar ? Progress : NullProgress
|
68
52
|
end
|
69
|
-
|
70
53
|
end
|
71
|
-
|
72
54
|
end
|
73
|
-
|
74
55
|
end
|
75
|
-
|
@@ -1,134 +1,139 @@
|
|
1
|
-
require 'ruby-progressbar'
|
2
|
-
require 'forwardable'
|
3
1
|
module ActiveMocker
|
4
|
-
class Generate
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
create_template
|
14
|
-
Config.build_in_progress = false
|
15
|
-
end
|
16
|
-
|
17
|
-
private
|
18
|
-
|
19
|
-
def generate_model_schema
|
20
|
-
ActiveMocker::ModelSchema::Assemble.new(progress: progress).run
|
21
|
-
end
|
2
|
+
class Generate
|
3
|
+
|
4
|
+
def initialize
|
5
|
+
raise ArgumentError, "mock_dir is missing a valued value!" if config.mock_dir.nil? || config.mock_dir.empty?
|
6
|
+
create_mock_dir
|
7
|
+
raise ArgumentError, "model_dir is missing a valued value!" if config.model_dir.nil? || config.model_dir.empty? || !Dir.exists?(config.model_dir)
|
8
|
+
@success_count = 0
|
9
|
+
@errors = []
|
10
|
+
end
|
22
11
|
|
23
|
-
|
24
|
-
|
25
|
-
|
12
|
+
# @returns self
|
13
|
+
def call
|
14
|
+
progress_init
|
15
|
+
models_paths.each do |file|
|
16
|
+
model_name = model_name(file)
|
17
|
+
model = model_name.constantize
|
18
|
+
mock_file_name = "#{model_name.underscore}_#{mock_append_name.underscore}.rb"
|
19
|
+
mock_file_path = File.join(Config.mock_dir, mock_file_name)
|
20
|
+
schema_scrapper = schema_scrapper(model)
|
21
|
+
File.open(mock_file_path, 'w') do |file_out|
|
22
|
+
begin
|
23
|
+
r = create_mock(file, file_out, schema_scrapper)
|
24
|
+
collect_errors(mock_file_path, r, schema_scrapper)
|
25
|
+
@success_count += 1 if r.completed?
|
26
|
+
rescue => e
|
27
|
+
rescue_clean_up(e, file_out, model_name)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
progress.increment
|
31
|
+
end
|
32
|
+
display_errors
|
33
|
+
failure_count_message
|
34
|
+
self
|
35
|
+
end
|
26
36
|
|
27
|
-
|
28
|
-
return @progress if !@progress.nil? || silence == true
|
29
|
-
progress_options = {:title => 'Generating Mocks',
|
30
|
-
:total => model_count * 2,
|
31
|
-
format: '%t |%b>>%i| %p%%'}
|
32
|
-
@progress = ProgressBar.create(progress_options)
|
33
|
-
end
|
37
|
+
private
|
34
38
|
|
35
|
-
|
36
|
-
progress.increment unless silence
|
37
|
-
end
|
39
|
+
attr_reader :success_count, :errors
|
38
40
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
next unless generate_and_rescue(model)
|
41
|
+
def create_mock(file, file_out, schema_scrapper)
|
42
|
+
MockCreator.new(file: File.open(file),
|
43
|
+
file_out: file_out,
|
44
|
+
schema_scrapper: schema_scrapper,
|
45
|
+
klasses_to_be_mocked: model_names,
|
46
|
+
enabled_partials: enabled_partials).create
|
46
47
|
end
|
47
|
-
exit_message
|
48
|
-
end
|
49
48
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
49
|
+
def collect_errors(mock_file_path, r, schema_scrapper)
|
50
|
+
unless r.errors.empty?
|
51
|
+
File.delete(mock_file_path)
|
52
|
+
errors.concat(r.errors)
|
53
|
+
end
|
54
|
+
unless schema_scrapper.associations.errors.empty?
|
55
|
+
errors.concat(schema_scrapper.associations.errors.uniq(&:message))
|
56
|
+
end
|
57
|
+
unless schema_scrapper.attributes.errors.empty?
|
58
|
+
errors.concat(schema_scrapper.attributes.errors.uniq(&:message))
|
59
|
+
end
|
58
60
|
end
|
59
|
-
end
|
60
61
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
raise model if model.class.ancestors.include?(Exception)
|
68
|
-
end
|
62
|
+
def rescue_clean_up(e, file_out, model_name)
|
63
|
+
file_out.close
|
64
|
+
File.delete(file_out.path)
|
65
|
+
e = OpenStruct.new(message: e.message, class_name: model_name, original_error: e) if e.class <= Exception
|
66
|
+
errors << e
|
67
|
+
end
|
69
68
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
69
|
+
def schema_scrapper(model)
|
70
|
+
# if model.respond_to?(:abstract_class?) && model.abstract_class?
|
71
|
+
# null_collection = OpenStruct.new(to_a: [], errors: [])
|
72
|
+
# OpenStruct.new(associations: null_collection, attributes: null_collection, abstract_class?: true)
|
73
|
+
# else
|
74
|
+
ActiveRecordSchemaScrapper.new(model: model)
|
75
|
+
# end
|
76
76
|
end
|
77
|
-
end
|
78
77
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
78
|
+
def display_errors
|
79
|
+
errors.flatten.each do |e|
|
80
|
+
if config.error_verbosity == 2
|
81
|
+
puts "#{e.class_name} has failed:"
|
82
|
+
puts e.message
|
83
|
+
puts e.original_error.message if e.respond_to? :original_error
|
84
|
+
puts e.original_error.backtrace if e.respond_to? :original_error
|
85
|
+
puts e.original_error.class.name if e.respond_to? :original_error
|
86
|
+
elsif config.error_verbosity == 1
|
87
|
+
puts e.message
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
84
91
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
92
|
+
def failure_count_message
|
93
|
+
if config.error_verbosity > 0 && success_count < models_paths.count
|
94
|
+
"#{ models_paths.count - success_count } mock(s) out of #{models_paths.count} failed."\
|
95
|
+
"To see more detail set error_verbosity = 2 or to mute this error set error_verbosity = 0."
|
96
|
+
end
|
97
|
+
end
|
90
98
|
|
91
|
-
|
92
|
-
|
93
|
-
|
99
|
+
def model_name(file)
|
100
|
+
File.basename(file, '.rb').camelize
|
101
|
+
end
|
94
102
|
|
95
|
-
|
96
|
-
|
97
|
-
f.write(klass_str)
|
103
|
+
def model_names
|
104
|
+
@model_names ||= models_paths.map { |p| model_name(p) }
|
98
105
|
end
|
99
|
-
end
|
100
106
|
|
101
|
-
|
102
|
-
|
103
|
-
|
107
|
+
def progress
|
108
|
+
@progress
|
109
|
+
end
|
104
110
|
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
end
|
111
|
+
def progress_init
|
112
|
+
@progress = config.progress_class.new(models_paths.count)
|
113
|
+
end
|
109
114
|
|
110
|
-
|
111
|
-
|
112
|
-
|
115
|
+
def models_paths
|
116
|
+
@models_paths ||= Dir.glob(config.single_model_path || File.join(config.model_dir, "*.rb"))
|
117
|
+
end
|
113
118
|
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
end
|
119
|
+
def config
|
120
|
+
ActiveMocker::Config
|
121
|
+
end
|
118
122
|
|
119
|
-
|
120
|
-
|
121
|
-
|
123
|
+
def create_mock_dir
|
124
|
+
FileUtils::mkdir_p(config.mock_dir) unless Dir.exists?(config.mock_dir)
|
125
|
+
end
|
122
126
|
|
123
|
-
|
124
|
-
|
125
|
-
|
127
|
+
def mock_append_name
|
128
|
+
'Mock'
|
129
|
+
end
|
126
130
|
|
127
|
-
|
128
|
-
|
131
|
+
def enabled_partials
|
132
|
+
if config.disable_modules_and_constants
|
133
|
+
MockCreator.enabled_partials_default - [*:modules_constants]
|
134
|
+
else
|
135
|
+
MockCreator.enabled_partials_default
|
136
|
+
end
|
137
|
+
end
|
129
138
|
end
|
130
|
-
|
131
|
-
end
|
132
|
-
end
|
133
|
-
|
134
|
-
|
139
|
+
end
|
@@ -1,91 +1,102 @@
|
|
1
1
|
module ActiveMocker
|
2
|
-
|
3
2
|
class LoadedMocks
|
4
|
-
|
5
3
|
class << self
|
6
4
|
|
7
|
-
|
8
|
-
|
9
|
-
def find(klass)
|
10
|
-
class_name_to_mock[klass]
|
11
|
-
end
|
5
|
+
extend Forwardable
|
6
|
+
def_delegators :mocks, :find, :delete_all
|
12
7
|
|
13
|
-
# Returns
|
14
|
-
#
|
15
|
-
# ActiveMocker::LoadedMocks.
|
16
|
-
#
|
17
|
-
|
18
|
-
|
8
|
+
# Returns an Enumerable of all currently loaded mocks.
|
9
|
+
#
|
10
|
+
# ActiveMocker::LoadedMocks.mocks
|
11
|
+
# => <Collection @hash: {'Person' => PersonMock}>
|
12
|
+
# @return ActiveMocker::LoadedMocks::Collection
|
13
|
+
def mocks
|
14
|
+
Collection.new(mocks_store.values.each_with_object({}) do |mock_constant, hash|
|
15
|
+
hash[mock_constant.send(:mocked_class)] = mock_constant
|
16
|
+
end)
|
19
17
|
end
|
20
18
|
|
21
|
-
#
|
22
|
-
|
23
|
-
# Method will be deprecated in v2 because mocking is deprecated
|
24
|
-
def clear_all
|
25
|
-
all_mocks.each { |m| m.clear_mock }
|
26
|
-
clear_subclasses
|
27
|
-
end
|
19
|
+
# @deprecated Use {#mocks} instead of this method.
|
20
|
+
alias_method :class_name_to_mock, :mocks
|
28
21
|
|
29
|
-
#
|
30
|
-
|
31
|
-
def delete_all
|
32
|
-
all_mocks.each { |m| m.delete_all }
|
33
|
-
end
|
22
|
+
# @deprecated Use {#mocks} instead of this method.
|
23
|
+
alias_method :all, :mocks
|
34
24
|
|
35
|
-
#
|
36
|
-
|
37
|
-
#
|
38
|
-
# ActiveMocker::LoadedMocks.class_name_to_mock
|
39
|
-
# => {'Person' => PersonMock}
|
40
|
-
def class_name_to_mock
|
41
|
-
mocks.values.each_with_object({}) do |mock_constant, hash|
|
42
|
-
hash[mock_constant.send(:mocked_class)] = mock_constant
|
43
|
-
end.merge(subclasses)
|
44
|
-
end
|
25
|
+
# @deprecated Use {#delete_all} instead of this method.
|
26
|
+
alias_method :clear_all, :delete_all
|
45
27
|
|
46
|
-
|
28
|
+
class Collection
|
47
29
|
|
48
|
-
|
49
|
-
mocks.merge!({mocks_to_add.name => mocks_to_add})
|
50
|
-
end
|
30
|
+
include Enumerable
|
51
31
|
|
52
|
-
|
53
|
-
|
54
|
-
|
32
|
+
# @option opts [Hash] hash
|
33
|
+
def initialize(hash={})
|
34
|
+
@hash = Hash[hash]
|
35
|
+
end
|
55
36
|
|
56
|
-
|
57
|
-
|
58
|
-
end
|
37
|
+
extend Forwardable
|
38
|
+
def_delegators :hash, :[]=, :[], :each, :to_hash, :to_h
|
59
39
|
|
60
|
-
|
61
|
-
|
62
|
-
|
40
|
+
# Calls {#delete_all} for all mocks globally, which removes all records that were saved or created.
|
41
|
+
# @return [NilClass]
|
42
|
+
def delete_all
|
43
|
+
mocks.each(&__method__)
|
63
44
|
end
|
64
|
-
end
|
65
45
|
|
66
|
-
|
67
|
-
|
68
|
-
|
46
|
+
# @param [Array<Symbol, String, ActiveMocker::Mock>] args an array of ActiveRecord Model Names as Strings or Symbols
|
47
|
+
# or of mock object.
|
48
|
+
# @return [ActiveMocker::LoadedMocks::Collection] returns ActiveMock equivalent class.
|
49
|
+
def slice(*args)
|
50
|
+
self.class.new(select { |k, v| get_item(args, k, v) })
|
51
|
+
end
|
69
52
|
|
70
|
-
|
71
|
-
|
72
|
-
|
53
|
+
# Input ActiveRecord Model Name as String or Symbol and it returns everything but that ActiveMock equivalent class.
|
54
|
+
# except('User') => [AccountMock, OtherMock]
|
55
|
+
# @param [Array<Symbol, String, ActiveMocker::Mock>] args
|
56
|
+
# @return ActiveMocker::LoadedMocks::Collection
|
57
|
+
def except(*args)
|
58
|
+
self.class.new(reject { |k, v| get_item(args, k, v) })
|
59
|
+
end
|
60
|
+
|
61
|
+
# Input ActiveRecord Model Name as String or Symbol returns ActiveMock equivalent class.
|
62
|
+
# find('User') => UserMock
|
63
|
+
# @param [Symbol, String, ActiveMocker::Mock] item
|
64
|
+
# @return ActiveMocker::Mock
|
65
|
+
def find(item)
|
66
|
+
slice(item).mocks.first
|
67
|
+
end
|
68
|
+
|
69
|
+
# @return [Array<ActiveMocker::Mock>]
|
70
|
+
def mocks
|
71
|
+
hash.values
|
72
|
+
end
|
73
|
+
alias_method :values, :mocks
|
74
|
+
|
75
|
+
private
|
76
|
+
attr_reader :hash
|
77
|
+
|
78
|
+
def get_item(args, k, v)
|
79
|
+
args.map do |e|
|
80
|
+
if [:to_str, :to_sym].any?{|i| e.respond_to? i}
|
81
|
+
e.to_s == k
|
82
|
+
else
|
83
|
+
e == v
|
84
|
+
end
|
85
|
+
end.any? { |a| a }
|
86
|
+
end
|
73
87
|
|
74
|
-
def clear_subclasses
|
75
|
-
subclasses.clear
|
76
88
|
end
|
77
89
|
|
78
|
-
|
79
|
-
|
80
|
-
|
90
|
+
private
|
91
|
+
|
92
|
+
def mocks_store
|
93
|
+
@mocks ||= {}
|
81
94
|
end
|
82
95
|
|
83
|
-
def
|
84
|
-
|
96
|
+
def add(mocks_to_add)
|
97
|
+
mocks_store.merge!({ mocks_to_add.name => mocks_to_add })
|
85
98
|
end
|
86
|
-
|
99
|
+
|
87
100
|
end
|
88
|
-
|
89
101
|
end
|
90
|
-
|
91
102
|
end
|