duck_puncher 4.4.1 → 4.4.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: 1fd23706fe9c393e764f8b626910f65387f4e61c
4
- data.tar.gz: 3bfa8e50421e3380e147cf4abf2a851667d15b12
3
+ metadata.gz: 62f4cac1d15e159d6ba58d71681cec7e34be8c71
4
+ data.tar.gz: 53ee95a89344c33f8960cf5d957cbb5dbf6d3562
5
5
  SHA512:
6
- metadata.gz: 95f234da65d3b1d75e20fad48d8c943b0521f9b7cb608ee737ab9b7da200e900c947e350c3b002dc95b33f7fa797e527a8b2f2f6bda6983ec1bc0d25226a2fac
7
- data.tar.gz: 0bb151f3465b618042896541a8b23d49803d3d25ee28b61ae757c8fca2f76aef396341ee5748acb1ac899b016b0d1b37719436702f37f04e8c77f9be1e6a2d80
6
+ metadata.gz: ea6d68c9efd49449907ccfb6aab5b810f6b0081ce399817afc78804a813cf0c66feea9ea0cfe74492e3b1091baa3011cc0cc1492d2bdb2ed6ca5e27433f9e9eb
7
+ data.tar.gz: dd5bf802c09382798460d26c81650588f10f64de8e2cba77fec53d82e55fffb932fe785178233037d96965b53b9093c7088e2c2f87c608c23828df71f81b05c8
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ 4.4.2 (01/10/2016)
2
+ ==================
3
+
4
+ * Fix glaring bug in Duck class where the `:only` option is ignored (since 4.4.0)
5
+
1
6
  4.4.1 (12/19/2016)
2
7
  ==================
3
8
 
data/README.md CHANGED
@@ -155,7 +155,11 @@ DuckPuncher allows you to utilize the `punch` and `punch!` interface to __decora
155
155
  call `DuckPuncher.register` with the name of your module (or an array of names) and any of
156
156
  [these options](https://github.com/ridiculous/duck_puncher/blob/master/lib/duck_puncher/duck.rb#L10).
157
157
 
158
+ ```ruby
159
+ DuckPuncher.register <class>, [<module>, ...]
160
+ ```
158
161
 
162
+ A full example:
159
163
  ```ruby
160
164
  # Define some extensions
161
165
  module Billable
@@ -181,7 +185,7 @@ class User < Struct.new(:name)
181
185
  end
182
186
 
183
187
  # Register the extensions
184
- DuckPuncher.register User, :Billable, :Retryable
188
+ DuckPuncher.register User, Billable, Retryable
185
189
 
186
190
  # Add the #punch method to User instances
187
191
  DuckPuncher.(Object, only: :punch)
data/bin/console CHANGED
@@ -2,9 +2,23 @@
2
2
 
3
3
  require 'bundler/setup'
4
4
  require 'pp'
5
- require 'duck_puncher'
6
5
  require 'irb'
7
6
  require 'byebug'
7
+
8
+ # Stub out Rails
9
+ module Rails
10
+ module VERSION
11
+ MAJOR = 4
12
+ end
13
+ end
14
+
15
+ # And AR
16
+ module ActiveRecord
17
+ class Base
18
+ end
19
+ end
20
+
21
+ require 'duck_puncher'
8
22
  require_relative '../test/fixtures/wut'
9
23
 
10
24
  DuckPuncher.logger.level = Logger::DEBUG
@@ -22,7 +22,7 @@ ducks = [
22
22
  [Module, DuckPuncher::Ducks::Module],
23
23
  [Method, DuckPuncher::Ducks::Method, { before: ->(_target) { DuckPuncher::GemInstaller.initialize! } }],
24
24
  ]
25
- ducks << ['ActiveRecord::Base', DuckPuncher::Ducks::ActiveRecord] if defined? ::ActiveRecord
25
+ ducks << [ActiveRecord::Base, DuckPuncher::Ducks::ActiveRecord] if defined?(Rails) && defined?(ActiveRecord) && defined?(ActiveRecord::Base)
26
26
  ducks.each do |duck|
27
27
  DuckPuncher.register *duck
28
28
  end
@@ -18,19 +18,23 @@ module DuckPuncher
18
18
  # @option options [Array,Symbol] :only Specifies the methods to extend onto the current object
19
19
  # @option options [Symbol,String] :method Specifies if the methods should be included or prepended (:include)
20
20
  # @return [Class] The class that was just punched
21
- def punch(opts = {})
21
+ def call(opts = {})
22
22
  opts = options.merge(opts)
23
23
  targets = Array(opts[:target] || self.target)
24
24
  targets.each do |target|
25
- options[:before].call(target) if options[:before]
25
+ opts[:before].call(target) if opts[:before]
26
26
  punches = Array(opts[:only] || Ducks::Module.instance_method(:local_methods).bind(mod).call)
27
+ unless target.is_a?(::Module)
28
+ fail ArgumentError, "Invalid target #{target}. Please pass a module as :target"
29
+ end
27
30
  DuckPuncher.logger.info %Q(#{target}#{" <-- #{mod.name}#{punches}" if punches.any?})
28
- extender = Usable::ModExtender.new(mod, only: options.delete(:only), method: options.delete(:method))
31
+ extender = Usable::ModExtender.new(mod, only: opts.delete(:only), method: opts.delete(:method))
29
32
  extender.call target
30
- extender = nil
31
- options[:after].call(target) if options[:after]
33
+ opts[:after].call(target) if opts[:after]
32
34
  end
33
35
  targets
34
36
  end
37
+
38
+ alias punch call
35
39
  end
36
40
  end
@@ -43,7 +43,7 @@ module DuckPuncher
43
43
  # shim for backwards compatibility with Rails 3
44
44
  def scoped
45
45
  where(nil)
46
- end if Rails::VERSION::MAJOR != 3
46
+ end if ::Rails::VERSION::MAJOR > 3
47
47
  end
48
48
  end
49
49
  end
@@ -1,3 +1,3 @@
1
1
  module DuckPuncher
2
- VERSION = '4.4.1'.freeze
2
+ VERSION = '4.4.2'.freeze
3
3
  end
@@ -0,0 +1,45 @@
1
+ require_relative '../../test_helper'
2
+
3
+ class DuckTest < MiniTest::Test
4
+ def setup
5
+ @class = Class.new
6
+ @object = @class.new
7
+ @mod = Module.new { %w[foo bar baz].each { |x| define_method(x, -> {}) } }
8
+ @subject = DuckPuncher::Duck.new(@class, @mod)
9
+ end
10
+
11
+ def test_punch
12
+ @subject.target = Kaia
13
+ refute_respond_to Kaia.new, :baz
14
+ @subject.call
15
+ assert_respond_to Kaia.new, :foo
16
+ assert_respond_to Kaia.new, :bar
17
+ assert_respond_to Kaia.new, :baz
18
+ end
19
+
20
+ def test_punch_with_instance
21
+ e = assert_raises ArgumentError do
22
+ @subject.call target: @object
23
+ end
24
+ assert_match /Invalid target #<#{@class}:.*>\. Please pass a module as :target/,
25
+ e.message
26
+ end
27
+
28
+ def test_punch_with_only
29
+ refute_respond_to @object, :foo
30
+ refute_respond_to @object, :bar
31
+ @subject.call(only: :foo)
32
+ refute_respond_to @object, :bar
33
+ assert_respond_to @object, :foo
34
+ @subject.call(only: :bar)
35
+ assert_respond_to @object, :bar
36
+ end
37
+
38
+ def test_punch_with_only_target
39
+ refute_respond_to @object, :bar
40
+ @subject.call target: @class, only: [:foo, :bar]
41
+ assert_respond_to @object, :bar
42
+ assert_respond_to @object, :foo
43
+ refute_respond_to @object, :baz
44
+ end
45
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: duck_puncher
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.4.1
4
+ version: 4.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Buckley
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-20 00:00:00.000000000 Z
11
+ date: 2017-01-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: usable
@@ -121,6 +121,7 @@ files:
121
121
  - lib/duck_puncher/version.rb
122
122
  - test/fixtures/test_classes.rb
123
123
  - test/fixtures/wut.rb
124
+ - test/lib/duck_puncher/duck_test.rb
124
125
  - test/lib/duck_puncher/enumerable_test.rb
125
126
  - test/lib/duck_puncher/hash_test.rb
126
127
  - test/lib/duck_puncher/method_test.rb
@@ -157,6 +158,7 @@ summary: Administer precision extensions (a.k.a "punches") to your favorite Ruby
157
158
  test_files:
158
159
  - test/fixtures/test_classes.rb
159
160
  - test/fixtures/wut.rb
161
+ - test/lib/duck_puncher/duck_test.rb
160
162
  - test/lib/duck_puncher/enumerable_test.rb
161
163
  - test/lib/duck_puncher/hash_test.rb
162
164
  - test/lib/duck_puncher/method_test.rb