active_mocker 1.7.1 → 1.7.2

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: 7d2e168d3b347ede9eb7102a8f4af8ae6109389c
4
- data.tar.gz: 453ba318bda8c12fd90f95ab46bb12ef3a2d77bf
3
+ metadata.gz: 70b459ba8b9483e0ad60206fbdc59bcabb9d53b1
4
+ data.tar.gz: c1477e53e36c6d5897864788adcade51e62016a4
5
5
  SHA512:
6
- metadata.gz: c980a6e65f99570348072d5fb82853d4efdbcee9e615d3bd2389e22ba3e63ed4fbefaab5c01a9c72406e9edbc71442a845ad30da015cdecef11f93e7d928b835
7
- data.tar.gz: 2fbe13cca919dacf3a39b91d4001fd728de86a6e84b90316125bae986715bfdb111bfd8ed6264fa5868a0be4fe6b80aa18a49a9ab7e97df3361246ff92489d6d
6
+ metadata.gz: bfb38756e480aa1ef4daa7f0e3891949837ae1a659b98a76dd9dbae3862066610071118781e24e4f36d3a138a6e42990e727e23a0735ced9460f171debba9c36
7
+ data.tar.gz: f43a9a48f74cec85c015727c1108fc765d92dec0b1ed27b694cce7a2b87dca37eec962018560564024bbbb0ba0bede939a1cbbbc3780823c154641b4b623575b
@@ -1,6 +1,14 @@
1
1
  # Changelog
2
2
  All notable changes to this project will be documented in this file.
3
3
 
4
+ ## 1.7.2 - 2014-10-27
5
+
6
+ ### Enhancement
7
+ - Build command can take aa Environment variable of model to just generate a mock for a specific model. `rake active_mocker:build MODEL=user`
8
+ - Add Method #freeze, see Doc for limitation.
9
+ - Method `find` can take a string number or an array of string numbers
10
+ - Query #all can take take method []
11
+
4
12
  ## 1.7.1 - 2014-10-20
5
13
 
6
14
  ### Enhancement
@@ -10,7 +10,8 @@ module ActiveMocker
10
10
  :file_reader,
11
11
  :build_in_progress,
12
12
  :log_location,
13
- :clear_log_on_build
13
+ :clear_log_on_build,
14
+ :generate_for_mock
14
15
 
15
16
  def model_base_classes=(val)
16
17
  @model_base_classes = val
@@ -29,6 +30,7 @@ module ActiveMocker
29
30
  @file_reader = FileReader unless @file_reader
30
31
  @log_location = 'log/active_mocker.log' unless @log_location
31
32
  @clear_log_on_build = true
33
+ @generate_for_mock = ENV['MODEL']
32
34
  rails_defaults if Object.const_defined?('Rails')
33
35
  end
34
36
 
@@ -9,7 +9,9 @@ class Generate
9
9
  def initialize(silence: false)
10
10
  @silence = silence
11
11
  Config.clear_log
12
+ Config.build_in_progress = true
12
13
  create_template
14
+ Config.build_in_progress = false
13
15
  end
14
16
 
15
17
  private
@@ -34,41 +36,73 @@ class Generate
34
36
  progress.increment unless silence
35
37
  end
36
38
 
39
+ attr_accessor :mocks_created, :not_valid_models
37
40
  def create_template
38
- Config.build_in_progress = true
39
- mocks_created = 0
40
- not_valid_models = 0
41
- FileUtils.rm_rf("#{Config.mock_dir}/", secure: true)
42
- FileUtils::mkdir_p Config.mock_dir unless File.directory? Config.mock_dir
41
+ @mocks_created = 0
42
+ @not_valid_models = 0
43
+ clean_up
43
44
  generate_model_schema.each do |model|
44
- if model.class.ancestors.include?(Exception)
45
- Config.logger.info "#{model}"
46
- not_valid_models += 1 if model.class == ModelLoadError::HasNoParentClass
47
- next
48
- end
49
-
50
- begin
51
- klass_str = model.render(File.open(File.join(File.expand_path('../', __FILE__), 'mock_template.erb')).read, mock_append_name)
52
-
53
- File.open(File.join(Config.mock_dir,"#{model.class_name.tableize.singularize}_mock.rb"), 'w').write(klass_str)
54
- Config.logger.info "saving mock #{model.class_name} to #{Config.mock_dir}"
55
-
56
- rescue Exception => exception
57
- Config.logger.debug $!.backtrace
58
- Config.logger.debug exception
59
- Config.logger.info "Failed to load #{model.class_name} model."
60
- next
61
- end
62
- mocks_created += 1
63
- increment_progress
45
+ next unless generate_and_rescue(model)
46
+ end
47
+ exit_message
48
+ end
49
+
50
+ def generate_and_rescue(model)
51
+ begin
52
+ raise_if_is_exception!(model)
53
+ generate_mock(model)
54
+ increase_mocks_created
55
+ rescue Exception => exception
56
+ log_failed_mock(exception, model)
57
+ false
64
58
  end
59
+ end
60
+
61
+ def increase_mocks_created
62
+ @mocks_created += 1
63
+ increment_progress
64
+ end
65
+
66
+ def raise_if_is_exception!(model)
67
+ raise model if model.class.ancestors.include?(Exception)
68
+ end
69
+
70
+ def exit_message
65
71
  progress.finish unless silence
66
72
  Config.logger.info "Generated #{mocks_created} of #{model_count} mocks."
67
73
  failed_mocks = (model_count - not_valid_models) - mocks_created
68
74
  if failed_mocks > 0
69
75
  puts "#{failed_mocks} mock(s) out of #{model_count} failed. See `log/active_mocker.log` for more info."
70
76
  end
71
- Config.build_in_progress = false
77
+ end
78
+
79
+ def generate_mock(model)
80
+ klass_str = generate_mock_string(model)
81
+ save_mock_file(klass_str, model)
82
+ log_save(model)
83
+ end
84
+
85
+ def log_failed_mock(exception, model)
86
+ Config.logger.debug $!.backtrace
87
+ Config.logger.debug exception
88
+ Config.logger.info "Failed to load #{model.class_name} model."
89
+ end
90
+
91
+ def log_save(model)
92
+ Config.logger.info "saving mock #{model.class_name} to #{Config.mock_dir}"
93
+ end
94
+
95
+ def save_mock_file(klass_str, model)
96
+ File.open(File.join(Config.mock_dir, "#{model.class_name.tableize.singularize}_mock.rb"), 'w').write(klass_str)
97
+ end
98
+
99
+ def generate_mock_string(model)
100
+ model.render(File.open(File.join(File.expand_path('../', __FILE__), 'mock_template.erb')).read, mock_append_name)
101
+ end
102
+
103
+ def clean_up
104
+ FileUtils.rm_rf("#{Config.mock_dir}/", secure: true) unless Config.generate_for_mock
105
+ FileUtils::mkdir_p Config.mock_dir unless File.directory? Config.mock_dir
72
106
  end
73
107
 
74
108
  def mock_append_name
@@ -275,6 +275,15 @@ class Base
275
275
  ObjectInspect.new(self.class.name, attributes).to_s
276
276
  end
277
277
 
278
+ # Will not allow attributes to be changed
279
+ #
280
+ # Will freeze attributes forever. Querying for the record again will not unfreeze it because records exist in memory
281
+ # and are not initialized upon a query. This behaviour differs from ActiveRecord, beware of any side effect this may
282
+ # have when using this method.
283
+ def freeze
284
+ @attributes.freeze; self
285
+ end
286
+
278
287
  module PropertiesGetterAndSetter
279
288
 
280
289
  # Returns the value of the attribute identified by <tt>attr_name</tt> after
@@ -16,7 +16,7 @@ module Mock
16
16
  end
17
17
 
18
18
  extend ::Forwardable
19
- def_delegators :@collection, :take, :push, :clear, :first, :last, :concat, :replace, :uniq, :count, :size, :length, :empty?, :any?, :many?, :include?, :delete
19
+ def_delegators :@collection, :[], :take, :push, :clear, :first, :last, :concat, :replace, :uniq, :count, :size, :length, :empty?, :any?, :many?, :include?, :delete
20
20
  alias_method :distinct, :uniq
21
21
 
22
22
  def select(&block)
@@ -118,10 +118,10 @@ module Mock
118
118
  # Person.find([7, 17]) # returns an array for objects with IDs in (7, 17)
119
119
  # Person.find([1]) # returns an array for the object with ID = 1
120
120
  #
121
- # <tt>ActiveRecord::RecordNotFound</tt> will be raised if one or more ids are not found.
121
+ # <tt>ActiveMocker::Mock::RecordNotFound</tt> will be raised if one or more ids are not found.
122
122
  def find(ids)
123
123
  results = [*ids].map do |id|
124
- find_by!(id: id)
124
+ find_by!(id: id.to_i)
125
125
  end
126
126
  return new_relation(results) if ids.class == Array
127
127
  results.first
@@ -17,16 +17,19 @@ module ActiveMocker
17
17
 
18
18
  def sandbox_model
19
19
  source = RubyParse.new(read_file)
20
- if source.has_parent_class? && !Config.model_base_classes.include?(source.parent_class_name)
21
- @parent_class = source.parent_class_name
22
- end
23
-
24
- unless source.has_parent_class?
25
- raise ModelLoadError::HasNoParentClass.new("#{model_name}")
26
- end
20
+ has_no_parent_class!(source)
21
+ get_non_active_record_parent_class(source)
27
22
  source.modify_parent_class('ActiveMocker::ActiveRecord::Base')
28
23
  end
29
24
 
25
+ def get_non_active_record_parent_class(source)
26
+ @parent_class = source.parent_class_name unless Config.model_base_classes.include?(source.parent_class_name)
27
+ end
28
+
29
+ def has_no_parent_class!(source)
30
+ raise ModelLoadError::HasNoParentClass.new("#{model_name}") unless source.has_parent_class?
31
+ end
32
+
30
33
  def module_namespace
31
34
  @module ||= Module.new
32
35
  end
@@ -28,8 +28,6 @@ module ActiveMocker
28
28
  end
29
29
  end
30
30
 
31
-
32
-
33
31
  class ModelSchema
34
32
 
35
33
  class Assemble
@@ -185,14 +183,16 @@ module ActiveMocker
185
183
  public
186
184
 
187
185
  def models
188
- Dir["#{Config.model_dir}/*.rb"].map do |file|
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
189
  Pathname.new(file).basename.to_s.sub('.rb', '')
190
190
  end
191
191
  end
192
192
 
193
193
  def get_model(model_file_name)
194
194
  model = ModelReader.new.parse(model_file_name)
195
- raise ModelLoadError::General.new(model_file_name) unless model unless model
195
+ raise ModelLoadError::General.new(model_file_name) unless model
196
196
  model
197
197
  end
198
198
 
@@ -6,7 +6,11 @@ module ActiveMocker
6
6
  end
7
7
 
8
8
  def read(path)
9
- @read
9
+ if @read.is_a?(Hash)
10
+ @read[path.sub('/','').sub('.rb','')]
11
+ else
12
+ @read
13
+ end
10
14
  end
11
15
  end
12
16
 
@@ -1,3 +1,3 @@
1
1
  module ActiveMocker
2
- VERSION = "1.7.1"
2
+ VERSION = "1.7.2"
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: 1.7.1
4
+ version: 1.7.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dustin Zeisler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-20 00:00:00.000000000 Z
11
+ date: 2014-10-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -178,34 +178,6 @@ dependencies:
178
178
  - - "~>"
179
179
  - !ruby/object:Gem::Version
180
180
  version: '3.5'
181
- - !ruby/object:Gem::Dependency
182
- name: mutant
183
- requirement: !ruby/object:Gem::Requirement
184
- requirements:
185
- - - "~>"
186
- - !ruby/object:Gem::Version
187
- version: '0.5'
188
- type: :development
189
- prerelease: false
190
- version_requirements: !ruby/object:Gem::Requirement
191
- requirements:
192
- - - "~>"
193
- - !ruby/object:Gem::Version
194
- version: '0.5'
195
- - !ruby/object:Gem::Dependency
196
- name: mutant-rspec
197
- requirement: !ruby/object:Gem::Requirement
198
- requirements:
199
- - - "~>"
200
- - !ruby/object:Gem::Version
201
- version: '0.5'
202
- type: :development
203
- prerelease: false
204
- version_requirements: !ruby/object:Gem::Requirement
205
- requirements:
206
- - - "~>"
207
- - !ruby/object:Gem::Version
208
- version: '0.5'
209
181
  - !ruby/object:Gem::Dependency
210
182
  name: fuubar
211
183
  requirement: !ruby/object:Gem::Requirement