rafini 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e7c87ce3266c62021f2ab485feacfcd7f6a612fa
4
+ data.tar.gz: d743b0e9354aca2a885d9c3fd4d472df72d66da5
5
+ SHA512:
6
+ metadata.gz: 10909f1435a0dcf339c1f47fbd955fe39497834e65a9bd5e31166b9d3ace6505cd4d6291c37c0a471f49f40f3a152b26625891944143a9cbdb77e709d71d8420
7
+ data.tar.gz: 299b09b91808ff9219d27f61ebbc3a3a28b14a6b574e4729421cc47e700372481cee73d191662a40ebe5285db0a2ef11f4974665ba27fc0b99190a289a454ef8
data/README.rdoc ADDED
@@ -0,0 +1,92 @@
1
+ = rafini
2
+
3
+ == DESCRIPTION:
4
+
5
+ Just a collection of useful refinements.
6
+
7
+ == SYNOPSIS:
8
+
9
+ === using Rafini::Array
10
+
11
+ # joins
12
+ ['a','b','c','d','e','f'].joins('-','-',' '){':'} #=> "a-b-c d:e:f"
13
+
14
+ # per
15
+ h={}
16
+ ['a','b','c'].per(['A','B','C']){|l,u| h[l]=u}
17
+ h #=> {'a'=>'A','b'=>'B','c'=>'C'}
18
+
19
+ === using Rafini::Exception
20
+
21
+ # $!.puts
22
+ begin
23
+ raise 'Ugly Message'
24
+ rescue RuntimeError
25
+ $!.puts 'Nice Message'
26
+ end
27
+
28
+ # Rafini.bang!
29
+ value = Rafini.bang!('Nice Message') do
30
+ raise 'Ugly Message'
31
+ end
32
+ value #=> return value of block or error object
33
+
34
+ # Rafini.thread_bang!
35
+ Rafini.thread_bang!('Nice Message') do
36
+ # this is in a thread
37
+ raise 'Ugly Message'
38
+ end
39
+
40
+ === using Rafini::Hash
41
+
42
+ # to_struc
43
+ struct = {a:'A',b:'C',c:'C'}.to_struct
44
+ struct.a #=> 'A'
45
+
46
+ === using Rafini::Integer
47
+
48
+ # odometer
49
+ 123.odometer(10,10) #=> [3,2,1]
50
+ 30.odometer(2,3,5) #=> [0,0,0,1]
51
+
52
+ === using Rafini::Odometers
53
+
54
+ # sec2time
55
+ 12501.sec2time.to_s #=> "3 hours and 28 minutes"
56
+
57
+ # illion
58
+ 3_512_325.illion.to_s #=> "3.51M"
59
+
60
+ === using Rafini::String
61
+
62
+ # camelize
63
+ 'a_camel_kick'.camelize #=> "ACamelKick"
64
+
65
+ == INSTALL:
66
+
67
+ $ sudo gem install rafini
68
+
69
+ == LICENSE:
70
+
71
+ (The MIT License)
72
+
73
+ Copyright (c) 2014 carlosjhr64
74
+
75
+ Permission is hereby granted, free of charge, to any person obtaining
76
+ a copy of this software and associated documentation files (the
77
+ 'Software'), to deal in the Software without restriction, including
78
+ without limitation the rights to use, copy, modify, merge, publish,
79
+ distribute, sublicense, and/or sell copies of the Software, and to
80
+ permit persons to whom the Software is furnished to do so, subject to
81
+ the following conditions:
82
+
83
+ The above copyright notice and this permission notice shall be
84
+ included in all copies or substantial portions of the Software.
85
+
86
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
87
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
88
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
89
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
90
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
91
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
92
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,42 @@
1
+ module Rafini
2
+ module Array
3
+ refine ::Array do
4
+
5
+ # string = array.joins(sep1,sep2,sep3,...){|i| sep[i]}
6
+ #
7
+ # Returns a string created by converting each element of the array to
8
+ # a string, separated by the given separators.
9
+ # If no separators are given or are used up,
10
+ # it uses the value of the executed block, which is passed an iteration number.
11
+ # Note that the iteration number starts at 1.
12
+ # Lastly it uses an empty string.
13
+ # ['a','b','c','d','e','f'].joins('-', '-', ' '){':'} #=> 'a-b-c d:e:f'
14
+ # ['a','b','c'].joins{','} #=> 'a,b,c'
15
+ # ['1','2','3'].joins('.') #=> '1.23'
16
+ # ['a','b','c'].joins{|i|i} #=> 'a1b2c'
17
+ def joins(*p, &block)
18
+ str = ''
19
+ if length > 1
20
+ str << self[0]
21
+ 1.upto(length-1) do |i|
22
+ str << (p.empty? ? (block ? block.call(i).to_s : '') : p.shift.to_s)
23
+ str << self[i]
24
+ end
25
+ end
26
+ return str
27
+ end
28
+
29
+ # array1.per(array2){|obj1, obj2| ... }
30
+ #
31
+ # Gives the block each two elements of two array respectively.
32
+ # If the second array is not given, it passes the block the index number instead.
33
+ # h={} # say you have a hash h, then
34
+ # ['a','b','c'].per(['A','B','C']){|l,n| h[l]=n} # h=={'a'=>'A','b'=>'B','c'=>'C'}
35
+ # ['a','b','c'].per{|l,i| h[l]=i} # h=={'a'=>0,'b'=>1,'c'=>2}
36
+ def per(b=nil)
37
+ 0.upto(length-1){|i| yield self[i], (b)? b[i] : i}
38
+ end
39
+
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,58 @@
1
+ module Rafini
2
+ module Exception
3
+ refine ::Exception do
4
+
5
+ # $!.puts outputs to standard error what went bang!
6
+ # The given message is what you normally want to see.
7
+ # The exeption message is also shown if in verbose mode.
8
+ # Backtrace is shown if in debug mode.
9
+ # begin
10
+ # raise 'Ugly message'
11
+ # rescue RuntimeError
12
+ # # exact output depends on $VERBOSE and $DEBUG
13
+ # $!.puts('Nice message')
14
+ # end
15
+ def puts(message=nil)
16
+ unless $VERBOSE.nil? then
17
+ $stderr.puts message if message
18
+ $stderr.puts self.message if $VERBOSE or !message
19
+ $stderr.puts self.backtrace.to_s if $DEBUG
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ using Rafini::Exception
27
+
28
+ # Module version of puts bang!
29
+ # Returns either the value or error of the block.
30
+ # value = Rafini.bang!('Ooops! Not perfect?') do
31
+ # # Perfect code here...
32
+ # end
33
+ def Rafini.bang!(message=nil, bang=Exception, &block)
34
+ value = nil
35
+ begin
36
+ value = block.call
37
+ rescue bang => value
38
+ value.puts(message)
39
+ end
40
+ return value
41
+ end
42
+
43
+ # The Thread wrapped version of bang!
44
+ # I often do
45
+ # Thread.new do
46
+ # begin
47
+ # ... stuff ..
48
+ # rescue Exception
49
+ # puts 'blah blah...'
50
+ # puts $!.message if $VERBOSE
51
+ # puts $!.backtrace if $DEBUG
52
+ # end
53
+ # end
54
+ # With the following below, I'll be able to say
55
+ # Rafini.thread_bang!('blah blah...'){ ...stuff... }
56
+ def Rafini.thread_bang!(header=nil, bang=Exception, &block)
57
+ Thread.new{Rafini.bang!(header, bang, &block)}
58
+ end
@@ -0,0 +1,12 @@
1
+ module Rafini
2
+ module Hash
3
+ refine ::Hash do
4
+
5
+ # struct = hash.to_struct
6
+ # Why not?
7
+ def to_struct
8
+ Struct.new(*self.keys).new(*self.values)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,27 @@
1
+ module Rafini
2
+ module Integer
3
+ refine ::Integer do
4
+
5
+ # odometer
6
+ # Kinda hard to explain...
7
+ # 123.odometer(10,10) #=> [3,2,1]
8
+ # 30.odometer(2,3,5) #=> [0,0,0,1]
9
+ # ((60*60*24)*3 + (60*60)*12 + 60*15 + 30).odometer(60,60,24) #=> [30, 15, 12, 3]
10
+ # Useful for making clocks, number scales, mayan long count... etc.
11
+ def odometer(*p)
12
+ n = self
13
+ m = p.inject(1,:*)
14
+ r = []
15
+
16
+ (p.length-1).downto(0) do |i|
17
+ y = n/m; r.unshift y
18
+ n = n%m
19
+ f = p[i]; m = m/f
20
+ end
21
+ r.unshift n
22
+
23
+ return r
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,128 @@
1
+ module Rafini
2
+ module Odometers
3
+ refine ::Integer do
4
+ # Need Rafini::Integer for #odometer
5
+ # Need Rafini::Hash for #to_struct
6
+ # Need Rafini::Array for #per
7
+ [Rafini::Integer, Rafini::Hash, Rafini::Array].each{|mod| using mod}
8
+
9
+ def odoread(scale)
10
+ values = scale.values
11
+ keys = scale.keys;
12
+ counts = self.odometer(*values[0..-2])
13
+
14
+ string = "#{counts[0]} #{keys[0]}#{(counts[0]==1)? '' : 's'}"
15
+ (keys.length-1).downto(1) do |i|
16
+ if counts[i] > 0
17
+ string = "#{counts[i]} #{keys[i]}#{(counts[i]>1)? 's' : ''}"
18
+ string << " and #{counts[i-1]} #{keys[i-1]}#{(counts[i-1]>1)? 's' : ''}" if counts[i-1]>0
19
+ break
20
+ end
21
+ end
22
+
23
+ hash = {}
24
+ keys.per(counts){|k,v| hash[k]=v}
25
+ hash[:to_s]=string
26
+
27
+ return hash.to_struct
28
+ end
29
+
30
+ SEC2TIME = {
31
+ second: 60,
32
+ minute: 60,
33
+ hour: 24,
34
+ day: 7,
35
+ week: 4,
36
+ month: 13,
37
+ year: 10,
38
+ decade: 10,
39
+ centurie: 10,
40
+ millennium: 10,
41
+ age: 10,
42
+ epoch: 10,
43
+ era: 5,
44
+ eon: 2,
45
+ gigaannum: nil,
46
+ }
47
+
48
+ # Integer#sec2time
49
+ # Returns a struct with the different time scales for number of seconds.
50
+ # Note that the month(4 weeks)/year(13 months) are not meant to be exact.
51
+ # 10_000.sec2time.to_s #=> "2 hours and 46 minutes"
52
+ # 10_000.sec2time.hour #=> 2
53
+ def sec2time
54
+ self.odoread(SEC2TIME)
55
+ end
56
+
57
+ SCALE = {
58
+ base: {
59
+ ones: 10,
60
+ tens: 10,
61
+ hundreds: 10,
62
+ thousands: 1_000,
63
+ },
64
+ short: {
65
+ millions: 1_000,
66
+ billions: 1_000,
67
+ trillions: 1_000,
68
+ quadrillions: nil,
69
+ },
70
+ long: {
71
+ millions: 1_000_000,
72
+ billions: 1_000_000,
73
+ trillions: 1_000_000,
74
+ quadrillions: nil,
75
+ },
76
+ }
77
+
78
+ # 1_230.illion.to_s #=> "1.23k"
79
+ # 1_230_000.illion.to_s #=> "1.23M"
80
+ # ...etc
81
+ # Does both short and long scales, short by default.
82
+ # Returns a struct with the different scales of the number
83
+ # m = 888_777_666_555_444_321.illion
84
+ # m.ones #=> 1
85
+ # m.tens #=> 2
86
+ # m.hundreds #=> 3
87
+ # m.thousands #=> 444
88
+ # m.millions #=> 555
89
+ # m.billions #=> 666
90
+ # m.trillions #=> 777
91
+ # m.quadrillions #=> 888
92
+ # m.to_s #=> "888Q" It rounds up 888.7!
93
+ def illion(type=:short)
94
+ keys = SCALE[:base].keys + SCALE[type].keys
95
+ values = SCALE[:base].values + SCALE[type].values
96
+ counts = self.odometer(*values[0..-2])
97
+
98
+ string = nil
99
+ if self < 1_000
100
+ string = self.to_s
101
+ elsif self < 1_000_000
102
+ d = (self<10_000)? 2 : (self<100_000)? 1 : 0
103
+ m = (self/1000.0).round(d)
104
+ string = "#{m}k"
105
+ else
106
+ (keys.length-1).downto(4) do |i|
107
+ next unless counts[i]>0
108
+ n = counts[i]
109
+ if n < 1_000
110
+ d = (n<10)? 2 : (n<100)? 1 : 0
111
+ n = (n + counts[i-1]/values[i-1].to_f).round(d)
112
+ else
113
+ n = n.illion
114
+ end
115
+ string = "#{n}#{keys[i][0].upcase}"
116
+ break
117
+ end
118
+ end
119
+
120
+ hash = {}
121
+ keys.per(counts){|k,v| hash[k]=v}
122
+ hash[:to_s] = string
123
+
124
+ return hash.to_struct
125
+ end
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,12 @@
1
+ module Rafini
2
+ module String
3
+ refine ::String do
4
+ # camelize:
5
+ # 1) A camel kick, as in "I gotz camelized".
6
+ # 2) "a_camel_kick" => "ACamelKick"
7
+ def camelize(sep=/_/)
8
+ self.split(sep).map{ |word| word.capitalize }.join('')
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module Rafini
2
+ VERSION = '0.0.0'
3
+ end
data/lib/rafini.rb ADDED
@@ -0,0 +1,12 @@
1
+ require 'rafini/version'
2
+
3
+ require 'rafini/array'
4
+ require 'rafini/exception'
5
+ require 'rafini/hash'
6
+ require 'rafini/integer'
7
+ require 'rafini/string'
8
+
9
+ require 'rafini/odometers'
10
+
11
+ # Requires:
12
+ #`ruby`
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rafini
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - carlosjhr64
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-11 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |
14
+ Just a collection of useful refinements.
15
+ email: carlosjhr64@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files:
19
+ - README.rdoc
20
+ files:
21
+ - README.rdoc
22
+ - lib/rafini.rb
23
+ - lib/rafini/array.rb
24
+ - lib/rafini/exception.rb
25
+ - lib/rafini/hash.rb
26
+ - lib/rafini/integer.rb
27
+ - lib/rafini/odometers.rb
28
+ - lib/rafini/string.rb
29
+ - lib/rafini/version.rb
30
+ homepage: https://github.com/carlosjhr64/rafini
31
+ licenses:
32
+ - MIT
33
+ metadata: {}
34
+ post_install_message:
35
+ rdoc_options:
36
+ - "--main"
37
+ - README.rdoc
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements:
51
+ - 'ruby: ruby 2.1.3p242 (2014-09-19 revision 47630) [x86_64-linux]'
52
+ rubyforge_project:
53
+ rubygems_version: 2.4.1
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: Just a collection of useful refinements.
57
+ test_files: []
58
+ has_rdoc: