active_mocker 2.5.4 → 2.6.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ee07a0c316a8e30d793536c32edcb6ffc1ff0d3a
4
- data.tar.gz: 2c7938f5ea726a3ddb388e6b65547df17bc5ce2f
3
+ metadata.gz: 9486fcafe8737ffaf943ef442d155de53152ac51
4
+ data.tar.gz: 1b70a0a54a57e9b093b9a010580403baa015ff0f
5
5
  SHA512:
6
- metadata.gz: 2909eac242da2763b902c05af8e462e49ee256640cc791716a76bde87d56dc7c2d3e2fb750996d77de865ec4bc0eb2d20d34bdd1c2e0cf343ebdb1f6d9231d71
7
- data.tar.gz: 59177e2d5513705962e95ba9c0784c5436299b38736cb3a660bd58d8e265a03d77a5d84e792d78a285d44d229cebdb3c91ad5f052bf755fbae58989450ba6f16
6
+ metadata.gz: b5fee7877de2d5f3c1fa342430a0607ea27d7fcb7936b74d2db2a5e9b6f6958f99c901477648968c3cebfc372cf4370fb6c266b68e8f748e790da114676ce745
7
+ data.tar.gz: 4dd3a0f9b59357d078e603457c34a3379f8b9548edb98e22b2dda6644742b848129e2bcb0a447f82223df4d54d13ea416979ab6cb250edba8a5382d144941112
@@ -1,6 +1,41 @@
1
1
  # Changelog
2
2
  All notable changes to this project will be documented in this file.
3
3
 
4
+ ## 2.6.0 - 2017-12-01
5
+ ### Feature
6
+ - Relation#order now accepts all non-SQL arguments
7
+
8
+ Example usage:
9
+ ```ruby
10
+ User.order('name')
11
+
12
+ User.order(:name)
13
+
14
+ User.order(email: :desc)
15
+
16
+ User.order(:name, email: :desc)
17
+
18
+ User.order(name: :desc, email: :asc)
19
+ ```
20
+
21
+ ### Enhancement
22
+ - Improved output for `rake active_mocker:build`. When all mocks built successfully the messaging could be misleading.
23
+ - Error message for calling stubbed methods better formats RSpec syntax for variable names.
24
+ - Now whitelist all locally defined methods as safe to copy/run within mock
25
+ ```ruby
26
+ # ActiveMocker.all_methods_safe
27
+ class User < ActiveRecord::Base
28
+ end
29
+ ```
30
+
31
+ OR blacklist methods from being safe.
32
+
33
+ ```ruby
34
+ # ActiveMocker.all_methods_safe except: { instance_methods: [], class_methods: [], scopes: [] }
35
+ class User < ActiveRecord::Base
36
+ end
37
+ ```
38
+
4
39
  ## 2.5.4 - 2017-11-17
5
40
  ### Fix
6
41
  - has_and_belongs_to_many association did not contain scoped methods.
@@ -44,19 +44,22 @@ module ActiveMocker
44
44
  end
45
45
 
46
46
  def error_summary
47
- display "Error Summary"
48
47
  display "errors: #{error_count}, warn: #{warn}, info: #{info}"
49
48
  display "Failed models: #{failed_models.join(", ")}" if failed_models.count > 0
50
49
  end
51
50
 
52
- def failure_count_message
51
+ def number_models_mocked
53
52
  if success_count < model_count || any_errors?
54
- display "#{model_count - success_count} mock(s) out of #{model_count} failed."
53
+ display "Mocked #{success_count} ActiveRecord #{plural("Model", success_count)} out of #{model_count} #{plural("file", model_count)}."
55
54
  end
56
55
  end
57
56
 
58
57
  private
59
58
 
59
+ def plural(string, count, plural="s")
60
+ count > 1 || count.zero? ? "#{string}#{plural}" : string
61
+ end
62
+
60
63
  def display(msg)
61
64
  out.puts(msg)
62
65
  end
@@ -95,10 +98,10 @@ module ActiveMocker
95
98
 
96
99
  error_summary if any_errors?
97
100
 
98
- failure_count_message
101
+ number_models_mocked
99
102
 
100
103
  return unless any_errors?
101
- display "To see more/less detail set error_verbosity = 0, 1, 2, 3"
104
+ display "To see more/less detail set ERROR_VERBOSITY = 0, 1, 2, 3"
102
105
  end
103
106
 
104
107
  def error_count
@@ -10,7 +10,7 @@ module ActiveMocker
10
10
  ["#{klass}_relation", klass.camelize, :scopes]
11
11
  elsif type == "#"
12
12
  klass = self.class.name
13
- ["#{klass.underscore}_record", klass, :instance_methods]
13
+ ["#{klass.underscore.split("/").first}_record", klass, :instance_methods]
14
14
  else
15
15
  [name, name, :class_methods]
16
16
  end
@@ -311,8 +311,50 @@ module ActiveMocker
311
311
  # User.order('name')
312
312
  #
313
313
  # User.order(:name)
314
- def order(key)
315
- __new_relation__(all.sort_by { |item| item.send(key) })
314
+ #
315
+ # User.order(email: :desc)
316
+ #
317
+ # User.order(:name, email: :desc)
318
+ def order(*args)
319
+ options = args.extract_options!
320
+ if options.empty? && args.count == 1
321
+ __new_relation__(all.sort_by { |item| item.send(args.first) })
322
+ else
323
+ __new_relation__(Sort.order_mixed_args(all, args, options))
324
+ end
325
+ end
326
+
327
+ module Sort
328
+ class DESC
329
+ attr_reader :r
330
+
331
+ def initialize(r)
332
+ @r = r
333
+ end
334
+
335
+ def <=>(other)
336
+ -(r <=> other.r) # Flip negative/positive result
337
+ end
338
+ end
339
+
340
+ class << self
341
+ def desc(r)
342
+ DESC.new(r)
343
+ end
344
+
345
+ def asc(r)
346
+ r
347
+ end
348
+
349
+ def order_mixed_args(all, args, options)
350
+ options.merge!(args.each_with_object({}) { |a, h| h[a] = :asc }) # Add non specified direction keys
351
+ all.sort { |a, b| build_order(a, options) <=> build_order(b, options) }
352
+ end
353
+
354
+ def build_order(a, options)
355
+ options.map { |k, v| send(v, a.send(k)) }
356
+ end
357
+ end
316
358
  end
317
359
 
318
360
  # Reverse the existing order clause on the relation.
@@ -24,7 +24,7 @@ module ActiveMocker
24
24
 
25
25
  def create_method(m, type)
26
26
  plural_type = (type.to_s + "s").to_sym
27
- if safe_methods[plural_type].include?(m)
27
+ if safe_method?(type, m)
28
28
  def_type = type == :method ? :class_defs : :defs
29
29
  def_method = class_introspector.parsed_source.public_send(def_type).detect { |meth| meth.name == m }
30
30
  raise "ActiveMocker.safe_methods is unable to find #{plural_type}: #{m}" unless def_method
@@ -2,11 +2,23 @@
2
2
  module ActiveMocker
3
3
  class MockCreator
4
4
  module SafeMethods
5
- BASE = { instance_methods: [], scopes: [], methods: [] }.freeze
5
+ BASE = { instance_methods: [], scopes: [], methods: [], all_methods_safe: false }.freeze
6
+
7
+ def safe_method?(type, name)
8
+ plural_type = (type.to_s + "s").to_sym
9
+ all_methods_safe = all_methods_safe?(type, name)
10
+ return true if all_methods_safe
11
+ return true if safe_methods[plural_type].include?(name)
12
+ false
13
+ end
14
+
15
+ private
6
16
 
7
17
  def safe_methods
8
18
  @safe_methods ||= class_introspector.parsed_source.comments.each_with_object(BASE.dup) do |comment, hash|
9
- if comment.text.include?("ActiveMocker.safe_methods")
19
+ if comment.text.include?("ActiveMocker.all_methods_safe")
20
+ hash[:all_methods_safe] = ActiveMocker.module_eval(comment.text.delete("#"))
21
+ elsif comment.text.include?("ActiveMocker.safe_methods")
10
22
  hash.merge!(ActiveMocker.module_eval(comment.text.delete("#")))
11
23
  else
12
24
  hash
@@ -14,9 +26,38 @@ module ActiveMocker
14
26
  end
15
27
  end
16
28
 
29
+ def all_methods_safe?(type, name)
30
+ plural_type = (type.to_s + "s").to_sym
31
+ all_methods_safe = safe_methods.fetch(:all_methods_safe)
32
+ if all_methods_safe.is_a?(Hash)
33
+ !all_methods_safe.fetch(plural_type).include?(name)
34
+ else
35
+ all_methods_safe
36
+ end
37
+ end
38
+
17
39
  module ActiveMocker
18
- def self.safe_methods(*arg_methods, scopes: [], instance_methods: [], class_methods: [])
19
- { instance_methods: arg_methods.concat(instance_methods), scopes: scopes, methods: class_methods }
40
+ class << self
41
+ def safe_methods(*arg_methods, scopes: [], instance_methods: [], class_methods: [], all_methods_safe: false)
42
+ {
43
+ instance_methods: arg_methods.concat(instance_methods),
44
+ scopes: scopes,
45
+ methods: class_methods,
46
+ all_methods_safe: all_methods_safe,
47
+ }
48
+ end
49
+
50
+ def all_methods_safe(except: {})
51
+ other_keys = except.except(:instance_methods, :scopes, :class_methods)
52
+ unless other_keys.empty?
53
+ raise ArgumentError, "ActiveMocker.all_methods_safe arguments must only be `except: { instance_methods: [], scopes: [], class_methods: [] }`"
54
+ end
55
+ {
56
+ instance_methods: except.fetch(:instance_methods, []),
57
+ scopes: except.fetch(:scopes, []),
58
+ methods: except.fetch(:class_methods, []),
59
+ }
60
+ end
20
61
  end
21
62
  end
22
63
  end
@@ -15,8 +15,8 @@ module ActiveMocker
15
15
  end
16
16
  end
17
17
 
18
- def scope_body(arguments, name)
19
- if safe_methods[:scopes].include?(name)
18
+ def scope_body(_arguments, name)
19
+ if safe_method?(:scope, name)
20
20
  find_scope_body_from_ast(name)
21
21
  else
22
22
  <<-METHOD
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
  module ActiveMocker
3
- VERSION = "2.5.4"
3
+ VERSION = "2.6.0"
4
4
  module Mock
5
5
  VERSION = "2" # This increments when breaking changes happen in the generated mocks
6
6
  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.5.4
4
+ version: 2.6.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: 2017-11-17 00:00:00.000000000 Z
11
+ date: 2017-12-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport