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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c9b2d6d3b9db64dae42eea2223e30608abae1b26
4
- data.tar.gz: 8b08a6fbe431e368a3316edbc5897e0f0a9509ee
3
+ metadata.gz: 7cb66180d5ec42fc5a0971815560364088008ced
4
+ data.tar.gz: 61b57337f4569854453c417d84323816995fc841
5
5
  SHA512:
6
- metadata.gz: 35604d9a34529d65362c204c108064508bd6bcff699fafa35a9696dd54dc74f11949b28d7f365f6fde7e066a37f981e9ad10f663a13381492e3be9dabc9845f7
7
- data.tar.gz: 34e6858458951b9b17b408da9e89459ddff3207845b19832206805ff6644cda465c4dd60d1a9eacc8d494d176a5238fbaf62661fe5213772bd2437582a7bcbbb
6
+ metadata.gz: ee31dab1abe47dff3fd80b11fa28b5767008d04b1c3eb266feae2ecc486a7b4bd62eaf093d97cc6b85a6c09e2b25f59ebb706982146f69a6681f2f88c7f397ed
7
+ data.tar.gz: 30ac184f5e8b5f42bc79ace88aaca7475a8cdd9a6c696d3aa11a51232a0b389365c0b1396eacc475fba916f87d5f29d09d6a044cb4cf841f71cc00346ad24269
@@ -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
- ### Depracated
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 2.1. The alternative is to use `RSpec` verified doubles.
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 2.1. The alternative is to use the new api that is accessible by `instead active_mocker.mocks.find('ClassName')`.
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
- ActiveMocker::LoadedMocks.delete_all
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. It's simplified, with only the most essential parts. There are many tests to verify that is works the same as ActiveRecord, but's yours responsibility to know how ActiveRecord works.
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
@@ -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 "colorize"
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"
@@ -67,3 +67,12 @@ module ActiveMocker
67
67
  end
68
68
  end
69
69
  end
70
+
71
+ class String
72
+ def colorize(*args)
73
+ require "colorize"
74
+ super(*args)
75
+ rescue LoadError
76
+ self
77
+ end
78
+ end
@@ -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
- File.basename(file, '.rb').camelize
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.new(models_paths.count)
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, "*.rb"))
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 but not yet saved (pass a hash with key names matching the associated table column names).
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>ActiveRecord::RecordNotFound</tt> error.
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,9 +1,14 @@
1
1
  module ActiveMocker
2
2
  class NullProgress
3
- def initialize(*)
4
3
 
4
+ def self.create(*)
5
+ self.new
5
6
  end
7
+
8
+ def initialize(*)
9
+ end
10
+
6
11
  def increment
7
12
  end
8
13
  end
9
- end
14
+ end
@@ -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
- @progress = ProgressBar.create(title: 'Generating Mocks',
5
- total: count,
6
- format: '%t |%b>>%i| %p%%')
12
+ @count = count
7
13
  end
8
14
 
9
15
  def increment
10
- @progress.increment
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
@@ -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 = ENV["MODEL"] if ENV["MODEL"]
7
- c.progress_bar = false if ENV["MUTE_PROGRESS_BAR"]
8
- c.error_verbosity = ENV["ERROR_VERBOSITY"].to_i if ENV["ERROR_VERBOSITY"]
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).gsub(/\n{2,5}/, "\n\n")
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
@@ -1,3 +1,3 @@
1
1
  module ActiveMocker
2
- VERSION = "2.0.0"
2
+ VERSION = "2.1.0"
3
3
  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.0.0
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: 2015-12-23 00:00:00.000000000 Z
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.6
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.6
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