powertools 0.1.0 → 0.2.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
  SHA256:
3
- metadata.gz: 70a557fe6767b31c92903590d47c183de88091f4ade26d9fee13a108924c7463
4
- data.tar.gz: d274ef8a8a7c56093ef72b46f1720cda5dbe2ade0e0ee11a9f5110306aa8a03a
3
+ metadata.gz: 3d16ccd25661c68f7d98b47bf6bf7dd3f2661afdce2d207e1c5590672196ad3b
4
+ data.tar.gz: 536553941e3cdcfe5b84a9da4a8f00c6fc8a39a43e9cab588de171f21b77b7b3
5
5
  SHA512:
6
- metadata.gz: 69bc39d2edcfee7db857eff60f6a9e64fe7dd419b4f0a77e29c76ef6fed9fb04f7082578e227c5a538f7c5dfd23cdd26fbd8e2b790961810e20bac9880b62fe3
7
- data.tar.gz: 35714111ea83705ddf8e136da7238e10f89cc8d78584f53eecdca67807f5d6dfe2d9b0b6c928027df7e4fb8455b2b72c03222ae9d194e44960d4934c8a81065d
6
+ metadata.gz: 71bd78e900870c20aa6efd634b67e0745677d7cb4ce48aeabc0273b866815176317635fddd29d65b934e42d14926e029bbf7a044cab145ad5d7740d14d88d17a
7
+ data.tar.gz: 3aa908abea767a78f8a2f999aefed68b5ba8aa1f4751c0c68cb62c2103cff5299ad6d597752f93cf848db89165456b3170015962febc30aa4792001067338479
@@ -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,4 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'powertool/maybe_chain'
2
- require_relative 'powertool/range'
4
+ require_relative 'powertool/range'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: powertools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - C. Kinniburgh
@@ -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
  - - ">="