utiles 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +2 -0
- data/README.md +11 -0
- data/Rakefile +2 -0
- data/lib/utiles/version.rb +6 -0
- data/lib/utiles.rb +5 -0
- data/lib/utiles_datetime.rb +25 -0
- data/lib/utiles_integer.rb +46 -0
- data/lib/utiles_nil.rb +18 -0
- data/lib/utiles_string.rb +38 -0
- data/test/utiles_spec.rb +143 -0
- data/utiles.gemspec +22 -0
- metadata +90 -0
data/Gemfile
ADDED
data/README.md
ADDED
data/Rakefile
ADDED
data/lib/utiles.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# Extentions for Time class.
|
2
|
+
class Time
|
3
|
+
|
4
|
+
# Formats Time object to string.
|
5
|
+
#
|
6
|
+
# @param [Symbol] format the format type, `:minutes` or `:date` or no parameters
|
7
|
+
# @example Usage
|
8
|
+
# obj.to_str == obj.strftime("%Y-%m-%d / %H:%M:%S")
|
9
|
+
# obj.to_str(:minutes) == obj.strftime("%Y-%m-%d / %H:%M")
|
10
|
+
# obj.to_str(:date) == obj.strftime("%Y-%m-%d")
|
11
|
+
def to_str(*splat)
|
12
|
+
if splat.empty?
|
13
|
+
localtime.strftime("%Y-%m-%d / %H:%M:%S")
|
14
|
+
else
|
15
|
+
case splat[0]
|
16
|
+
when :minutes
|
17
|
+
localtime.strftime("%Y-%m-%d / %H:%M")
|
18
|
+
when :date
|
19
|
+
localtime.strftime("%Y-%m-%d")
|
20
|
+
else
|
21
|
+
localtime.strftime("%Y-%m-%d / %H:%M:%S")
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# Extentions for Integer class.
|
2
|
+
class Integer
|
3
|
+
|
4
|
+
# Checks if it is nil or empty.
|
5
|
+
#
|
6
|
+
# @return always false
|
7
|
+
def noe?
|
8
|
+
false
|
9
|
+
end
|
10
|
+
|
11
|
+
# Checks if it is blank.
|
12
|
+
#
|
13
|
+
# @return always false
|
14
|
+
def is_blank?
|
15
|
+
false
|
16
|
+
end
|
17
|
+
|
18
|
+
# Formats integer to data volume.
|
19
|
+
#
|
20
|
+
# @return [String]
|
21
|
+
# @example
|
22
|
+
# 18539311.to_date_vol => "185 MB"
|
23
|
+
# 18532.to_date_vol => "18,5 kB"
|
24
|
+
def to_data_vol
|
25
|
+
if self > 1000000000
|
26
|
+
gb = self / 1000000000
|
27
|
+
rest = self % 1000000000
|
28
|
+
s = gb < 10 ? "#{gb},#{rest.to_s[0,2]}" : gb < 100 ? "#{gb},#{(rest.to_s)[0,1]}" : "#{gb}"
|
29
|
+
"#{s} GB"
|
30
|
+
elsif self > 1000000
|
31
|
+
mb = self / 1000000
|
32
|
+
rest = self % 1000000
|
33
|
+
s = mb < 10 ? "#{mb},#{rest.to_s[0,2]}" : mb < 100 ? "#{mb},#{(rest.to_s)[0,1]}" : "#{mb}"
|
34
|
+
"#{s} MB"
|
35
|
+
elsif self > 1000
|
36
|
+
kb = self / 1000
|
37
|
+
rest = self % 1000
|
38
|
+
s = kb < 10 ? "#{kb},#{rest.to_s[0,2]}" : kb < 100 ? "#{kb},#{(rest.to_s)[0,1]}" : "#{kb}"
|
39
|
+
"#{s} kB"
|
40
|
+
elsif self > 0
|
41
|
+
"#{self} B"
|
42
|
+
else
|
43
|
+
"0"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/lib/utiles_nil.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# Extentions for String class.
|
2
|
+
class String
|
3
|
+
|
4
|
+
# Checks if it is nil or empty.
|
5
|
+
#
|
6
|
+
def noe?
|
7
|
+
empty?
|
8
|
+
end
|
9
|
+
|
10
|
+
# Checks if it is blank.
|
11
|
+
#
|
12
|
+
def is_blank?
|
13
|
+
noe? or self =~ /^\s+$/
|
14
|
+
end
|
15
|
+
|
16
|
+
# Formats string to currency.
|
17
|
+
#
|
18
|
+
# @return [String]
|
19
|
+
# @example
|
20
|
+
# "0202".to_currency => "2,02 zł"
|
21
|
+
def to_currency
|
22
|
+
zl = to_i/100
|
23
|
+
gr = to_i%100
|
24
|
+
gr = "0#{gr}" if gr < 10
|
25
|
+
"#{zl},#{gr} zł"
|
26
|
+
end
|
27
|
+
|
28
|
+
# Formats string to data volume.
|
29
|
+
#
|
30
|
+
# @return [String]
|
31
|
+
# @example
|
32
|
+
# "18539311".to_date_vol => "185 MB"
|
33
|
+
# "18532".to_date_vol => "18,5 kB"
|
34
|
+
def to_data_vol
|
35
|
+
to_i.to_data_vol
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
data/test/utiles_spec.rb
ADDED
@@ -0,0 +1,143 @@
|
|
1
|
+
require 'utiles'
|
2
|
+
|
3
|
+
describe NilClass do
|
4
|
+
|
5
|
+
it "noe?" do
|
6
|
+
a = nil
|
7
|
+
a.noe?.should be_true
|
8
|
+
end
|
9
|
+
|
10
|
+
it "is_blank?" do
|
11
|
+
a = nil
|
12
|
+
a.is_blank?.should be_true
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
describe String do
|
18
|
+
|
19
|
+
it "noe? empty" do
|
20
|
+
s = ""
|
21
|
+
s.noe?.should be_true
|
22
|
+
end
|
23
|
+
|
24
|
+
it "noe? not empty" do
|
25
|
+
s = " "
|
26
|
+
s.noe?.should be_false
|
27
|
+
end
|
28
|
+
|
29
|
+
it "is_blank? blank" do
|
30
|
+
s1 = ""
|
31
|
+
s2 = " "
|
32
|
+
s3 = "\n\t"
|
33
|
+
s1.is_blank?.should be_true
|
34
|
+
s2.is_blank?.should be_true
|
35
|
+
s3.is_blank?.should be_true
|
36
|
+
end
|
37
|
+
|
38
|
+
it "is_blank? not blank" do
|
39
|
+
s1 = "sad"
|
40
|
+
s2 = " asdad"
|
41
|
+
s3 = " asdsada asda"
|
42
|
+
s1.is_blank?.should be_false
|
43
|
+
s2.is_blank?.should be_false
|
44
|
+
s2.is_blank?.should be_false
|
45
|
+
end
|
46
|
+
|
47
|
+
it "grosze" do
|
48
|
+
"23".to_currency.should eq("0,23 zł")
|
49
|
+
end
|
50
|
+
|
51
|
+
it "zlotowki" do
|
52
|
+
"123".to_currency.should eq("1,23 zł")
|
53
|
+
end
|
54
|
+
|
55
|
+
it "zlotowki i male grosze" do
|
56
|
+
"1103".to_currency.should eq("11,03 zł")
|
57
|
+
end
|
58
|
+
|
59
|
+
it "zlotowki i duze grosze" do
|
60
|
+
"1123".to_currency.should eq("11,23 zł")
|
61
|
+
end
|
62
|
+
|
63
|
+
it "male zlotowki i male grosze" do
|
64
|
+
"0202".to_currency.should eq("2,02 zł")
|
65
|
+
end
|
66
|
+
|
67
|
+
it "Data in 185 GB" do
|
68
|
+
"185393114721".to_data_vol.should eq("185 GB")
|
69
|
+
end
|
70
|
+
|
71
|
+
it "Data in 18 GB" do
|
72
|
+
"18539311472".to_data_vol.should eq("18,5 GB")
|
73
|
+
end
|
74
|
+
|
75
|
+
it "Data in 185 MB" do
|
76
|
+
"185393114".to_data_vol.should eq("185 MB")
|
77
|
+
end
|
78
|
+
|
79
|
+
it "Data in 18 MB" do
|
80
|
+
"18539311".to_data_vol.should eq("18,5 MB")
|
81
|
+
end
|
82
|
+
|
83
|
+
it "Data in 1 MB" do
|
84
|
+
"1853931".to_data_vol.should eq("1,85 MB")
|
85
|
+
end
|
86
|
+
|
87
|
+
it "Data in 185 kB" do
|
88
|
+
"185322".to_data_vol.should eq("185 kB")
|
89
|
+
end
|
90
|
+
|
91
|
+
it "Data in 18 kB" do
|
92
|
+
"18532".to_data_vol.should eq("18,5 kB")
|
93
|
+
end
|
94
|
+
|
95
|
+
it "Data in 185 B" do
|
96
|
+
"185".to_data_vol.should eq("185 B")
|
97
|
+
end
|
98
|
+
|
99
|
+
it "Data in 0" do
|
100
|
+
"0".to_data_vol.should eq("0")
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
104
|
+
|
105
|
+
describe Time do
|
106
|
+
|
107
|
+
before do
|
108
|
+
secs = 1341225602
|
109
|
+
@time = Time.at(secs)
|
110
|
+
end
|
111
|
+
|
112
|
+
it "to_s" do
|
113
|
+
@time.to_s.should eq("Mon Jul 02 12:40:02 +0200 2012")
|
114
|
+
end
|
115
|
+
|
116
|
+
it "to_str" do
|
117
|
+
@time.to_str.should eq("2012-07-02 / 12:40:02")
|
118
|
+
end
|
119
|
+
|
120
|
+
it "to_str(:minutes)" do
|
121
|
+
@time.to_str(:minutes).should eq("2012-07-02 / 12:40")
|
122
|
+
end
|
123
|
+
|
124
|
+
it "to_str(:date)" do
|
125
|
+
@time.to_str(:date).should eq("2012-07-02")
|
126
|
+
end
|
127
|
+
|
128
|
+
end
|
129
|
+
|
130
|
+
describe Integer do
|
131
|
+
|
132
|
+
it "noe?" do
|
133
|
+
a = 1
|
134
|
+
a.noe?.should be_false
|
135
|
+
end
|
136
|
+
|
137
|
+
it "is_blank?" do
|
138
|
+
a = 2
|
139
|
+
a.is_blank?.should be_false
|
140
|
+
end
|
141
|
+
|
142
|
+
end
|
143
|
+
|
data/utiles.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "utiles/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "utiles"
|
7
|
+
s.version = Utiles::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Wojciech Todryk"]
|
10
|
+
s.email = ["wojciech@todryk.pl"]
|
11
|
+
s.homepage = "http://todryk.pl/utiles"
|
12
|
+
s.summary = %q{Utiles}
|
13
|
+
s.description = %q{Extends standard classes}
|
14
|
+
|
15
|
+
s.rubyforge_project = "utiles"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
s.add_development_dependency "rspec", [">= 0"]
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: utiles
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Wojciech Todryk
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-07-05 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
34
|
+
description: Extends standard classes
|
35
|
+
email:
|
36
|
+
- wojciech@todryk.pl
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files: []
|
42
|
+
|
43
|
+
files:
|
44
|
+
- Gemfile
|
45
|
+
- README.md
|
46
|
+
- Rakefile
|
47
|
+
- lib/utiles.rb
|
48
|
+
- lib/utiles/version.rb
|
49
|
+
- lib/utiles_datetime.rb
|
50
|
+
- lib/utiles_integer.rb
|
51
|
+
- lib/utiles_nil.rb
|
52
|
+
- lib/utiles_string.rb
|
53
|
+
- test/utiles_spec.rb
|
54
|
+
- utiles.gemspec
|
55
|
+
homepage: http://todryk.pl/utiles
|
56
|
+
licenses: []
|
57
|
+
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options: []
|
60
|
+
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
hash: 3
|
69
|
+
segments:
|
70
|
+
- 0
|
71
|
+
version: "0"
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
hash: 3
|
78
|
+
segments:
|
79
|
+
- 0
|
80
|
+
version: "0"
|
81
|
+
requirements: []
|
82
|
+
|
83
|
+
rubyforge_project: utiles
|
84
|
+
rubygems_version: 1.8.24
|
85
|
+
signing_key:
|
86
|
+
specification_version: 3
|
87
|
+
summary: Utiles
|
88
|
+
test_files:
|
89
|
+
- test/utiles_spec.rb
|
90
|
+
has_rdoc:
|