duck_puncher 2.6.0 → 2.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/Gemfile +4 -0
- data/README.md +4 -15
- data/lib/duck_puncher/duck.rb +1 -7
- data/lib/duck_puncher/ducks/hash.rb +5 -8
- data/lib/duck_puncher/ducks/object.rb +4 -0
- data/lib/duck_puncher/ducks/string.rb +2 -2
- data/lib/duck_puncher/version.rb +1 -1
- data/lib/duck_puncher.rb +0 -4
- data/test/duck_puncher/array_test.rb +4 -4
- data/test/duck_puncher/hash_test.rb +2 -2
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f48603e05216d29053e2f95e16b169b802bc78e2
|
4
|
+
data.tar.gz: 044ce7bcd8d7418a24bdc8455c514b642da23913
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b79b94c75adda2e450d3799b47ab21564817e732ddbd19f2c05dcec268ed55421f0448bafad72177316afc3cf51e01a4364a1d2ac960b379c8dcafe62ff1bb18
|
7
|
+
data.tar.gz: c60bc8371e38deeb3b322c3cffeb608ac2df063c9496eabad82f7cc8f3c976c6a0a81770ee79e3e7d0c9fc2b87767307bf9ae516aacab23968dd3bd35dc17d1b
|
data/.gitignore
CHANGED
data/Gemfile
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#dig => `{a: 1, b: {c: 2}}.dig(:b, :c)` => 2 (
|
11
|
+
Hash#dig => `{a: 1, b: {c: 2}}.dig(:b, :c)` => 2 (Part of standard lib in 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'
|
@@ -65,22 +65,11 @@ DuckString.new.respond_to? :underscore #=> true
|
|
65
65
|
```
|
66
66
|
|
67
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
|
68
|
+
+why DuckPuncher defines an `Object#punch` method that sets this stuff up for you!
|
69
69
|
|
70
70
|
```ruby
|
71
|
-
punch
|
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:
|
76
|
-
|
77
|
-
Using a combination of `Array#mm` and `Hash#dig`:
|
78
|
-
|
79
|
-
```ruby
|
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]
|
71
|
+
DuckPuncher.punch! :Object
|
72
|
+
"yes".punch.to_boolean
|
84
73
|
```
|
85
74
|
|
86
75
|
## Contributing
|
data/lib/duck_puncher/duck.rb
CHANGED
@@ -8,14 +8,8 @@ module DuckPuncher
|
|
8
8
|
@punched = false
|
9
9
|
end
|
10
10
|
|
11
|
-
# @note Assumes the String duck is loaded first
|
12
11
|
def load_path
|
13
|
-
|
14
|
-
name.to_s.downcase
|
15
|
-
else
|
16
|
-
Object.send(:punch, :String, name.to_s).underscore
|
17
|
-
end
|
18
|
-
"duck_puncher/ducks/#{path_name}"
|
12
|
+
"duck_puncher/ducks/#{name.to_s.gsub(/\B([A-Z])/, '_\1').downcase}"
|
19
13
|
end
|
20
14
|
|
21
15
|
def punch(target = nil)
|
@@ -5,16 +5,13 @@ module DuckPuncher
|
|
5
5
|
def dig(*_keys_)
|
6
6
|
last_level = self
|
7
7
|
sought_value = nil
|
8
|
-
|
9
8
|
_keys_.each_with_index do |_key_, _idx_|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
last_level = last_level[_key_]
|
15
|
-
end
|
9
|
+
break unless last_level.respond_to?(:has_key?)
|
10
|
+
break unless last_level.has_key?(_key_)
|
11
|
+
if _idx_ + 1 == _keys_.length
|
12
|
+
sought_value = last_level[_key_]
|
16
13
|
else
|
17
|
-
|
14
|
+
last_level = last_level[_key_]
|
18
15
|
end
|
19
16
|
end
|
20
17
|
|
@@ -12,9 +12,9 @@ module DuckPuncher
|
|
12
12
|
def to_boolean(strict = false)
|
13
13
|
@boolean_map ||= begin
|
14
14
|
truths, falsities = %w(true 1 yes y on), ['false', '0', 'no', 'n', 'off', '']
|
15
|
-
Hash[truths.product([true]) + falsities.product([false])]
|
15
|
+
::Hash[truths.product([true]) + falsities.product([false])]
|
16
16
|
end
|
17
|
-
strict ? !downcase.in?(falsities) :
|
17
|
+
strict ? !downcase.in?(falsities) : !!@boolean_map[downcase]
|
18
18
|
end
|
19
19
|
end
|
20
20
|
end
|
data/lib/duck_puncher/version.rb
CHANGED
data/lib/duck_puncher.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
require_relative '../test_helper'
|
2
|
-
DuckPuncher.punch! :
|
2
|
+
DuckPuncher.punch! :Object
|
3
3
|
|
4
4
|
class ArrayTest < MiniTest::Test
|
5
5
|
attr_reader :subject
|
6
6
|
|
7
7
|
def setup
|
8
|
-
@subject = ('a'..'m').to_a
|
8
|
+
@subject = ('a'..'m').to_a.punch
|
9
9
|
end
|
10
10
|
|
11
11
|
def test_m
|
@@ -21,7 +21,7 @@ class ArrayTest < MiniTest::Test
|
|
21
21
|
end
|
22
22
|
|
23
23
|
def test_get
|
24
|
-
assert_equal [].methods.get(/ty\?/), [:empty?]
|
25
|
-
assert_equal [].methods.get('ty?'), [:empty?]
|
24
|
+
assert_equal [].methods.punch.get(/ty\?/), [:empty?]
|
25
|
+
assert_equal [].methods.punch.get('ty?'), [:empty?]
|
26
26
|
end
|
27
27
|
end
|
@@ -1,9 +1,9 @@
|
|
1
1
|
require_relative '../test_helper'
|
2
|
-
DuckPuncher.punch! :
|
2
|
+
DuckPuncher.punch! :Object
|
3
3
|
|
4
4
|
class HashTest < MiniTest::Test
|
5
5
|
def test_dig
|
6
|
-
my_hash = { a: 1, b: { c: 2 } }
|
6
|
+
my_hash = { a: 1, b: { c: 2 } }.punch
|
7
7
|
assert_equal my_hash.dig(:a), 1
|
8
8
|
assert_equal my_hash.dig(:b, :a), nil
|
9
9
|
assert_equal my_hash.dig(:b, :c), 2
|
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: 2.
|
4
|
+
version: 2.7.0
|
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-01
|
11
|
+
date: 2016-02-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -126,7 +126,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
126
126
|
version: '0'
|
127
127
|
requirements: []
|
128
128
|
rubyforge_project:
|
129
|
-
rubygems_version: 2.5.1
|
129
|
+
rubygems_version: 2.4.5.1
|
130
130
|
signing_key:
|
131
131
|
specification_version: 4
|
132
132
|
summary: Duck punches Ruby
|