powertools 0.0.7 → 0.2.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
  SHA256:
3
- metadata.gz: ed198bbbfd3e8c93142048362c85abd75951d7a0a0faf95921cff8d28338078d
4
- data.tar.gz: 9bbeeadac664dffd079b1f1c1e259508807aebe694c09feb5e69890f45d6fe43
3
+ metadata.gz: 9bcd41100572beb93a11d58824fc071ea5fdf680fc56f0b5c747d5233a4e73fd
4
+ data.tar.gz: 52fe1e4acd2de1331c214e2a2575e6d94672172e459303b93fe528daa4ce79ab
5
5
  SHA512:
6
- metadata.gz: '0091acf40578b33fbf35c7a38c8816d1e1fcf9543b298e8ab95ede575e4b443a17acee729ed0a40c1a917d58ec728cbcb315a4442d6488894d945a0a7233e71d'
7
- data.tar.gz: d9d61c01d78960bcc6a096271a4b532d436db527b1610a6230c7846d0d1d1471577fb2c9f77555944d133476e66f4bdc5665ae4590562a985c197fff524c6542
6
+ metadata.gz: a63ab6c525bf83c21a6d6374bf720b389c88483fa6e9620b97ed2faf033fbd68b077f8f1d42b4a97754818fb837844eded29ea197a5e83ba283fc9cf04d3a1ee
7
+ data.tar.gz: 42d65c8ab48f60abbcb0604a2604ff7221df1d4bd12dd1e96b7d680c2e2e68c2f90662c2ecfe140f81ff2aab5ed879cae694683660ba51e0bfe927df3fe0c678
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2021 C.J.Kinniburgh
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,75 @@
1
+ # Powertools
2
+
3
+ This gem provides some extensions to Ruby classes that make my life a lot easier.
4
+
5
+ It doesn't aim to be a comprehensive list of improvements, but rather a curated
6
+ list of things I find myself encountering frequently.
7
+
8
+ # Usage
9
+
10
+ Install Powertools with `gem install powertools`.
11
+
12
+ Then, you can add `require 'powertools'` to your Ruby file to access all the
13
+ features provided by Powertools.
14
+
15
+ You may also want to only use certain features.
16
+
17
+ You can currently also use the following to selectively add features:
18
+
19
+ - `require 'powertool/maybe_chain'`
20
+ - `require 'powertool/range'`
21
+ - `require 'powertool/set'`
22
+
23
+ # Warning about Breaking Changes
24
+
25
+ Some features may introduce breaking changes to the Ruby standard library.
26
+ These 'breaking changes' are generally minor changes which in my opinion
27
+ conform to my expectations about how the language should work.
28
+
29
+ Still, I'll make an effor to list any changes that break a potential assumption
30
+ about the Ruby standard library here.
31
+
32
+ ## `Range#each`
33
+
34
+ If you are relying on `(4..1).each { |_| ... }` to do nothing,
35
+ because it's going from a higher number to a lower number, you should not use
36
+ the `powertool/range` package.
37
+
38
+ # Range Improvements
39
+
40
+ `#sort` is provided as a helper method to ensure your range is from smallest
41
+ to largest.
42
+
43
+ `#each` now works backwards for all classes that include a `#pred` method.
44
+ This is only enabled when a range is between two elements of the same class.
45
+ (For example, `(date.today..100000000)` is a valid Ruby range. We fallback
46
+ to the default `#each` implementation for these.)
47
+
48
+ # Maybe Chains
49
+
50
+ `#maybe_chain` is added to `Array` and `Hash`. It takes an array as an argument,
51
+ and attempts to access nested data based on the elements of the argument. If at
52
+ any point it cannot access an element, it will return nil without throwing an
53
+ error. This can be very useful for accessing deeply nested data structures, like
54
+ parsed JSON objects, which may have nil elements.
55
+
56
+ E.g.
57
+
58
+ ```ruby
59
+ a = [1,2,3,{d:[9]}]
60
+ a.maybe_chain [0]
61
+ => 1
62
+ a.maybe_chain [3]
63
+ => {:d=>[1]}
64
+ a.maybe_chain [3, :d, 0]
65
+ => 9
66
+ a.maybe_chain [4]
67
+ => nil
68
+ a.maybe_chain [4, :missing_element]
69
+ => nil
70
+ ```
71
+
72
+ # Popable Sets
73
+
74
+ `#pop` has been added to `Set`. This returns a random
75
+ element from the set, and removes it from the set.
@@ -1,27 +1,31 @@
1
- class Hash
2
- def maybe_chain(args = [])
3
- return nil unless self
4
-
5
- r = self
6
- args.each_with_index do |a, _i|
7
- return nil if r[a].nil? || (r.class != Hash && r.class != Array)
8
-
9
- r = a.instance_of?(Range) ? a.map { |i| r[i] } : r[a]
10
- end
11
- r
1
+ # frozen_string_literal: true
2
+
3
+ # A private helper for MaybeChain
4
+ class PowertoolMaybeChain
5
+ def self.maybe_chain(caller, args)
6
+ args.each_with_index do |a, _i|
7
+ return nil if caller[a].nil? || ![Hash, Array, Range].include?(caller.class)
8
+
9
+ caller = a.instance_of?(Range) ? a.map { |i| caller[i] } : caller[a]
12
10
  end
11
+ caller
12
+ end
13
+ end
14
+
15
+ # Ruby's Hash class
16
+ class Hash
17
+ def maybe_chain(args = [])
18
+ return nil unless self
19
+
20
+ PowertoolMaybeChain.maybe_chain(self, args)
21
+ end
13
22
  end
14
23
 
24
+ # Ruby's Array class
15
25
  class Array
16
- def maybe_chain(args = [])
17
- return nil unless self
18
-
19
- r = self
20
- args.each_with_index do |a, _i|
21
- return nil if r[a].nil? || (r.class != Hash && r.class != Array)
22
-
23
- r = a.instance_of?(Range) ? a.map { |i| r[i] } : r[a]
24
- end
25
- r
26
- end
27
- end
26
+ def maybe_chain(args = [])
27
+ return nil unless self
28
+
29
+ PowertoolMaybeChain.maybe_chain(self, args)
30
+ end
31
+ end
@@ -1,47 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Extensions to Ruby's Range class
1
4
  module RangeExtensions
2
- def each
3
- return super if self.first <= self.last
4
-
5
- position = self.first
6
- while position >= self.last
7
- yield(position)
8
- position = position.pred
9
- end
10
- end
11
-
12
- def each(warn_on_order_error: true)
13
- return super() if self.first <= self.last
14
-
15
- position = self.first
16
- prev_position = nil
17
- while position >= self.last
18
- # We always attempt pred before yielding.
19
- prev_position = position.clone
20
- begin
21
- position = position.pred
22
- rescue NoMethodError
23
- if warn_on_order_error
24
- raise ArgumentError.new "#{self.first.class}s do not support backwards iteration."
25
- else
26
- return super()
27
- end
28
- end
5
+ def each(warn_on_order_error: true)
6
+ return super() if first <= last || first.class != last.class
29
7
 
30
- yield(prev_position)
31
- end
32
- end
8
+ position = first
9
+ while position >= last
10
+ # We always attempt pred before yielding.
11
+ prev_position = position.clone
12
+ begin
13
+ position = position.pred
14
+ rescue NoMethodError
15
+ raise ArgumentError, "#{first.class}s do not support backwards iteration." if warn_on_order_error
33
16
 
34
- def sort
35
- begin
36
- return self if self.first <= self.last
37
- rescue NoMethodError
38
- raise "#{self.first.class}s cannot be sorted"
39
- end
17
+ return super()
18
+ end
40
19
 
41
- (self.last..self.first)
20
+ yield(prev_position)
42
21
  end
22
+ end
23
+
24
+ def sort
25
+ begin
26
+ return self if first <= last
27
+ rescue NoMethodError
28
+ raise "#{first.class}s cannot be sorted"
29
+ end
30
+
31
+ (last..first)
32
+ end
43
33
  end
44
34
 
35
+ # Ruby's Range class
45
36
  class Range
46
- prepend RangeExtensions
47
- end
37
+ prepend RangeExtensions
38
+ end
@@ -0,0 +1,7 @@
1
+ class Set
2
+ def pop
3
+ temp = self.to_a.pop
4
+ self.delete(temp)
5
+ temp
6
+ end
7
+ end
data/lib/powertools.rb CHANGED
@@ -1,2 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'powertool/maybe_chain'
2
- require_relative 'powertool/range'
4
+ require_relative 'powertool/range'
5
+ require_relative 'powertool/set'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: powertools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - C. Kinniburgh
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-05 00:00:00.000000000 Z
11
+ date: 2021-12-09 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: powertools@cjkinni.com
@@ -16,13 +16,17 @@ executables: []
16
16
  extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
+ - LICENSE
20
+ - README.md
19
21
  - lib/powertool/maybe_chain.rb
20
22
  - lib/powertool/range.rb
23
+ - lib/powertool/set.rb
21
24
  - lib/powertools.rb
22
25
  homepage: http://github.com/cjkinni/powertools
23
26
  licenses:
24
27
  - MIT
25
- metadata: {}
28
+ metadata:
29
+ rubygems_mfa_required: 'true'
26
30
  post_install_message:
27
31
  rdoc_options: []
28
32
  require_paths:
@@ -31,7 +35,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
31
35
  requirements:
32
36
  - - ">="
33
37
  - !ruby/object:Gem::Version
34
- version: '0'
38
+ version: 2.5.0
35
39
  required_rubygems_version: !ruby/object:Gem::Requirement
36
40
  requirements:
37
41
  - - ">="