duck_puncher 2.5.1 → 2.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -1
- data/.travis.yml +1 -0
- data/README.md +23 -12
- data/duck_puncher.gemspec +2 -2
- data/lib/duck_puncher/ducks/hash.rb +2 -2
- data/lib/duck_puncher/version.rb +1 -1
- data/test/duck_puncher/hash_test.rb +5 -5
- data/test/duck_puncher/method_test.rb +2 -2
- metadata +2 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f9f681e5807943d40660685fd071fd2efd65d595
|
4
|
+
data.tar.gz: 24c64853a09c5f52516e8d2c5462919be3178e1a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c9e6c53ce21e6a8564e2b0e09b8b39a8fef2e1f5dcddf2225e9aeb807441fb9c85d22fc48dd012abbb493dd4736ce7cd8b7f3d52b7158d8368fa3ed54d8bd884
|
7
|
+
data.tar.gz: 34bb650e74c17f00b5bd4894653c42f29fe9c8a943e5035173dd345e160ba90fae5e0d5d07d118fbd2bf0314bbc85b52de7a733a02b9b9f21bf8683fe525a768
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
ruby-2.
|
1
|
+
ruby-2.3.0
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -8,7 +8,7 @@ These are the ducks I love the most:
|
|
8
8
|
Array#m => `[].m(:to_s)` => `[].map(&:to_s)`
|
9
9
|
Array#mm => `[].mm(:sub, /[aeiou]/, '*')` => `[].map { |x| x.sub(/[aeiou]/, '*') }`
|
10
10
|
Array#get => `[].methods.get('ty?')` => [:empty?]
|
11
|
-
Hash#
|
11
|
+
Hash#dig => `{a: 1, b: {c: 2}}.dig(:b, :c)` => 2 (Now build into Ruby 2.3)
|
12
12
|
Numeric#to_currency => `25.245.to_currency` => 25.25
|
13
13
|
Numeric#to_duration => `10_000.to_duration` => '2 h 46 min'
|
14
14
|
Numeric#to_time_ago => `10_000.to_time_ago` => '2 hours ago'
|
@@ -40,7 +40,7 @@ Fetching: pry-0.10.3.gem (100%)
|
|
40
40
|
[1] pry(main)>
|
41
41
|
```
|
42
42
|
|
43
|
-
|
43
|
+
Pretty cool, right? Although, it doesn't work well with bigger gems or those with native extensions.
|
44
44
|
|
45
45
|
## Install
|
46
46
|
|
@@ -48,28 +48,39 @@ Try it out if your feeling frisky! However, I noticed it doesn't work well with
|
|
48
48
|
|
49
49
|
## Usage
|
50
50
|
|
51
|
-
Ducks need to be _loaded_ before they can be punched! Maybe
|
51
|
+
Ducks need to be _loaded_ before they can be punched! Maybe put this in an initializer:
|
52
52
|
|
53
53
|
```ruby
|
54
|
+
# config/initializers/duck_puncher.rb
|
54
55
|
DuckPuncher.punch_all! #=> punches all the ducks forever
|
55
56
|
DuckPuncher.punch! :Hash, :Object #=> only punches the Hash and Object ducks
|
57
|
+
```
|
58
|
+
|
59
|
+
Create a new class of your favorite duck pre-punched:
|
60
|
+
|
61
|
+
```ruby
|
56
62
|
DuckPuncher.punch :String #=> returns an anonymous punched duck that inherits from String
|
57
63
|
DuckString = DuckPuncher.punch :String #=> give the anonymous duck a name, so that you can use it!
|
58
64
|
DuckString.new.respond_to? :underscore #=> true
|
59
65
|
```
|
60
66
|
|
61
|
-
|
62
|
-
ducks
|
67
|
+
That works, but it's pretty verbose and not real pactical to be managing potentially a bunch of custom classes. That's
|
68
|
+
+why DuckPuncher defines a global `punch` method. This makes it easy to decorate your ducks on demand:
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
punch(:String, "yes").to_boolean
|
72
|
+
```
|
73
|
+
|
74
|
+
The new method isn't persisted on the String class. Instead it creates (and caches) a String delegation
|
75
|
+
class. This class is pre-punched with the available string extensions! :punch:
|
63
76
|
|
64
|
-
|
77
|
+
Using a combination of `Array#mm` and `Hash#dig`:
|
65
78
|
|
66
79
|
```ruby
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
>> a.mm :pluralize, 2
|
72
|
-
=> ["foos", "bars"]
|
80
|
+
params = { users: [{ id: 1, profile: { name: 'ryan' }}, { id: 2, profile: { name: 'kawika' }}, { id: 3 }] }
|
81
|
+
list = punch :Array, params[:users].map { |u| punch(:Hash, u) }
|
82
|
+
list.mm :dig, :profile, :name
|
83
|
+
#=> ["ryan", "kawika", nil]
|
73
84
|
```
|
74
85
|
|
75
86
|
## Contributing
|
data/duck_puncher.gemspec
CHANGED
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
21
|
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
-
spec.add_development_dependency "rake", '~> 10.1'
|
22
|
+
spec.add_development_dependency "rake", '~> 10.1'
|
23
23
|
spec.add_development_dependency "minitest", '~> 5.0'
|
24
|
-
spec.add_development_dependency "minitest-reporters", '~> 1.1'
|
24
|
+
spec.add_development_dependency "minitest-reporters", '~> 1.1'
|
25
25
|
end
|
@@ -2,7 +2,7 @@ module DuckPuncher
|
|
2
2
|
module Ducks
|
3
3
|
module Hash
|
4
4
|
# http://coryodaniel.com/index.php/2009/12/30/ruby-getting-deeply-nested-values-from-a-hash-in-one-line-of-code/
|
5
|
-
def
|
5
|
+
def dig(*_keys_)
|
6
6
|
last_level = self
|
7
7
|
sought_value = nil
|
8
8
|
|
@@ -19,7 +19,7 @@ module DuckPuncher
|
|
19
19
|
end
|
20
20
|
|
21
21
|
sought_value
|
22
|
-
end
|
22
|
+
end unless method_defined?(:dig)
|
23
23
|
end
|
24
24
|
end
|
25
25
|
end
|
data/lib/duck_puncher/version.rb
CHANGED
@@ -2,11 +2,11 @@ require_relative '../test_helper'
|
|
2
2
|
DuckPuncher.punch! :Hash
|
3
3
|
|
4
4
|
class HashTest < MiniTest::Test
|
5
|
-
def
|
5
|
+
def test_dig
|
6
6
|
my_hash = { a: 1, b: { c: 2 } }
|
7
|
-
assert_equal my_hash.
|
8
|
-
assert_equal my_hash.
|
9
|
-
assert_equal my_hash.
|
10
|
-
assert_equal my_hash.
|
7
|
+
assert_equal my_hash.dig(:a), 1
|
8
|
+
assert_equal my_hash.dig(:b, :a), nil
|
9
|
+
assert_equal my_hash.dig(:b, :c), 2
|
10
|
+
assert_equal my_hash.dig(:b), { c: 2 }
|
11
11
|
end
|
12
12
|
end
|
@@ -11,14 +11,14 @@ class MethodTest < MiniTest::Test
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def test_to_instruct
|
14
|
-
assert_match
|
14
|
+
assert_match /:to_a/, @subject.method(:to_a).to_instruct
|
15
15
|
assert_match /newarray/, @subject.method(:to_a).to_instruct
|
16
16
|
assert_match /opt_plus/, @subject.method(:to_a).to_instruct
|
17
17
|
assert_equal nil, [].method(:to_s).to_instruct
|
18
18
|
end
|
19
19
|
|
20
20
|
def test_to_instruct_single_line
|
21
|
-
assert_match
|
21
|
+
assert_match /:to_f/, @subject.method(:to_f).to_instruct
|
22
22
|
assert_match /getconstant\s*:INFINITY/, @subject.method(:to_f).to_instruct
|
23
23
|
end
|
24
24
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: duck_puncher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Buckley
|
@@ -31,9 +31,6 @@ dependencies:
|
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '10.1'
|
34
|
-
- - ">="
|
35
|
-
- !ruby/object:Gem::Version
|
36
|
-
version: 10.1.1
|
37
34
|
type: :development
|
38
35
|
prerelease: false
|
39
36
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -41,9 +38,6 @@ dependencies:
|
|
41
38
|
- - "~>"
|
42
39
|
- !ruby/object:Gem::Version
|
43
40
|
version: '10.1'
|
44
|
-
- - ">="
|
45
|
-
- !ruby/object:Gem::Version
|
46
|
-
version: 10.1.1
|
47
41
|
- !ruby/object:Gem::Dependency
|
48
42
|
name: minitest
|
49
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -65,9 +59,6 @@ dependencies:
|
|
65
59
|
- - "~>"
|
66
60
|
- !ruby/object:Gem::Version
|
67
61
|
version: '1.1'
|
68
|
-
- - ">="
|
69
|
-
- !ruby/object:Gem::Version
|
70
|
-
version: 1.1.0
|
71
62
|
type: :development
|
72
63
|
prerelease: false
|
73
64
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -75,9 +66,6 @@ dependencies:
|
|
75
66
|
- - "~>"
|
76
67
|
- !ruby/object:Gem::Version
|
77
68
|
version: '1.1'
|
78
|
-
- - ">="
|
79
|
-
- !ruby/object:Gem::Version
|
80
|
-
version: 1.1.0
|
81
69
|
description: Duck punches Ruby with some of my favorite class extensions
|
82
70
|
email:
|
83
71
|
- arebuckley@gmail.com
|
@@ -138,7 +126,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
138
126
|
version: '0'
|
139
127
|
requirements: []
|
140
128
|
rubyforge_project:
|
141
|
-
rubygems_version: 2.
|
129
|
+
rubygems_version: 2.5.1
|
142
130
|
signing_key:
|
143
131
|
specification_version: 4
|
144
132
|
summary: Duck punches Ruby
|