small 0.1 → 0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +2 -1
- data/lib/small.rb +3 -1
- data/lib/small/integer.rb +39 -0
- data/lib/small/string.rb +7 -3
- data/lib/small/time.rb +31 -0
- metadata +5 -3
data/README.md
CHANGED
data/lib/small.rb
CHANGED
@@ -0,0 +1,39 @@
|
|
1
|
+
class Integer
|
2
|
+
|
3
|
+
def year
|
4
|
+
# http://www.wolframalpha.com/input/?i=year
|
5
|
+
self * 31536000
|
6
|
+
end
|
7
|
+
|
8
|
+
def month
|
9
|
+
self * 3600 * 24 * 30
|
10
|
+
end
|
11
|
+
|
12
|
+
def week
|
13
|
+
self * 3600 * 24 * 7
|
14
|
+
end
|
15
|
+
|
16
|
+
def day
|
17
|
+
self * 3600 * 24
|
18
|
+
end
|
19
|
+
|
20
|
+
def hour
|
21
|
+
self * 3600
|
22
|
+
end
|
23
|
+
|
24
|
+
def minute
|
25
|
+
self * 60
|
26
|
+
end
|
27
|
+
|
28
|
+
def second
|
29
|
+
self * 1
|
30
|
+
end
|
31
|
+
|
32
|
+
alias :years :year
|
33
|
+
alias :months :month
|
34
|
+
alias :weeks :week
|
35
|
+
alias :days :day
|
36
|
+
alias :hours :hour
|
37
|
+
alias :minutes :minute
|
38
|
+
alias :seconds :second
|
39
|
+
end
|
data/lib/small/string.rb
CHANGED
@@ -18,7 +18,11 @@ class String
|
|
18
18
|
|
19
19
|
def constantize
|
20
20
|
constant = Object
|
21
|
-
constant.const_defined?(self.to_s)
|
21
|
+
if constant.const_defined?(self.to_s)
|
22
|
+
constant.const_get(self.to_s)
|
23
|
+
else
|
24
|
+
constant.const_missing(self.to_s)
|
25
|
+
end
|
22
26
|
end
|
23
27
|
|
24
28
|
def encode64
|
@@ -35,12 +39,12 @@ class String
|
|
35
39
|
end
|
36
40
|
|
37
41
|
def to_sha2(provider = 256)
|
38
|
-
require 'digest/sha2' unless defined?(Digest::SHA256)
|
42
|
+
require 'digest/sha2' unless defined?(Digest::SHA256)
|
39
43
|
object = case provider
|
40
44
|
when 256 then Digest::SHA256.new
|
41
45
|
when 384 then Digest::SHA384.new
|
42
46
|
when 512 then Digest::SHA512.new
|
43
|
-
else raise Exception, "Unknown
|
47
|
+
else raise Exception, "Unknown provider: #{provider}"
|
44
48
|
end
|
45
49
|
object.hexdigest(self.to_s)
|
46
50
|
end
|
data/lib/small/time.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'date'
|
2
|
+
require 'datetime' unless defined?(DateTime)
|
3
|
+
|
4
|
+
# Some codes extracted from Rails
|
5
|
+
class Time
|
6
|
+
|
7
|
+
DATE_FORMATS = {
|
8
|
+
:default => "%Y-%m-%d %H:%M:%S"
|
9
|
+
}
|
10
|
+
|
11
|
+
def to_date
|
12
|
+
::Date.new(year, month, day)
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_datetime
|
16
|
+
::DateTime.civil(year, month, day, hour, min, sec, Rational(utc_offset, 86400))
|
17
|
+
end
|
18
|
+
|
19
|
+
def to_formatted_s(format = :default)
|
20
|
+
if formatter = DATE_FORMATS[format]
|
21
|
+
strftime(formatter)
|
22
|
+
else
|
23
|
+
self.to_s
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def today?
|
28
|
+
to_date == ::Time.now.to_date
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: small
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.2'
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-04-
|
12
|
+
date: 2012-04-23 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description:
|
15
15
|
email: bryann83@gmail.com
|
@@ -21,9 +21,11 @@ files:
|
|
21
21
|
- LICENSE
|
22
22
|
- lib/small/array.rb
|
23
23
|
- lib/small/hash.rb
|
24
|
+
- lib/small/integer.rb
|
24
25
|
- lib/small/math.rb
|
25
26
|
- lib/small/object.rb
|
26
27
|
- lib/small/string.rb
|
28
|
+
- lib/small/time.rb
|
27
29
|
- lib/small.rb
|
28
30
|
homepage: https://github.com/bry4n/small
|
29
31
|
licenses: []
|
@@ -45,7 +47,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
45
47
|
version: '0'
|
46
48
|
requirements: []
|
47
49
|
rubyforge_project:
|
48
|
-
rubygems_version: 1.8.
|
50
|
+
rubygems_version: 1.8.11
|
49
51
|
signing_key:
|
50
52
|
specification_version: 3
|
51
53
|
summary: A small collection of useful Ruby utilities
|