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 +4 -4
- data/lib/powertool/maybe_chain.rb +27 -23
- data/lib/powertool/range.rb +30 -39
- data/lib/powertools.rb +3 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3d16ccd25661c68f7d98b47bf6bf7dd3f2661afdce2d207e1c5590672196ad3b
|
4
|
+
data.tar.gz: 536553941e3cdcfe5b84a9da4a8f00c6fc8a39a43e9cab588de171f21b77b7b3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 71bd78e900870c20aa6efd634b67e0745677d7cb4ce48aeabc0273b866815176317635fddd29d65b934e42d14926e029bbf7a044cab145ad5d7740d14d88d17a
|
7
|
+
data.tar.gz: 3aa908abea767a78f8a2f999aefed68b5ba8aa1f4751c0c68cb62c2103cff5299ad6d597752f93cf848db89165456b3170015962febc30aa4792001067338479
|
@@ -1,27 +1,31 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
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
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
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
|
data/lib/powertool/range.rb
CHANGED
@@ -1,47 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Extensions to Ruby's Range class
|
1
4
|
module RangeExtensions
|
2
|
-
|
3
|
-
|
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
|
-
|
31
|
-
|
32
|
-
|
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
|
-
|
35
|
-
|
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
|
-
|
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
|
-
|
47
|
-
end
|
37
|
+
prepend RangeExtensions
|
38
|
+
end
|
data/lib/powertools.rb
CHANGED
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.
|
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:
|
35
|
+
version: 2.5.0
|
35
36
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
37
|
requirements:
|
37
38
|
- - ">="
|