powertools 0.0.6 → 0.2.1

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: ddcd97fb825ac6d5ed45e04047a1a1a94f12cf9b173417aa398eaa2fa24c52f2
4
- data.tar.gz: f6ab74e95928578957ca4799f6a5bfff68fe647ceaabc1597501512150bb47e8
3
+ metadata.gz: f7b4521f52edb1386945a738a2011114bc9367a4116edc013f3c39dbb291784e
4
+ data.tar.gz: 1fb661d669f30866bf44de553f0ce7b8096aa3f6649fe7633e3aa6e5ecff382b
5
5
  SHA512:
6
- metadata.gz: a0c824ef5b0ca49abc5079a49709af69a856d00b2f82c650262b2c8ca63a075b0fdab5bf4d418efc2c87b6305b625b0b96a3dd299b576e497f01371d6b05af60
7
- data.tar.gz: 8647c2f5b579839de6958481b1b13bd51c2ccb52479e3470992bec9af9629ad461a2b03da46dddc6a6e5a39fc1a6510dca770acfc5b1863b4576cb46e2c7ee7c
6
+ metadata.gz: e4f3c01831b3b70356807668ace48a47aed9ef804a5b8ce8fcd7a3e4dbf219e18f883773eb6fb5bba26401c451786c2403ef164be27b6d37ee25b1a597d76a26
7
+ data.tar.gz: c1da19c583b5b229488c11e386bcbda9b8d90ba47af8f3b934c31d1503d4a7df4fe27d77d620eb85d892aeb137ffee5e993a121a5d9740a9ade49f44998cf1ad
@@ -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
data/lib/powertools.rb CHANGED
@@ -1,2 +1,5 @@
1
- require_relative 'maybe_chain'
2
- require_relative 'range'
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'powertool/maybe_chain'
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.6
4
+ version: 0.2.1
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
@@ -22,7 +22,8 @@ files:
22
22
  homepage: http://github.com/cjkinni/powertools
23
23
  licenses:
24
24
  - MIT
25
- metadata: {}
25
+ metadata:
26
+ rubygems_mfa_required: 'true'
26
27
  post_install_message:
27
28
  rdoc_options: []
28
29
  require_paths:
@@ -31,7 +32,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
31
32
  requirements:
32
33
  - - ">="
33
34
  - !ruby/object:Gem::Version
34
- version: '0'
35
+ version: 2.5.0
35
36
  required_rubygems_version: !ruby/object:Gem::Requirement
36
37
  requirements:
37
38
  - - ">="