refinements 3.2.0 → 4.0.0

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: fac72c34bfb6a876d35c12e6286f649c09754678
4
- data.tar.gz: 30cf5ec268f368c13befd78f0f389ab20c428694
3
+ metadata.gz: 19bcdd156b3b6053dd3ff8a312502d807b6dacfd
4
+ data.tar.gz: 91b591f5acfd7ae55b174dd48183effdf09fb10f
5
5
  SHA512:
6
- metadata.gz: 73587dc8896e3c799fb9a81fa1e0b748ef63865d740afe26e9446b3feb41a7428e2bedaae0068bd0233fc1e539daee201daa89f83dc0cfd2af5baf7254b772c6
7
- data.tar.gz: dd4845ac56ea495cf387dd3459b022e732bce845e63d4cd7d7fff9bb1c25023aa3d512eab5eba02d5eec0e5a54263553b4dfe45c3e038f10d653f68efcdd79c2
6
+ metadata.gz: 5af7222803acf6bebb65b266d8a33b76eebd3712181a4f9b321464d28290a6d51e4611eb3248a33e18c3c5402488ec8a8fd91ec982b43ecf8072966bd846b875
7
+ data.tar.gz: ff2fb700725382ef9dd508d9e72df9a7ba5675730c916bb4b6865799b1e4b5da9a484d299a7196337fbf085f5ef32ea9ebe46f7a81be1035edbfda2b59a741c4
checksums.yaml.gz.sig CHANGED
Binary file
data.tar.gz.sig CHANGED
Binary file
data/README.md CHANGED
@@ -42,8 +42,8 @@ Provides a collection of refinements to core Ruby objects.
42
42
  - Provides BigDecimal refinements:
43
43
  - `BigDecimal#inspect` - Allows one to inspect a big decimal with numeric representation.
44
44
  - Provides Hash refinements:
45
- - `#compact` - Removes key/value pairs with `nil` values without modifying itself.
46
- - `#compact!` - Removes key/value pairs with `nil` values while modifying itself.
45
+ - `except` - Answers new hash with given with given keys removed without modifying calling hash.
46
+ - `except!` - Answers new hash with given with given keys removed while modifying calling hash.
47
47
  - `#symbolize_keys` - Converts keys to symbols without modifying itself.
48
48
  - `#symbolize_keys!` - Converts keys to symbols while modifying itself.
49
49
  - `#slice` - Selects hash subset for given keys without modifying itself.
@@ -53,6 +53,8 @@ Provides a collection of refinements to core Ruby objects.
53
53
  - `#reverse_merge` - Merges calling hash into passed in hash without modifying calling hash.
54
54
  - `#reverse_merge!` - Merges calling hash into passed in hash while modifying calling hash.
55
55
  - Provides String refinements:
56
+ - `#first` - Answers first character of a string or first set of characters if given a number.
57
+ - `#last` - Answers last character of a string or last set of characters if given a number.
56
58
  - `#blank?` - Answers `true`/`false` based on whether string is blank or not (i.e. `<space>`,
57
59
  `\n`, `\t`, `\r`).
58
60
  - `#up` - Answers string with only first letter upcased.
@@ -60,10 +62,12 @@ Provides a collection of refinements to core Ruby objects.
60
62
  - `#camelcase` - Answers a camelcased string. Example: "ThisIsCamelcase".
61
63
  - `#snakecase` - Answers a snakecased string. Example: "this_is_snakecase".
62
64
  - `#titleize` - Answers titleized string. Example: "This Is Titleized".
65
+ - `#use` - Provides hash value computation, via a block, by using
66
+ only the keys as arguments to the block.
63
67
 
64
68
  # Requirements
65
69
 
66
- 0. [Ruby 2.3.x](https://www.ruby-lang.org).
70
+ 0. [Ruby 2.4.x](https://www.ruby-lang.org).
67
71
  0. A solid understanding of [Ruby refinements and lexical scope](https://www.youtube.com/watch?v=qXC9Gk4dCEw).
68
72
 
69
73
  # Setup
@@ -117,6 +121,12 @@ The following sections demonstrate how each refinement enriches your objects wit
117
121
 
118
122
  ### String
119
123
 
124
+ "example".first # => "e"
125
+ "example".first 4 # => "exam"
126
+
127
+ "instant".last # => "t"
128
+ "instant".first 3 # => "ant"
129
+
120
130
  " \n\t\r".blank? # => true
121
131
  "example".up # => "Example"
122
132
  "EXAMPLE".down # => "eXAMPLE"
@@ -141,13 +151,13 @@ The following sections demonstrate how each refinement enriches your objects wit
141
151
 
142
152
  ### Hash
143
153
 
144
- example = {a: 1, b: nil}
145
- example.compact # => {a: 1}
146
- example # => {a: 1, b: nil}
154
+ example = {a: 1, b: 2, c: 3}
155
+ example.except :a, :b # => {c: 3}
156
+ example # => {a: 1, b: 2, c: 3}
147
157
 
148
- example = {a: 1, b: nil}
149
- example.compact! # => {a: 1}
150
- example # => {a: 1}
158
+ example = {a: 1, b: 2, c: 3}
159
+ example.except! :a, :b # => {c: 3}
160
+ example # => {c: 3}
151
161
 
152
162
  example = {"a" => 1, "b" => 2}
153
163
  example.symbolize_keys # => {a: 1, b: 2}
@@ -181,6 +191,9 @@ The following sections demonstrate how each refinement enriches your objects wit
181
191
  example.reverse_merge! a: 0, c: 3 # => {a: 1, b: 2, c: 3}
182
192
  example # => {a: 1, b: 2, c: 3}
183
193
 
194
+ example = {unit: "221B", street: "Baker Street", city: "London", country: "UK"}
195
+ example.use { |unit, street| "#{unit} #{street}" } # => "221B Baker Street"
196
+
184
197
  # Tests
185
198
 
186
199
  To test, run:
@@ -5,16 +5,12 @@ module Refinements
5
5
  module Hashes
6
6
  # rubocop:disable Metrics/BlockLength
7
7
  refine Hash do
8
- # TODO: Remove when Ruby 2.4 is released.
9
- def compact
10
- puts "[DEPRECATION]: #compact is deprecated and is included, by default, in Ruby 2.4."
11
- dup.compact!
8
+ def except *keys
9
+ reject { |key, _value| keys.include? key }
12
10
  end
13
11
 
14
- # TODO: Remove when Ruby 2.4 is released.
15
- def compact!
16
- puts "[DEPRECATION]: #compact! is deprecated and is included, by default, in Ruby 2.4."
17
- reject! { |_, value| value.nil? }
12
+ def except! *keys
13
+ replace except(*keys)
18
14
  end
19
15
 
20
16
  def symbolize_keys
@@ -27,39 +23,44 @@ module Refinements
27
23
  end
28
24
 
29
25
  def slice *keys
30
- keys.each.with_object({}) do |key, sliced_hash|
31
- sliced_hash[key] = self[key] if key?(key)
32
- end
26
+ select { |key, _value| keys.include? key }
33
27
  end
34
28
 
35
29
  def slice! *keys
36
30
  replace slice(*keys)
37
31
  end
38
32
 
39
- def deep_merge other_hash
40
- dup.deep_merge! other_hash
33
+ def deep_merge other
34
+ dup.deep_merge! other
41
35
  end
42
36
 
43
- def deep_merge! other_hash
44
- other_hash.each.with_object self do |(other_key, other_value), original_hash|
45
- current_value = original_hash[other_key]
46
- original_hash[other_key] = deep_merge_value current_value, other_value
37
+ def deep_merge! other
38
+ other.each do |(other_key, other_value)|
39
+ current_value = self[other_key]
40
+
41
+ self[other_key] = if current_value.is_a?(Hash) && other_value.is_a?(Hash)
42
+ current_value.deep_merge! other_value
43
+ else
44
+ other_value
45
+ end
47
46
  end
47
+
48
+ self
48
49
  end
49
50
 
50
- def reverse_merge other_hash
51
- other_hash.merge self
51
+ def reverse_merge other
52
+ other.merge self
52
53
  end
53
54
 
54
- def reverse_merge! other_hash
55
- merge!(other_hash) { |_, left_value, _| left_value }
55
+ def reverse_merge! other
56
+ merge!(other) { |_key, old_value, _new_value| old_value }
56
57
  end
57
58
 
58
- private
59
+ def use &block
60
+ return [] unless block_given?
59
61
 
60
- def deep_merge_value current_value, other_value
61
- return current_value.deep_merge(other_value) if current_value.is_a?(Hash) && other_value.is_a?(Hash)
62
- other_value
62
+ values = block.parameters.map { |(_type, key)| self[key] }
63
+ yield values
63
64
  end
64
65
  end
65
66
  end
@@ -12,7 +12,7 @@ module Refinements
12
12
  end
13
13
 
14
14
  def self.version
15
- "3.2.0"
15
+ "4.0.0"
16
16
  end
17
17
 
18
18
  def self.version_label
@@ -11,42 +11,66 @@ module Refinements
11
11
 
12
12
  # rubocop:disable Metrics/BlockLength
13
13
  refine String do
14
+ def first number = 0
15
+ return self if empty?
16
+
17
+ max = Integer number
18
+
19
+ return self[0] if max.zero?
20
+ return "" if max.negative?
21
+
22
+ self[0..(max - 1)]
23
+ end
24
+
25
+ # :reek:TooManyStatements
26
+ def last number = 0
27
+ return self if empty?
28
+
29
+ min = Integer number
30
+ max = size - 1
31
+
32
+ return self[max] if min.zero?
33
+ return "" if min.negative?
34
+
35
+ self[(min + 1)..max]
36
+ end
37
+
14
38
  def blank?
15
- !match(/\A\s*\z/).nil?
39
+ match?(/\A\s*\z/)
16
40
  end
17
41
 
18
42
  def up
19
43
  return self if empty?
20
- self[0].upcase + self[1, size]
44
+ first.upcase + self[1, size]
21
45
  end
22
46
 
23
47
  def down
24
48
  return self if empty?
25
- self[0].downcase + self[1, size]
49
+ first.downcase + self[1, size]
26
50
  end
27
51
 
28
52
  def camelcase
29
- if self =~ self.class.delimiters
30
- result = up_and_join split(%r(\s*\-\s*|\s*\/\s*|\s*\:+\s*)), delimiter: "::"
31
- up_and_join result.split(/\s*\_\s*|\s+/)
53
+ if match?(self.class.delimiters)
54
+ result = join_parts split(%r(\s*\-\s*|\s*\/\s*|\s*\:+\s*)), method: :up, delimiter: "::"
55
+ join_parts result.split(/\s*\_\s*|\s+/), method: :up
32
56
  else
33
57
  up
34
58
  end
35
59
  end
36
60
 
37
61
  def snakecase
38
- if self =~ self.class.delimiters
39
- result = down_and_join split(%r(\s*\-\s*|\s*\/\s*|\s*\:+\s*)), delimiter: "/"
40
- down_and_join result.split(/(?=[A-Z])|\s*\_\s*|\s+/), delimiter: "_"
62
+ if match?(self.class.delimiters)
63
+ result = join_parts split(%r(\s*\-\s*|\s*\/\s*|\s*\:+\s*)), method: :down, delimiter: "/"
64
+ join_parts result.split(/(?=[A-Z])|\s*\_\s*|\s+/), method: :down, delimiter: "_"
41
65
  else
42
66
  downcase
43
67
  end
44
68
  end
45
69
 
46
70
  def titleize
47
- if self =~ self.class.delimiters
48
- result = up_and_join split(/(?=[A-Z])|\s*\_\s*|\s*\-\s*|\s+/), delimiter: " "
49
- up_and_join result.split(%r(\s*\/\s*|\s*\:+\s*)), delimiter: "/"
71
+ if match?(self.class.delimiters)
72
+ result = join_parts split(/(?=[A-Z])|\s*\_\s*|\s*\-\s*|\s+/), method: :up, delimiter: " "
73
+ join_parts result.split(%r(\s*\/\s*|\s*\:+\s*)), method: :up, delimiter: "/"
50
74
  else
51
75
  capitalize
52
76
  end
@@ -54,17 +78,12 @@ module Refinements
54
78
 
55
79
  private
56
80
 
57
- def up_and_join parts, delimiter: ""
58
- parts.reduce "" do |result, part|
59
- next part.up if result.empty?
60
- "#{result}#{delimiter}#{part.up}"
61
- end
62
- end
63
-
64
- def down_and_join parts, delimiter: ""
81
+ # :reek:DuplicateMethodCall
82
+ # :reek:UtilityFunction
83
+ def join_parts parts, method:, delimiter: ""
65
84
  parts.reduce "" do |result, part|
66
- next part.down if result.empty?
67
- "#{result}#{delimiter}#{part.down}"
85
+ next part.send(method) if result.empty?
86
+ "#{result}#{delimiter}#{part.send method}"
68
87
  end
69
88
  end
70
89
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: refinements
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.0
4
+ version: 4.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brooke Kuhlmann
@@ -30,7 +30,7 @@ cert_chain:
30
30
  n/LUZ1dKhIHzfKx1B4+TEIefArObGfkLIDM8+Dq1RX7TF1k81Men7iu4MgE9bYBn
31
31
  3dE+xI3FdB5gWcdWxdtgRCmWjtXeYYyb4z6NQQ==
32
32
  -----END CERTIFICATE-----
33
- date: 2016-12-18 00:00:00.000000000 Z
33
+ date: 2017-01-22 00:00:00.000000000 Z
34
34
  dependencies:
35
35
  - !ruby/object:Gem::Dependency
36
36
  name: rake
@@ -50,14 +50,14 @@ dependencies:
50
50
  name: gemsmith
51
51
  requirement: !ruby/object:Gem::Requirement
52
52
  requirements:
53
- - - "~>"
53
+ - - ">="
54
54
  - !ruby/object:Gem::Version
55
55
  version: '8.2'
56
56
  type: :development
57
57
  prerelease: false
58
58
  version_requirements: !ruby/object:Gem::Requirement
59
59
  requirements:
60
- - - "~>"
60
+ - - ">="
61
61
  - !ruby/object:Gem::Version
62
62
  version: '8.2'
63
63
  - !ruby/object:Gem::Dependency
@@ -206,14 +206,14 @@ dependencies:
206
206
  requirements:
207
207
  - - "~>"
208
208
  - !ruby/object:Gem::Version
209
- version: '0.46'
209
+ version: '0.47'
210
210
  type: :development
211
211
  prerelease: false
212
212
  version_requirements: !ruby/object:Gem::Requirement
213
213
  requirements:
214
214
  - - "~>"
215
215
  - !ruby/object:Gem::Version
216
- version: '0.46'
216
+ version: '0.47'
217
217
  - !ruby/object:Gem::Dependency
218
218
  name: codeclimate-test-reporter
219
219
  requirement: !ruby/object:Gem::Requirement
@@ -257,7 +257,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
257
257
  requirements:
258
258
  - - "~>"
259
259
  - !ruby/object:Gem::Version
260
- version: '2.3'
260
+ version: '2.4'
261
261
  required_rubygems_version: !ruby/object:Gem::Requirement
262
262
  requirements:
263
263
  - - ">="
@@ -265,7 +265,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
265
265
  version: '0'
266
266
  requirements: []
267
267
  rubyforge_project:
268
- rubygems_version: 2.6.8
268
+ rubygems_version: 2.6.9
269
269
  signing_key:
270
270
  specification_version: 4
271
271
  summary: Provides a collection of refinements to core Ruby objects.
metadata.gz.sig CHANGED
Binary file