powertools 0.0.5 → 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: ecf303fcd15e95c1a56ec40da7400d109f7d885e4d1429b0665f98c13530533c
4
- data.tar.gz: 308dbf034e3fd9d2ca673fa65d50b9101cee87068199a770a898374c7646cf71
3
+ metadata.gz: 3d16ccd25661c68f7d98b47bf6bf7dd3f2661afdce2d207e1c5590672196ad3b
4
+ data.tar.gz: 536553941e3cdcfe5b84a9da4a8f00c6fc8a39a43e9cab588de171f21b77b7b3
5
5
  SHA512:
6
- metadata.gz: 3a0eff1867c0c5a87ab6ef75b346e6bb310293d330cb21daa294cf25694edb88cd9edca3622ee25358d0676a01f0817b5c53be04e0ce9ce6b9b4fec639e9178c
7
- data.tar.gz: c0b0897664c4cea460b714575f98861f6c0229497f7133355a7493148fc3ca93434420c9501975cb06fc32671e6c57da102b3e32cfff91b2b309a08097845d91
6
+ metadata.gz: 71bd78e900870c20aa6efd634b67e0745677d7cb4ce48aeabc0273b866815176317635fddd29d65b934e42d14926e029bbf7a044cab145ad5d7740d14d88d17a
7
+ data.tar.gz: 3aa908abea767a78f8a2f999aefed68b5ba8aa1f4751c0c68cb62c2103cff5299ad6d597752f93cf848db89165456b3170015962febc30aa4792001067338479
@@ -0,0 +1,31 @@
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]
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
22
+ end
23
+
24
+ # Ruby's Array class
25
+ class Array
26
+ def maybe_chain(args = [])
27
+ return nil unless self
28
+
29
+ PowertoolMaybeChain.maybe_chain(self, args)
30
+ end
31
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Extensions to Ruby's Range class
4
+ module RangeExtensions
5
+ def each(warn_on_order_error: true)
6
+ return super() if first <= last || first.class != last.class
7
+
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
16
+
17
+ return super()
18
+ end
19
+
20
+ yield(prev_position)
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
33
+ end
34
+
35
+ # Ruby's Range class
36
+ class Range
37
+ prepend RangeExtensions
38
+ end
data/lib/powertools.rb ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'powertool/maybe_chain'
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.0.5
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - C. Kinniburgh
@@ -16,12 +16,14 @@ executables: []
16
16
  extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
- - lib/maybe_chain.rb
20
- - lib/range.rb
19
+ - lib/powertool/maybe_chain.rb
20
+ - lib/powertool/range.rb
21
+ - lib/powertools.rb
21
22
  homepage: http://github.com/cjkinni/powertools
22
23
  licenses:
23
24
  - MIT
24
- metadata: {}
25
+ metadata:
26
+ rubygems_mfa_required: 'true'
25
27
  post_install_message:
26
28
  rdoc_options: []
27
29
  require_paths:
@@ -30,7 +32,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
30
32
  requirements:
31
33
  - - ">="
32
34
  - !ruby/object:Gem::Version
33
- version: '0'
35
+ version: 2.5.0
34
36
  required_rubygems_version: !ruby/object:Gem::Requirement
35
37
  requirements:
36
38
  - - ">="
data/lib/maybe_chain.rb DELETED
@@ -1,27 +0,0 @@
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
12
- end
13
- end
14
-
15
- 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
data/lib/range.rb DELETED
@@ -1,47 +0,0 @@
1
- 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
29
-
30
- yield(prev_position)
31
- end
32
- end
33
-
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
40
-
41
- (self.last..self.first)
42
- end
43
- end
44
-
45
- class Range
46
- prepend RangeExtensions
47
- end