duck_puncher 2.6.0 → 2.7.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: f9f681e5807943d40660685fd071fd2efd65d595
4
- data.tar.gz: 24c64853a09c5f52516e8d2c5462919be3178e1a
3
+ metadata.gz: f48603e05216d29053e2f95e16b169b802bc78e2
4
+ data.tar.gz: 044ce7bcd8d7418a24bdc8455c514b642da23913
5
5
  SHA512:
6
- metadata.gz: c9e6c53ce21e6a8564e2b0e09b8b39a8fef2e1f5dcddf2225e9aeb807441fb9c85d22fc48dd012abbb493dd4736ce7cd8b7f3d52b7158d8368fa3ed54d8bd884
7
- data.tar.gz: 34bb650e74c17f00b5bd4894653c42f29fe9c8a943e5035173dd345e160ba90fae5e0d5d07d118fbd2bf0314bbc85b52de7a733a02b9b9f21bf8683fe525a768
6
+ metadata.gz: b79b94c75adda2e450d3799b47ab21564817e732ddbd19f2c05dcec268ed55421f0448bafad72177316afc3cf51e01a4364a1d2ac960b379c8dcafe62ff1bb18
7
+ data.tar.gz: c60bc8371e38deeb3b322c3cffeb608ac2df063c9496eabad82f7cc8f3c976c6a0a81770ee79e3e7d0c9fc2b87767307bf9ae516aacab23968dd3bd35dc17d1b
data/.gitignore CHANGED
@@ -16,3 +16,4 @@ test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
18
  .duck_puncher/
19
+ .byebug_history
data/Gemfile CHANGED
@@ -2,3 +2,7 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in duck_puncher.gemspec
4
4
  gemspec
5
+
6
+ if RUBY_VERSION >= '2.0'
7
+ gem 'byebug'
8
+ end
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 (Now build into Ruby 2.3)
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 a global `punch` method. This makes it easy to decorate your ducks on demand:
68
+ +why DuckPuncher defines an `Object#punch` method that sets this stuff up for you!
69
69
 
70
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:
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
@@ -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
- path_name = if name == :String
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
- if last_level.is_a?(Hash) && last_level.has_key?(_key_)
11
- if _idx_ + 1 == _keys_.length
12
- sought_value = last_level[_key_]
13
- else
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
- break
14
+ last_level = last_level[_key_]
18
15
  end
19
16
  end
20
17
 
@@ -10,6 +10,10 @@ module DuckPuncher
10
10
  require file_or_gem.tr('-', '/')
11
11
  end
12
12
  end
13
+
14
+ def punch
15
+ DuckPuncher.delegate_class(self.class.name.to_sym).new(self)
16
+ end
13
17
  end
14
18
  end
15
19
  end
@@ -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) : @boolean_map[downcase]
17
+ strict ? !downcase.in?(falsities) : !!@boolean_map[downcase]
18
18
  end
19
19
  end
20
20
  end
@@ -1,3 +1,3 @@
1
1
  module DuckPuncher
2
- VERSION = '2.6.0'.freeze
2
+ VERSION = '2.7.0'.freeze
3
3
  end
data/lib/duck_puncher.rb CHANGED
@@ -57,7 +57,3 @@ module DuckPuncher
57
57
  config.formatter = proc { |*args| "#{args.first}: #{args.last.to_s}\n" }
58
58
  end
59
59
  end
60
-
61
- def punch(name, val)
62
- DuckPuncher.delegate_class(name).new val
63
- end
@@ -1,11 +1,11 @@
1
1
  require_relative '../test_helper'
2
- DuckPuncher.punch! :Array
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! :Hash
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.6.0
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-31 00:00:00.000000000 Z
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