active_mocker 2.1.3 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +13 -0
  3. data/README.md +5 -0
  4. data/lib/active_mocker.rb +7 -6
  5. data/lib/active_mocker/config.rb +6 -8
  6. data/lib/active_mocker/deprecated_components/mock_abilities.rb +6 -10
  7. data/lib/active_mocker/deprecated_components/rspec.rb +2 -1
  8. data/lib/active_mocker/display_errors.rb +3 -2
  9. data/lib/active_mocker/error_object.rb +13 -13
  10. data/lib/active_mocker/file_path_to_ruby_class.rb +2 -3
  11. data/lib/active_mocker/file_writer.rb +125 -0
  12. data/lib/active_mocker/generate.rb +56 -71
  13. data/lib/active_mocker/hash_new_style.rb +2 -1
  14. data/lib/active_mocker/loaded_mocks.rb +9 -11
  15. data/lib/active_mocker/mock.rb +28 -26
  16. data/lib/active_mocker/mock/alias_attribute.rb +19 -0
  17. data/lib/active_mocker/mock/association.rb +2 -1
  18. data/lib/active_mocker/mock/base.rb +22 -20
  19. data/lib/active_mocker/mock/belongs_to.rb +1 -1
  20. data/lib/active_mocker/mock/collection.rb +6 -6
  21. data/lib/active_mocker/mock/do_nothing_active_record_methods.rb +4 -7
  22. data/lib/active_mocker/mock/exceptions.rb +1 -4
  23. data/lib/active_mocker/mock/has_and_belongs_to_many.rb +2 -1
  24. data/lib/active_mocker/mock/has_many.rb +6 -7
  25. data/lib/active_mocker/mock/has_one.rb +2 -2
  26. data/lib/active_mocker/mock/hash_process.rb +2 -2
  27. data/lib/active_mocker/mock/mock_relation.rb +1 -0
  28. data/lib/active_mocker/mock/object_inspect.rb +2 -2
  29. data/lib/active_mocker/mock/queries.rb +12 -16
  30. data/lib/active_mocker/mock/records.rb +3 -3
  31. data/lib/active_mocker/mock/relation.rb +5 -5
  32. data/lib/active_mocker/mock/single_relation.rb +2 -4
  33. data/lib/active_mocker/mock/template_methods.rb +3 -5
  34. data/lib/active_mocker/mock_creator.rb +78 -22
  35. data/lib/active_mocker/mock_template.erb +1 -0
  36. data/lib/active_mocker/mock_template/_recreate_class_method_calls.erb +12 -0
  37. data/lib/active_mocker/null_progress.rb +2 -2
  38. data/lib/active_mocker/parent_class.rb +4 -2
  39. data/lib/active_mocker/progress.rb +6 -8
  40. data/lib/active_mocker/public_methods.rb +3 -2
  41. data/lib/active_mocker/railtie.rb +3 -3
  42. data/lib/active_mocker/rspec.rb +2 -1
  43. data/lib/active_mocker/rspec_helper.rb +4 -3
  44. data/lib/active_mocker/task.rake +9 -10
  45. data/lib/active_mocker/template_creator.rb +5 -5
  46. data/lib/active_mocker/version.rb +2 -1
  47. metadata +25 -8
@@ -1,7 +1,8 @@
1
+ # frozen_string_literal: true
1
2
  module ActiveMocker
2
3
  class HashNewStyle < Hash
3
4
  def inspect
4
- '{ ' + self.map { |name, type| "#{name}: #{type}" }.join(', ') + ' }'
5
+ "{ " + map { |name, type| "#{name}: #{type}" }.join(", ") + " }"
5
6
  end
6
7
  end
7
8
  end
@@ -1,7 +1,7 @@
1
+ # frozen_string_literal: true
1
2
  module ActiveMocker
2
3
  class LoadedMocks
3
4
  class << self
4
-
5
5
  extend Forwardable
6
6
  def_delegators :mocks, :find, :delete_all
7
7
 
@@ -17,20 +17,19 @@ module ActiveMocker
17
17
  end
18
18
 
19
19
  # @deprecated Use {#mocks} instead of this method.
20
- alias_method :class_name_to_mock, :mocks
20
+ alias class_name_to_mock mocks
21
21
 
22
22
  # @deprecated Use {#mocks} instead of this method.
23
- alias_method :all, :mocks
23
+ alias all mocks
24
24
 
25
25
  # @deprecated Use {#delete_all} instead of this method.
26
- alias_method :clear_all, :delete_all
26
+ alias clear_all delete_all
27
27
 
28
28
  class Collection
29
-
30
29
  include Enumerable
31
30
 
32
31
  # @option opts [Hash] hash
33
- def initialize(hash={})
32
+ def initialize(hash = {})
34
33
  @hash = Hash[hash]
35
34
  end
36
35
 
@@ -70,21 +69,21 @@ module ActiveMocker
70
69
  def mocks
71
70
  hash.values
72
71
  end
73
- alias_method :values, :mocks
72
+ alias values mocks
74
73
 
75
74
  private
75
+
76
76
  attr_reader :hash
77
77
 
78
78
  def get_item(args, k, v)
79
79
  args.map do |e|
80
- if [:to_str, :to_sym].any?{|i| e.respond_to? i}
80
+ if [:to_str, :to_sym].any? { |i| e.respond_to? i }
81
81
  e.to_s == k
82
82
  else
83
83
  e == v
84
84
  end
85
85
  end.any? { |a| a }
86
86
  end
87
-
88
87
  end
89
88
 
90
89
  private
@@ -94,9 +93,8 @@ module ActiveMocker
94
93
  end
95
94
 
96
95
  def add(mocks_to_add)
97
- mocks_store.merge!({ mocks_to_add.name => mocks_to_add })
96
+ mocks_store.merge!(mocks_to_add.name => mocks_to_add)
98
97
  end
99
-
100
98
  end
101
99
  end
102
100
  end
@@ -1,27 +1,29 @@
1
- require 'active_support/deprecation'
2
- require 'active_support/dependencies/autoload'
3
- require 'active_support/hash_with_indifferent_access'
4
- require 'active_support/core_ext/module/delegation'
5
- require 'active_support/inflector'
6
- require 'active_support/core_ext'
7
- require 'virtus'
1
+ # frozen_string_literal: true
2
+ require "active_support/deprecation"
3
+ require "active_support/dependencies/autoload"
4
+ require "active_support/hash_with_indifferent_access"
5
+ require "active_support/core_ext/module/delegation"
6
+ require "active_support/inflector"
7
+ require "active_support/core_ext"
8
+ require "virtus"
8
9
 
9
- require 'active_mocker/version'
10
- require 'active_mocker/loaded_mocks'
11
- require 'active_mocker/mock/hash_process'
12
- require 'active_mocker/mock/collection'
13
- require 'active_mocker/mock/queries'
14
- require 'active_mocker/mock/relation'
15
- require 'active_mocker/mock/mock_relation'
16
- require 'active_mocker/mock/association'
17
- require 'active_mocker/mock/has_many'
18
- require 'active_mocker/mock/single_relation'
19
- require 'active_mocker/mock/has_one'
20
- require 'active_mocker/mock/has_and_belongs_to_many'
21
- require 'active_mocker/mock/belongs_to'
22
- require 'active_mocker/mock/exceptions'
23
- require 'active_mocker/mock/template_methods'
24
- require 'active_mocker/mock/do_nothing_active_record_methods'
25
- require 'active_mocker/mock/records'
26
- require 'active_mocker/mock/object_inspect'
27
- require 'active_mocker/mock/base'
10
+ require "active_mocker/version"
11
+ require "active_mocker/loaded_mocks"
12
+ require "active_mocker/mock/hash_process"
13
+ require "active_mocker/mock/collection"
14
+ require "active_mocker/mock/queries"
15
+ require "active_mocker/mock/relation"
16
+ require "active_mocker/mock/mock_relation"
17
+ require "active_mocker/mock/association"
18
+ require "active_mocker/mock/has_many"
19
+ require "active_mocker/mock/single_relation"
20
+ require "active_mocker/mock/has_one"
21
+ require "active_mocker/mock/has_and_belongs_to_many"
22
+ require "active_mocker/mock/belongs_to"
23
+ require "active_mocker/mock/exceptions"
24
+ require "active_mocker/mock/template_methods"
25
+ require "active_mocker/mock/do_nothing_active_record_methods"
26
+ require "active_mocker/mock/records"
27
+ require "active_mocker/mock/object_inspect"
28
+ require "active_mocker/mock/alias_attribute"
29
+ require "active_mocker/mock/base"
@@ -0,0 +1,19 @@
1
+ module ActiveMocker
2
+ module AliasAttribute
3
+ # Is +new_name+ an alias?
4
+ def attribute_alias?(new_name)
5
+ attribute_aliases.key? new_name.to_s
6
+ end
7
+
8
+ # Returns the original name for the alias +name+
9
+ def attribute_alias(name)
10
+ attribute_aliases[name.to_s]
11
+ end
12
+
13
+ private
14
+
15
+ def attribute_aliases
16
+ @attribute_aliases ||= {}
17
+ end
18
+ end
19
+ end
@@ -1,4 +1,5 @@
1
+ # frozen_string_literal: true
1
2
  module ActiveMocker
2
3
  class Association < Relation
3
4
  end
4
- end
5
+ end
@@ -1,15 +1,16 @@
1
+ # frozen_string_literal: true
1
2
  module ActiveMocker
2
3
  class Base
3
4
  include DoNothingActiveRecordMethods
4
5
  include TemplateMethods
5
6
  extend Queries
7
+ extend AliasAttribute
6
8
 
7
9
  def self.inherited(subclass)
8
10
  return ActiveMocker::LoadedMocks.send(:add, subclass) if subclass.superclass == Base
9
11
  end
10
12
 
11
13
  class << self
12
-
13
14
  # Creates an object (or multiple objects) and saves it to memory.
14
15
  #
15
16
  # The +attributes+ parameter can be either a Hash or an Array of Hashes. These Hashes describe the
@@ -43,7 +44,7 @@ module ActiveMocker
43
44
  end
44
45
  end
45
46
 
46
- alias_method :create!, :create
47
+ alias create! create
47
48
 
48
49
  def records
49
50
  @records ||= Records.new
@@ -51,8 +52,8 @@ module ActiveMocker
51
52
 
52
53
  private :records
53
54
 
54
- delegate :insert, :exists?, :to_a, :to => :records
55
- delegate :first, :last, :to => :all
55
+ delegate :insert, :exists?, :to_a, to: :records
56
+ delegate :first, :last, to: :all
56
57
 
57
58
  # Delete an object (or multiple objects) that has the given id.
58
59
  #
@@ -78,17 +79,17 @@ module ActiveMocker
78
79
  end
79
80
  end
80
81
 
81
- alias_method :destroy, :delete
82
+ alias destroy delete
82
83
 
83
84
  # Deletes the records matching +conditions+.
84
85
  #
85
86
  # Post.where(person_id: 5).where(category: ['Something', 'Else']).delete_all
86
- def delete_all(conditions=nil)
87
+ def delete_all(conditions = nil)
87
88
  return records.reset if conditions.nil?
88
89
  super
89
90
  end
90
91
 
91
- alias_method :destroy_all, :delete_all
92
+ alias destroy_all delete_all
92
93
 
93
94
  # @api private
94
95
  def from_limit?
@@ -130,12 +131,12 @@ module ActiveMocker
130
131
  private
131
132
 
132
133
  def created_with(version)
133
- raise UpdateMocksError.new(self.name, version, ActiveMocker::VERSION) if version != ActiveMocker::VERSION
134
+ raise UpdateMocksError.new(name, version, ActiveMocker::VERSION) if version != ActiveMocker::VERSION
134
135
  end
135
136
 
136
137
  # @deprecated
137
138
  def call_mock_method(method:, caller:, arguments: [])
138
- is_implemented(method, '::', caller)
139
+ is_implemented(method, "::", caller)
139
140
  end
140
141
 
141
142
  # @deprecated
@@ -183,12 +184,12 @@ module ActiveMocker
183
184
 
184
185
  private :setup_instance_variables
185
186
 
186
- def update(attributes={})
187
+ def update(attributes = {})
187
188
  assign_attributes(attributes)
188
189
  end
189
190
 
190
191
  # @api private
191
- def assign_attributes(new_attributes, &block)
192
+ def assign_attributes(new_attributes)
192
193
  yield self if block_given?
193
194
  unless new_attributes.respond_to?(:stringify_keys)
194
195
  raise ArgumentError, "When assigning attributes, you must pass a hash as an argument."
@@ -213,10 +214,8 @@ module ActiveMocker
213
214
  end
214
215
  end
215
216
 
216
- def save(*args)
217
- unless self.class.exists?(self)
218
- self.class.send(:insert, self)
219
- end
217
+ def save(*_args)
218
+ self.class.send(:insert, self) unless self.class.exists?(self)
220
219
  true
221
220
  end
222
221
 
@@ -232,7 +231,7 @@ module ActiveMocker
232
231
  records.delete(self)
233
232
  end
234
233
 
235
- alias_method :destroy, :delete
234
+ alias destroy delete
236
235
 
237
236
  delegate :[], :[]=, to: :attributes
238
237
 
@@ -256,7 +255,7 @@ module ActiveMocker
256
255
  # person.has_attribute?('age') # => true
257
256
  # person.has_attribute?(:nothing) # => false
258
257
  def has_attribute?(attr_name)
259
- @attributes.has_key?(attr_name.to_s)
258
+ @attributes.key?(attr_name.to_s)
260
259
  end
261
260
 
262
261
  # Returns +true+ if the specified +attribute+ has been set and is neither +nil+ nor <tt>empty?</tt> (the latter only applies
@@ -275,6 +274,11 @@ module ActiveMocker
275
274
  !value.nil? && !(value.respond_to?(:empty?) && value.empty?)
276
275
  end
277
276
 
277
+ # Returns a hash of the given methods with their names as keys and returned values as values.
278
+ def slice(*methods)
279
+ Hash[methods.map! { |method| [method, public_send(method)] }].with_indifferent_access
280
+ end
281
+
278
282
  # Returns an array of names for the attributes available on this object.
279
283
  #
280
284
  # person = Person.new
@@ -298,7 +302,6 @@ module ActiveMocker
298
302
  end
299
303
 
300
304
  module PropertiesGetterAndSetter
301
-
302
305
  # Returns the value of the attribute identified by <tt>attr_name</tt> after
303
306
  # it has been typecast (for example, "2004-12-12" in a date column is cast
304
307
  # to a date object, like Date.new(2004, 12, 12))
@@ -314,7 +317,7 @@ module ActiveMocker
314
317
  end
315
318
 
316
319
  # @api private
317
- def read_association(attr, assign_if_value_nil=nil)
320
+ def read_association(attr, assign_if_value_nil = nil)
318
321
  @associations[attr.to_sym] ||= assign_if_value_nil.try(:call)
319
322
  end
320
323
 
@@ -324,7 +327,6 @@ module ActiveMocker
324
327
  end
325
328
 
326
329
  protected :read_attribute, :write_attribute, :read_association, :write_association
327
-
328
330
  end
329
331
 
330
332
  include PropertiesGetterAndSetter
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module ActiveMocker
2
3
  class BelongsTo < SingleRelation
3
4
  attr_reader :item
@@ -20,4 +21,3 @@ module ActiveMocker
20
21
  end
21
22
  end
22
23
  end
23
-
@@ -1,14 +1,14 @@
1
- require 'forwardable'
1
+ # frozen_string_literal: true
2
+ require "forwardable"
2
3
 
3
4
  module ActiveMocker
4
5
  class Collection
5
6
  include Enumerable
6
7
  extend ::Forwardable
7
8
  def_delegators :@collection, :[], :take, :push, :clear, :first, :last, :concat, :replace, :uniq, :count, :size, :length, :empty?, :any?, :many?, :include?, :delete
8
- alias_method :distinct, :uniq
9
+ alias distinct uniq
9
10
 
10
-
11
- def initialize(collection=[])
11
+ def initialize(collection = [])
12
12
  @collection = [*collection]
13
13
  end
14
14
 
@@ -16,7 +16,7 @@ module ActiveMocker
16
16
  collection.concat(records.flatten)
17
17
  end
18
18
 
19
- def each(&block)
19
+ def each
20
20
  collection.each do |item|
21
21
  yield(item)
22
22
  end
@@ -46,4 +46,4 @@ module ActiveMocker
46
46
 
47
47
  attr_accessor :collection
48
48
  end
49
- end
49
+ end
@@ -1,12 +1,11 @@
1
+ # frozen_string_literal: true
1
2
  module ActiveMocker
2
3
  module DoNothingActiveRecordMethods
3
-
4
4
  def self.included(base)
5
5
  base.extend(ClassMethods)
6
6
  end
7
7
 
8
8
  module ClassMethods
9
-
10
9
  def transaction
11
10
  yield
12
11
  rescue LocalJumpError => err
@@ -18,7 +17,6 @@ module ActiveMocker
18
17
  def column_names
19
18
  attribute_names
20
19
  end
21
-
22
20
  end
23
21
 
24
22
  def readonly?
@@ -28,11 +26,11 @@ module ActiveMocker
28
26
  def errors
29
27
  obj = Object.new
30
28
 
31
- def obj.[](key)
29
+ def obj.[](_key)
32
30
  []
33
31
  end
34
32
 
35
- def obj.full_messages()
33
+ def obj.full_messages
36
34
  []
37
35
  end
38
36
 
@@ -54,6 +52,5 @@ module ActiveMocker
54
52
  def reload
55
53
  self
56
54
  end
57
-
58
55
  end
59
- end
56
+ end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module ActiveMocker
2
3
  class RecordNotFound < StandardError
3
4
  end
@@ -12,7 +13,6 @@ module ActiveMocker
12
13
 
13
14
  # Raised when unknown attributes are supplied via mass assignment.
14
15
  class UnknownAttributeError < NoMethodError
15
-
16
16
  attr_reader :record, :attribute
17
17
 
18
18
  def initialize(record, attribute)
@@ -20,15 +20,12 @@ module ActiveMocker
20
20
  @attribute = attribute.to_s
21
21
  super("unknown attribute: #{attribute}")
22
22
  end
23
-
24
23
  end
25
24
 
26
25
  class UpdateMocksError < Exception
27
-
28
26
  def initialize(name, mock_version, gem_version)
29
27
  super("#{name} was built with #{mock_version} but the gem version is #{gem_version}. Run `rake active_mocker:build` to update.")
30
28
  end
31
-
32
29
  end
33
30
 
34
31
  class NotImplementedError < Exception
@@ -1,4 +1,5 @@
1
+ # frozen_string_literal: true
1
2
  module ActiveMocker
2
3
  class HasAndBelongsToMany < HasMany
3
4
  end
4
- end
5
+ end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module ActiveMocker
2
3
  class HasMany < Association
3
4
  include Queries
@@ -7,7 +8,7 @@ module ActiveMocker
7
8
  super(collection, options)
8
9
  end
9
10
 
10
- def initialize(collection, options={})
11
+ def initialize(collection, options = {})
11
12
  @relation_class = options[:relation_class]
12
13
  @foreign_key = options[:foreign_key]
13
14
  @foreign_id = options[:foreign_id]
@@ -26,7 +27,7 @@ module ActiveMocker
26
27
  # @api private
27
28
  attr_reader :relation_class, :foreign_key, :foreign_id, :source
28
29
 
29
- def build(options={}, &block)
30
+ def build(options = {}, &block)
30
31
  new_record = relation_class.new(init_options.merge!(options), &block)
31
32
 
32
33
  # @private
@@ -45,23 +46,21 @@ module ActiveMocker
45
46
  new_record
46
47
  end
47
48
 
48
- def create(options={}, &block)
49
+ def create(options = {}, &block)
49
50
  created_record = relation_class.create(init_options.merge!(options), &block)
50
51
  collection << created_record
51
52
  created_record
52
53
  end
53
54
 
54
- alias_method :create!, :create
55
+ alias create! create
55
56
 
56
57
  # @api private
57
58
  def init_options
58
- {foreign_key => foreign_id}
59
+ { foreign_key => foreign_id }
59
60
  end
60
-
61
61
  end
62
62
  module Mock
63
63
  # @deprecated
64
64
  HasMany = ActiveMocker::HasMany
65
65
  end
66
66
  end
67
-