rafini 3.0.210112 → 3.1.221212

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: 8daaac2f079c639abaa892b634f6400f1b0e75ffead282c4551dc8fb7fe45fda
4
+ data.tar.gz: a383dca85331bc2cf43e443fc909a9612044ea2c0f73a33a7bc9c568f2db0370
5
5
  SHA512:
6
- metadata.gz: eb0f6fcdf17d108b75ddd5af587ceb7fe2d6294e8134cfeae90807f62607d49e158754903526f49b22fb6522a4ded0cf2f1522ad9c1305f295b9bd858cad9714
7
- data.tar.gz: 8b327f489cfa71d77eca28df39b4408fcf8c7c4c8e7ed233c10f05e242bcfee0936a96f97e5bb8eee0fa4f49a058bc7b754fc549ab47d8e70574e756ccdc1d93
6
+ metadata.gz: b169dce8d8edd85971c8e62c433b3485a863fa8327e84430e253d660dbc0ca5a89c0106830ecedbfdb52103fd12bfb0193b07504dbc50b2812ced3a12096595c
7
+ data.tar.gz: c62702b40d3f826f4a63828e3122dada54376c8cd23f358b17a1c6650e6fc79bec2914f706c55d61c2ba0a087e88c2a848cdaec7410bda5b238d44b33f6b4f9d
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.1.221212](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,77 @@
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:
16
+ ```ruby
17
+ require 'rafini'
18
+ ```
19
+ ### include Rafini::Empty
20
+ ```ruby
21
+ include Rafini::Empty
18
22
 
23
+ s0 #=> ""
24
+ a0 #=> []
25
+ h0 #=> {}
26
+ [s0,a0,h0].all?(&:frozen?) #=> true
27
+ ```
19
28
  ### using Rafini::Array
20
-
21
29
  ```ruby
22
- require 'rafini/array'
23
30
  using Rafini::Array
24
31
 
32
+ # classify(like Set#classify)
33
+ [1, 2.0, 'Three', 4.0].classify #=> {Integer=>[1], Float=>[2.0, 4.0], String=>["Three"]}
34
+
35
+ # is
36
+ [:a,:b,:c].is(true) #=> {:a=>true, :b=>true, :c=>true}
37
+
25
38
  # joins
26
39
  ['Y','M','D','h','m','s'].joins('-','-',' '){':'}
27
40
  #=> "Y-M-D h:m:s"
28
41
  [1,9,2,8,3,7,4,6,5,5].joins{|a,b|a>b ? '>': a<b ? '<': '='}
29
42
  #=> "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
43
  ```
34
- ### Rafini::Empty
44
+ ### using Rafini::Hash
45
+ ```ruby
46
+ using Rafini::Hash
47
+
48
+ # to_struc
49
+ struct = {a:'A',b:'C',c:'C'}.to_struct{ def ok = "OK" }
50
+ struct #=> #<struct a="A", b="C", c="C">
51
+ struct.a #=> "A"
52
+ struct.ok #=> "OK"
53
+
54
+ # supplement
55
+ {a:'A',b:'B'}.supplement({b:'X',c:'C'},{c:'Y',d:'D'}) #=> {:a=>"A", :b=>"B", :c=>"C", :d=>"D"}
35
56
 
57
+ # amend
58
+ {a:'A',b:'B'}.amend({b:'X',c:'C'},{c:'Y',d:'D'}) #=> {:a=>"A", :b=>"X"}
59
+ ```
60
+ ### using Rafini::Integer
36
61
  ```ruby
37
- require 'rafini/empty'
38
- include Rafini::Empty
39
- s0 #=> ""
40
- a0 #=> []
41
- h0 #=> {}
42
- [s0,a0,h0].all?(&:frozen?) #=> true
62
+ using Rafini::Integer
63
+
64
+ # odometer
65
+ 123.odometer(10,10) #=> [3, 2, 1]
66
+ 30.odometer(2,3,5) #=> [0, 0, 0, 1]
43
67
  ```
68
+ ### using Rafini::String
69
+ ```ruby
70
+ using Rafini::String
44
71
 
45
- ### using Rafini::Exception
72
+ # camelize
73
+ 'a_camel_kick'.camelize #=> "ACamelKick"
74
+
75
+ # semantic
76
+ '1.2.3'.semantic(0..1) #=> "1.2"
46
77
 
78
+ # shellescape(like Shellwords.escape)
79
+ 'Hello World!'.shellescape #=> "Hello\\ World\\!"
80
+ ```
81
+ ### using Rafini::Exception
47
82
  ```ruby
48
- require 'rafini/exception'
49
83
  using Rafini::Exception
50
84
 
51
85
  # $!.puts
@@ -76,41 +110,8 @@ end
76
110
  # will not re-raise the error(but gives the error).
77
111
  thread.value.class #=> RuntimeError
78
112
  ```
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
113
  ### using Rafini::Odometers
111
-
112
114
  ```ruby
113
- require 'rafini/odometers'
114
115
  using Rafini::Odometers
115
116
 
116
117
  # sec2time
@@ -119,25 +120,20 @@ using Rafini::Odometers
119
120
  # illion
120
121
  3_512_325.illion.to_s #=> "3.51M"
121
122
  ```
122
-
123
- ### using Rafini::String
124
-
123
+ ### using Rafini::Requires
125
124
  ```ruby
126
- require 'rafini/string'
127
- using Rafini::String
125
+ using Rafini::Requires
128
126
 
129
- # camelize
130
- 'a_camel_kick'.camelize #=> "ACamelKick"
131
-
132
- # semantic
133
- '1.2.3'.semantic(0..1) #=> "1.2"
127
+ requires'
128
+ ruby ~>3.0
129
+ rafini ~>3.1
130
+ json ~>2.0' #=> ["json"]
134
131
  ```
135
-
136
132
  ## LICENSE:
137
133
 
138
134
  (The MIT License)
139
135
 
140
- Copyright (c) 2021 carlosjhr64
136
+ Copyright (c) 2022 CarlosJHR64
141
137
 
142
138
  Permission is hereby granted, free of charge, to any person obtaining
143
139
  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,42 @@
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
+ 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.1.221212'
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.1.221212
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-12 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: []