rafini 3.0.210112 → 3.2.221213

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: df4dc73d0b92dcd762d1c741eb30f8dcfc56fdd84d18bfdbc2435290d9a3df97
4
- data.tar.gz: c7764c8ae13bb99ff51977d7b52f926eee9083e5db6f75e43c860c2eb9557715
3
+ metadata.gz: bdc3386e65af02b3a86031e7123e10391dc321de2b7559fb83e0639520db56a9
4
+ data.tar.gz: 440f1bd45fee01d4ed81bbb536672367419e5cef35dc973277d3f12a950124d4
5
5
  SHA512:
6
- metadata.gz: eb0f6fcdf17d108b75ddd5af587ceb7fe2d6294e8134cfeae90807f62607d49e158754903526f49b22fb6522a4ded0cf2f1522ad9c1305f295b9bd858cad9714
7
- data.tar.gz: 8b327f489cfa71d77eca28df39b4408fcf8c7c4c8e7ed233c10f05e242bcfee0936a96f97e5bb8eee0fa4f49a058bc7b754fc549ab47d8e70574e756ccdc1d93
6
+ metadata.gz: 29397cbfc1807873786d8770fd0b2fe5d2a1712492e1079cb568d6f39e8147abca80240cdd7f7885b16d2c90e4e453b7026cfcdc296ec921ee8bf17a9cd2b411
7
+ data.tar.gz: ece7843e819e9c4745286f2f77052b9fbdd1a521d3f174fd8d652dd5d7d63650185a09410d457150fca8458f273965c9af6252f5883d56e3e33dcafc9fa8008b
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Rafini
2
2
 
3
- * [VERSION 3.0.210112](https://github.com/carlosjhr64/rafini/releases)
3
+ * [VERSION 3.2.221213](https://github.com/carlosjhr64/rafini/releases)
4
4
  * [github](https://github.com/carlosjhr64/rafini)
5
5
  * [rubygems](https://rubygems.org/gems/rafini)
6
6
 
@@ -9,43 +9,79 @@
9
9
  Just a collection of useful refinements.
10
10
 
11
11
  ## INSTALL:
12
-
13
12
  ```shell
14
13
  $ gem install rafini
15
14
  ```
16
-
17
15
  ## SYNOPSIS:
18
16
 
19
- ### using Rafini::Array
17
+ Note that `Rafini` auto-loads assets as requested.
18
+ ```ruby
19
+ require 'rafini'
20
+ ```
21
+ ### include Rafini::Empty
22
+ ```ruby
23
+ include Rafini::Empty
20
24
 
25
+ s0 #=> ""
26
+ a0 #=> []
27
+ h0 #=> {}
28
+ [s0,a0,h0].all?(&:frozen?) #=> true
29
+ ```
30
+ ### using Rafini::Array
21
31
  ```ruby
22
- require 'rafini/array'
23
32
  using Rafini::Array
24
33
 
34
+ # classify(like Set#classify)
35
+ [1, 2.0, 'Three', 4.0].classify #=> {Integer=>[1], Float=>[2.0, 4.0], String=>["Three"]}
36
+
37
+ # is
38
+ [:a,:b,:c].is(true) #=> {:a=>true, :b=>true, :c=>true}
39
+
25
40
  # joins
26
41
  ['Y','M','D','h','m','s'].joins('-','-',' '){':'}
27
42
  #=> "Y-M-D h:m:s"
28
43
  [1,9,2,8,3,7,4,6,5,5].joins{|a,b|a>b ? '>': a<b ? '<': '='}
29
44
  #=> "1<9>2<8>3<7>4<6>5=5"
30
-
31
- # is
32
- [:a,:b,:c].is(true) #=> {:a=>true, :b=>true, :c=>true}
33
45
  ```
34
- ### Rafini::Empty
46
+ ### using Rafini::Hash
47
+ ```ruby
48
+ using Rafini::Hash
35
49
 
50
+ # to_struc
51
+ struct = {a:'A',b:'C',c:'C'}.to_struct{ def ok = "OK" }
52
+ struct #=> #<struct a="A", b="C", c="C">
53
+ struct.a #=> "A"
54
+ struct.ok #=> "OK"
55
+
56
+ # supplement
57
+ {a:'A',b:'B'}.supplement({b:'X',c:'C'},{c:'Y',d:'D'}) #=> {:a=>"A", :b=>"B", :c=>"C", :d=>"D"}
58
+
59
+ # amend
60
+ {a:'A',b:'B'}.amend({b:'X',c:'C'},{c:'Y',d:'D'}) #=> {:a=>"A", :b=>"X"}
61
+ ```
62
+ ### using Rafini::Integer
36
63
  ```ruby
37
- require 'rafini/empty'
38
- include Rafini::Empty
39
- s0 #=> ""
40
- a0 #=> []
41
- h0 #=> {}
42
- [s0,a0,h0].all?(&:frozen?) #=> true
64
+ using Rafini::Integer
65
+
66
+ # odometer
67
+ 123.odometer(10,10) #=> [3, 2, 1]
68
+ 30.odometer(2,3,5) #=> [0, 0, 0, 1]
43
69
  ```
70
+ ### using Rafini::String
71
+ ```ruby
72
+ using Rafini::String
44
73
 
45
- ### using Rafini::Exception
74
+ # camelize
75
+ 'a_camel_kick'.camelize #=> "ACamelKick"
46
76
 
77
+ # semantic
78
+ '1.2.3'.semantic(0..1) #=> "1.2"
79
+
80
+ # shellescape(like Shellwords.escape)
81
+ 'Hello World!'.shellescape #=> "Hello\\ World\\!"
82
+ ```
83
+ ### using Rafini::Exception
47
84
  ```ruby
48
- require 'rafini/exception'
49
85
  using Rafini::Exception
50
86
 
51
87
  # $!.puts
@@ -76,41 +112,8 @@ end
76
112
  # will not re-raise the error(but gives the error).
77
113
  thread.value.class #=> RuntimeError
78
114
  ```
79
-
80
- ### using Rafini::Hash
81
-
82
- ```ruby
83
- require 'rafini/hash'
84
- using Rafini::Hash
85
-
86
- # to_struc
87
- struct = {a:'A',b:'C',c:'C'}.to_struct{ def ok = "OK" }
88
- struct #=> #<struct a="A", b="C", c="C">
89
- struct.a #=> "A"
90
- struct.ok #=> "OK"
91
-
92
- # supplement
93
- {a:'A',b:'B'}.supplement({b:'X',c:'C'},{c:'Y',d:'D'}) #=> {:a=>"A", :b=>"B", :c=>"C", :d=>"D"}
94
-
95
- # amend
96
- {a:'A',b:'B'}.amend({b:'X',c:'C'},{c:'Y',d:'D'}) #=> {:a=>"A", :b=>"X"}
97
- ```
98
-
99
- ### using Rafini::Integer
100
-
101
- ```ruby
102
- require 'rafini/integer'
103
- using Rafini::Integer
104
-
105
- # odometer
106
- 123.odometer(10,10) #=> [3, 2, 1]
107
- 30.odometer(2,3,5) #=> [0, 0, 0, 1]
108
- ```
109
-
110
115
  ### using Rafini::Odometers
111
-
112
116
  ```ruby
113
- require 'rafini/odometers'
114
117
  using Rafini::Odometers
115
118
 
116
119
  # sec2time
@@ -119,25 +122,25 @@ using Rafini::Odometers
119
122
  # illion
120
123
  3_512_325.illion.to_s #=> "3.51M"
121
124
  ```
122
-
123
- ### using Rafini::String
124
-
125
+ ### using Rafini::Requires
125
126
  ```ruby
126
- require 'rafini/string'
127
- using Rafini::String
128
-
129
- # camelize
130
- 'a_camel_kick'.camelize #=> "ACamelKick"
131
-
132
- # semantic
133
- '1.2.3'.semantic(0..1) #=> "1.2"
127
+ using Rafini::Requires
128
+
129
+ requires'
130
+ ruby ~>3.0
131
+ rafini ~>3.2
132
+ json ~>2.0' #=> ["json"]
133
+ ## You can also say:
134
+ # require 'rafini/requires'
135
+ # Rafini.requires...
136
+ ## But you wont be able to test for Rafini's VERSION
137
+ ## unless you've also required 'rafini'.
134
138
  ```
135
-
136
139
  ## LICENSE:
137
140
 
138
141
  (The MIT License)
139
142
 
140
- Copyright (c) 2021 carlosjhr64
143
+ Copyright (c) 2022 CarlosJHR64
141
144
 
142
145
  Permission is hereby granted, free of charge, to any person obtaining
143
146
  a copy of this software and associated documentation files (the
data/lib/rafini/array.rb CHANGED
@@ -1,13 +1,32 @@
1
1
  module Rafini
2
2
  module Array
3
3
  refine ::Array do
4
+ # classify:
5
+ # Like Set#classify
6
+ def classify(hash: ::Hash.new{|h,k|h[k]=[]}, &block)
7
+ block ||= lambda{|v|v.class}
8
+ self.each{|v| hash[block[v]] << v}
9
+ return hash
10
+ end
11
+
12
+ # is:
13
+ # [:a,:b,:c].is(true) #=> {:a=>true,:b=>true,:c=>true}
14
+ #
15
+ # Updates a hash with the keys given by the array to the given value.
16
+ def is(value, hash={})
17
+ each{|key| hash[key]=value}
18
+ return hash
19
+ end
20
+
21
+ # joins:
4
22
  # type _AToS = ::Array[(_AToS|_ToS)]
5
23
  # _AToS#joins(*_AToS seps)?{(_ToS,_ToS)->_ToS}
6
24
  #
7
25
  # Returns a string created by joining the elements of the (flatten) array,
8
26
  # separated by the given (flatten) separators.
9
27
  # If no separators are given or are used up,
10
- # it uses the value of the executed block, which is passed the next neigboring iteration items.
28
+ # it uses the value of the executed block,
29
+ # which is passed the next neigboring iteration items.
11
30
  # Else, it just joins the items.
12
31
  # ['2021','Jan','09','07','29','05'].joins('-', '-', ' '){':'}
13
32
  # #=> "2021-Jan-09 07:29:05"
@@ -23,20 +42,14 @@ module Rafini
23
42
  return string if items.empty?
24
43
  seps.flatten!
25
44
  while item = items.shift
26
- sep = seps.shift&.to_s || block&.call(previous,item).to_s and string << sep
45
+ if sep = seps.shift&.to_s || block&.call(previous,item)&.to_s
46
+ string << sep
47
+ end
27
48
  string << item.to_s
28
49
  previous = item
29
50
  end
30
51
  return string
31
52
  end
32
-
33
- # [:a,:b,:c].is(true) #=> {:a=>true,:b=>true,:c=>true}
34
- #
35
- # Updates a hash with the keys given by the array to the given value.
36
- def is(value, hash={})
37
- each{|key| hash[key]=value}
38
- return hash
39
- end
40
53
  end
41
54
  end
42
55
  end
@@ -29,7 +29,7 @@ using Rafini::Exception
29
29
  # value = Rafini.bang!('Ooops! Not perfect?') do
30
30
  # # Perfect code here...
31
31
  # end
32
- def Rafini.bang!(message=nil, bang=Exception, &block)
32
+ def Rafini.bang!(message=nil, bang=::Exception, &block)
33
33
  value = nil
34
34
  begin
35
35
  value = block.call
@@ -52,6 +52,6 @@ end
52
52
  # end
53
53
  # With the following below, I'll be able to say
54
54
  # Rafini.thread_bang!('blah blah...'){ ...stuff... }
55
- def Rafini.thread_bang!(header=nil, bang=Exception, &block)
55
+ def Rafini.thread_bang!(header=nil, bang=::Exception, &block)
56
56
  Thread.new{Rafini.bang!(header, bang, &block)}
57
57
  end
data/lib/rafini/hash.rb CHANGED
@@ -11,7 +11,8 @@ module Rafini
11
11
  #
12
12
  # Supplements hash with hashes.
13
13
  # Adds missing elements only.
14
- # {a:'A',b:'B'}.supplement({b:'X',c:'C'},{c:'Y',d:'D'}) #=> {a:'A',b:'B',c:'C',d:'D'}
14
+ # {a:'A',b:'B'}.supplement({b:'X',c:'C'},{c:'Y',d:'D'})
15
+ # #=> {a:'A',b:'B',c:'C',d:'D'}
15
16
  def supplement!(*hashes)
16
17
  hashes.each do |hash|
17
18
  hash.each do |key, value|
@@ -27,7 +28,8 @@ module Rafini
27
28
  # hash0.amend(hash1,...)
28
29
  #
29
30
  # Ammends hash with hashes.
30
- # Overwrites existing keys only with first key value found in amending hashes.
31
+ # Overwrites existing keys
32
+ # only with first key value found in amending hashes.
31
33
  # {a:'A',b:'B'}.amend({b:'X',c:'C'},{c:'Y',d:'D'}) #=> {a:'A',b:'X'}
32
34
  def amend!(*hashes)
33
35
  self.each_key do |key|
@@ -5,7 +5,8 @@ module Rafini
5
5
  # Kinda hard to explain...
6
6
  # 123.odometer(10,10) #=> [3,2,1]
7
7
  # 30.odometer(2,3,5) #=> [0,0,0,1]
8
- # ((60*60*24)*3 + (60*60)*12 + 60*15 + 30).odometer(60,60,24) #=> [30, 15, 12, 3]
8
+ # ((60*60*24)*3 + (60*60)*12 + 60*15 + 30).odometer(60,60,24)
9
+ # #=> [30, 15, 12, 3]
9
10
  # Useful for making clocks, number scales, mayan long count... etc.
10
11
  def odometer(*levels, factors: true)
11
12
  raise RangeError, 'negative odometer' if self < 0
@@ -0,0 +1,45 @@
1
+ module Rafini
2
+ module Requires
3
+ refine ::String do
4
+ # satisfies?:
5
+ # "1.2.3".satisfies?('~>1.1') #=> true
6
+ # "1.2.3".satisfies?('~>2.3') #=> false
7
+ def satisfies?(*reqs)
8
+ Gem::Requirement.new(*reqs).satisfied_by? Gem::Version.new(self)
9
+ end
10
+ end
11
+
12
+ refine ::Kernel do
13
+ def requires(*list)
14
+ loaded = []
15
+ list.each do |gems|
16
+ gems.lines.each do |gemname_reqs|
17
+ gemname, *reqs = gemname_reqs.split
18
+ next unless gemname
19
+ unless reqs.empty?
20
+ case gemname
21
+ when 'rafini'
22
+ unless VERSION.satisfies?(*reqs)
23
+ raise "helpema #{VERSION} not #{reqs.join(', ')}"
24
+ end
25
+ next
26
+ when 'ruby'
27
+ unless RUBY_VERSION.satisfies?(*reqs)
28
+ raise "ruby #{RUBY_VERSION} not #{reqs.join(', ')}"
29
+ end
30
+ next
31
+ else
32
+ gem gemname, *reqs
33
+ end
34
+ end
35
+ require gemname and loaded.push gemname
36
+ end
37
+ end
38
+ return loaded
39
+ end
40
+ end
41
+ end
42
+
43
+ using Requires
44
+ def Rafini.requires(*list) = Kernel.requires(*list)
45
+ end
data/lib/rafini/string.rb CHANGED
@@ -16,6 +16,13 @@ module Rafini
16
16
  def semantic(v=(0..2), split:'.', join:'.')
17
17
  [*self.split(split)[v]].join(join)
18
18
  end
19
+
20
+ # shellescape:
21
+ # Same funtionality as Shellword's String#shellescape
22
+ def shellescape
23
+ # This is a contraction of Shellwords.escape function
24
+ self.gsub(/[^\w\-.,:+\/@\n]/,'\\\\\\&').gsub(/\n/,"'\n'")
25
+ end
19
26
  end
20
27
  end
21
28
  end
data/lib/rafini.rb CHANGED
@@ -1,12 +1,17 @@
1
1
  module Rafini
2
- VERSION = '3.0.210112'
2
+ VERSION = '3.2.221213'
3
+ # Constants
4
+ autoload :Empty, 'rafini/empty'
5
+ # Pure
3
6
  autoload :Array, 'rafini/array'
4
- autoload :Exception, 'rafini/exception'
5
7
  autoload :Hash, 'rafini/hash'
6
8
  autoload :Integer, 'rafini/integer'
7
9
  autoload :String, 'rafini/string'
10
+ # Hybrid
11
+ autoload :Exception, 'rafini/exception'
12
+ # Mix
8
13
  autoload :Odometers, 'rafini/odometers'
9
- autoload :Empty, 'rafini/empty'
14
+ autoload :Requires, 'rafini/requires'
10
15
  end
11
16
  # Requires:
12
17
  #`ruby`
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rafini
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.210112
4
+ version: 3.2.221213
5
5
  platform: ruby
6
6
  authors:
7
- - carlosjhr64
8
- autorequire:
7
+ - CarlosJHR64
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-01-12 00:00:00.000000000 Z
11
+ date: 2022-12-13 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: 'Just a collection of useful refinements.
14
14
 
@@ -26,12 +26,13 @@ files:
26
26
  - lib/rafini/hash.rb
27
27
  - lib/rafini/integer.rb
28
28
  - lib/rafini/odometers.rb
29
+ - lib/rafini/requires.rb
29
30
  - lib/rafini/string.rb
30
31
  homepage: https://github.com/carlosjhr64/rafini
31
32
  licenses:
32
33
  - MIT
33
34
  metadata: {}
34
- post_install_message:
35
+ post_install_message:
35
36
  rdoc_options: []
36
37
  require_paths:
37
38
  - lib
@@ -46,9 +47,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
46
47
  - !ruby/object:Gem::Version
47
48
  version: '0'
48
49
  requirements:
49
- - 'ruby: ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-linux]'
50
- rubygems_version: 3.2.3
51
- signing_key:
50
+ - 'ruby: ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [aarch64-linux]'
51
+ rubygems_version: 3.3.7
52
+ signing_key:
52
53
  specification_version: 4
53
54
  summary: Just a collection of useful refinements.
54
55
  test_files: []