nobiru 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/nobiru.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'nobiru/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "nobiru"
8
+ spec.version = Nobiru::VERSION
9
+ spec.authors = ["Juan Gomez"]
10
+ spec.email = ["j.gomez@drexed.com"]
11
+ spec.summary = %q{Commonly used ruby object helpers.}
12
+ spec.description = %q{Class extensions of commonly used ruby object helpers.}
13
+ spec.homepage = "https://github.com/drexed/nobiru"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler"
22
+ spec.add_development_dependency "coveralls"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec"
25
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe Array do
4
+
5
+ describe "#remove_blanks" do
6
+ it "to be this is a test" do
7
+ expect("this is a test".split(" ").remove_blanks).to eq(["this", "is", "a", "test"])
8
+ end
9
+
10
+ it "to be this is a test" do
11
+ expect(["this", "", "that", nil].remove_blanks).to eq(["this", "that"])
12
+ end
13
+ end
14
+
15
+ describe "#remove_first" do
16
+ it "to be this is a test" do
17
+ expect(["1", "2", "3"].remove_first).to eq(["2", "3"])
18
+ end
19
+ end
20
+
21
+ describe "#remove_last" do
22
+ it "to be this is a test" do
23
+ expect(["1", "2", "3"].remove_last).to eq(["1", "2"])
24
+ end
25
+ end
26
+
27
+ end
@@ -0,0 +1,219 @@
1
+ require 'spec_helper'
2
+
3
+ describe Enumerable do
4
+
5
+ describe "#drop_last" do
6
+ it "to be [2, 3]" do
7
+ expect([1, 2, 3].drop_last(1)).to eq([1, 2])
8
+ end
9
+
10
+ it "to be []" do
11
+ expect([].drop_last(3)).to eq([])
12
+ end
13
+ end
14
+
15
+ describe "#drop_last_while" do
16
+ it "to be [1, 2]" do
17
+ expect([1, 2, 3].drop_last_while(&:odd?)).to eq([1, 2])
18
+ end
19
+
20
+ it "to be []" do
21
+ expect([].take_last_while(&:odd?)).to eq([])
22
+ end
23
+ end
24
+
25
+ describe "#exactly?" do
26
+ it "to be true" do
27
+ expect([1, false, nil].exactly?(1)).to eq(true)
28
+ expect([false, nil].exactly?(0)).to eq(true)
29
+ expect([1, 2, 3].exactly?(3)).to eq(true)
30
+ expect([1, 2, 3, 4].exactly?(1) { |n| n > 3 }).to eq(true)
31
+ expect([1, 2, 3, 4].exactly?(2, &:even?)).to eq(true)
32
+ end
33
+
34
+ it "to be false" do
35
+ expect([].exactly?(1)).to eq(false)
36
+ expect([1, false, nil].exactly?(3)).to eq(false)
37
+ expect([1, 1, 3, 3].exactly?(2, &:even?)).to eq(false)
38
+ end
39
+ end
40
+
41
+ describe "#frequencies" do
42
+ it "to be {}" do
43
+ expect([].frequencies).to eq({})
44
+ end
45
+
46
+ it "to be { 1 => 2, :symbol => 2, 'string' => 1, 3 => 1 }" do
47
+ expect([1, :symbol, 'string', 3, :symbol, 1].frequencies).to eq({ 1 => 2, :symbol => 2, 'string' => 1, 3 => 1 })
48
+ end
49
+ end
50
+
51
+ describe "#max" do
52
+ it "to be 0" do
53
+ expect([].max).to eq(0)
54
+ end
55
+
56
+ it "to be nil" do
57
+ expect([].max(nil)).to eq(nil)
58
+ end
59
+
60
+ it "to be 3" do
61
+ expect([2, 3, 1].max).to eq(3)
62
+ end
63
+ end
64
+
65
+ describe "#min" do
66
+ it "to be 0" do
67
+ expect([].min).to eq(0)
68
+ end
69
+
70
+ it "to be nil" do
71
+ expect([].min(nil)).to eq(nil)
72
+ end
73
+
74
+ it "to be 3" do
75
+ expect([2, 3, 1].min).to eq(1)
76
+ end
77
+ end
78
+
79
+ describe "#mean" do
80
+ it "to be 0" do
81
+ expect([].mean).to eq(0)
82
+ end
83
+
84
+ it "to be nil" do
85
+ expect([].mean(nil)).to eq(nil)
86
+ end
87
+
88
+ it "to be 2" do
89
+ expect([1, 2, 3].mean).to eq(2)
90
+ end
91
+
92
+ it "to be 2.5" do
93
+ expect([1, 2, 3, 4].mean).to eq(2.5)
94
+ end
95
+ end
96
+
97
+ describe '#median' do
98
+ it 'to be 0' do
99
+ expect([].median).to eq(0)
100
+ end
101
+
102
+ it 'to be nil' do
103
+ expect([].median(nil)).to eq(nil)
104
+ end
105
+
106
+ it 'to be 2' do
107
+ expect([1,2,6].median).to eq(2)
108
+ end
109
+
110
+ it 'to be 2.5' do
111
+ expect([1,2,3,6].median).to eq(2.5)
112
+ end
113
+ end
114
+
115
+ describe '#mode' do
116
+ it 'to be 0' do
117
+ expect([].mode).to eq(0)
118
+ end
119
+
120
+ it 'to be nil' do
121
+ expect([].mode(nil)).to eq(nil)
122
+ expect([1,2,3].mode).to eq(nil)
123
+ end
124
+
125
+ it 'to be 1' do
126
+ expect([1,1,2,46].mode).to eq(1)
127
+ end
128
+
129
+ it 'to be 3.5' do
130
+ expect([3.5].mode).to eq(3.5)
131
+ end
132
+ end
133
+
134
+ describe '#range' do
135
+ it 'to be 0' do
136
+ expect([].range).to eq(0)
137
+ end
138
+
139
+ it 'to be nil' do
140
+ expect([].range(nil)).to eq(nil)
141
+ end
142
+
143
+ it 'to be 5' do
144
+ expect([1,2,6].range).to eq(5)
145
+ end
146
+ end
147
+
148
+ describe "#several?" do
149
+ it "to be true" do
150
+ expect([1, 2, 3].several?).to eq(true)
151
+ expect([1, 2, 3, 4].several?(&:even?)).to eq(true)
152
+ end
153
+
154
+ it "to be false" do
155
+ expect([].several?).to eq(false)
156
+ expect([1, false, nil].several?).to eq(false)
157
+ expect([1, 1, 3, 3].several?(&:even?)).to eq(false)
158
+ end
159
+ end
160
+
161
+ describe '#standard_deviation' do
162
+ it 'to be the square root of the #variance' do
163
+ expect([1,2,6].standard_deviation).to eq(2.6457513110645907)
164
+ end
165
+
166
+ it 'to be nil for single element arrays' do
167
+ expect([1].standard_deviation).to eq(0)
168
+ end
169
+
170
+ it 'to be nil if empty' do
171
+ expect([].standard_deviation).to eq(0)
172
+ end
173
+ end
174
+
175
+ describe "#sum" do
176
+ it "to be 0" do
177
+ expect([].sum).to eq(0)
178
+ end
179
+
180
+ it "to be nil" do
181
+ expect([].sum(nil)).to eq(nil)
182
+ end
183
+
184
+ it "to be 6" do
185
+ expect([1, 2, 3].sum).to eq(6)
186
+ end
187
+ end
188
+
189
+ describe "#take_last" do
190
+ it "to be [2, 3]" do
191
+ expect([1, 2, 3].take_last(2)).to eq([2, 3])
192
+ end
193
+
194
+ it "to be []" do
195
+ expect([].take_last(3)).to eq([])
196
+ end
197
+ end
198
+
199
+ describe "#take_last_while" do
200
+ it "to be [3, 5]" do
201
+ expect([1, 2, 3, 5].take_last_while(&:odd?)).to eq([3, 5])
202
+ end
203
+
204
+ it "to be []" do
205
+ expect([].take_last_while(&:odd?)).to eq([])
206
+ end
207
+ end
208
+
209
+ describe '#variance' do
210
+ it 'to be the mean squared deviation of the sample (n - 1 denominator)' do
211
+ expect([1,2,6].variance).to eq(7)
212
+ end
213
+
214
+ it 'to be nil if empty' do
215
+ expect([].variance).to eq(0)
216
+ end
217
+ end
218
+
219
+ end
@@ -0,0 +1,80 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hash do
4
+
5
+ describe "#except" do
6
+ it "to be {}" do
7
+ expect({}.except(:foo)).to eq({})
8
+ end
9
+
10
+ it "to be { :foo => 1 }" do
11
+ expect({ :foo => 1, :baz => 2, :bar => 3 }.except(:baz, :bar)).to eq({ :foo => 1 })
12
+ expect({ foo: 1, baz: 2, bar: 3 }.except(:baz, :bar)).to eq({ foo: 1 })
13
+ end
14
+
15
+ it "to be { :baz => 2, :bar => 3 }" do
16
+ expect({ :foo => 1, :baz => 2, :bar => 3 }.except(:foo)).to eq({ :baz => 2, :bar => 3 })
17
+ expect({ foo: 1, baz: 2, bar: 3 }.except(:foo)).to eq({ baz: 2, bar: 3 })
18
+ end
19
+ end
20
+
21
+ describe "#only" do
22
+ it "to be {}" do
23
+ expect({}.only(:foo)).to eq({})
24
+ end
25
+
26
+ it "to be { :foo => 1 }" do
27
+ expect({ :foo => 1, :baz => 2, :bar => 3 }.only(:foo)).to eq({ :foo => 1 })
28
+ expect({ foo: 1, baz: 2, bar: 3 }.only(:foo)).to eq({ foo: 1 })
29
+ end
30
+
31
+ it "to be { :baz => 2, :bar => 3 }" do
32
+ expect({ :foo => 1, :baz => 2, :bar => 3 }.only(:baz, :bar)).to eq({ :baz => 2, :bar => 3 })
33
+ expect({ foo: 1, baz: 2, bar: 3 }.only(:baz, :bar)).to eq({ baz: 2, bar: 3 })
34
+ end
35
+ end
36
+
37
+ describe "#rename_keys(!)" do
38
+ it "to be [:baz, :bar]" do
39
+ expect({ foo: 'foo', baz: 'baz' }.rename_keys(foo: :bar).keys).to eq([:baz, :bar])
40
+ expect({ foo: 'foo', baz: 'baz' }.rename_keys!(foo: :bar).keys).to eq([:baz, :bar])
41
+ end
42
+
43
+ it "to be [:foo, 'tick']" do
44
+ expect({ foo: 'foo', 'baz' => 'baz' }.rename_keys('baz' => 'tick').keys).to eq([:foo, 'tick'])
45
+ expect({ foo: 'foo', 'baz' => 'baz' }.rename_keys!('baz' => 'tick').keys).to eq([:foo, 'tick'])
46
+ end
47
+
48
+ it "to be [:bar, :tick]" do
49
+ expect({ foo: 'foo', baz: 'baz' }.rename_keys(foo: :bar, baz: :tick).keys).to eq([:bar, :tick])
50
+ expect({ foo: 'foo', baz: 'baz' }.rename_keys!(foo: :bar, baz: :tick).keys).to eq([:bar, :tick])
51
+ end
52
+
53
+ it "to be [:bar, 'tick']" do
54
+ expect({ foo: 'foo', 'baz' => 'baz' }.rename_keys(foo: :bar, 'baz' => 'tick').keys).to eq([:bar, 'tick'])
55
+ expect({ foo: 'foo', 'baz' => 'baz' }.rename_keys!(foo: :bar, 'baz' => 'tick').keys).to eq([:bar, 'tick'])
56
+ end
57
+ end
58
+
59
+ describe "#stringify_keys(!)" do
60
+ it "to be ['foo', 'bar']" do
61
+ expect({ foo: 'foo', 'bar' => 'bar' }.stringify_keys.keys).to eq(['foo', 'bar'])
62
+ expect({ foo: 'foo', 'bar' => 'bar' }.stringify_keys!.keys).to eq(['foo', 'bar'])
63
+ end
64
+ end
65
+
66
+ describe "#symbolize_keys(!)" do
67
+ it "to be [:foo, :bar]" do
68
+ expect({ foo: 'foo', 'bar' => 'bar' }.symbolize_keys.keys).to eq([:foo, :bar])
69
+ expect({ foo: 'foo', 'bar' => 'bar' }.symbolize_keys!.keys).to eq([:foo, :bar])
70
+ end
71
+ end
72
+
73
+ describe "#symbolize_and_underscore_keys(!)" do
74
+ it "to be [:foo_bar, :baz_bar]" do
75
+ expect({ 'foo_Bar' => 'example', bazBar: 'string' }.symbolize_and_underscore_keys.keys).to eq([:foo_bar, :baz_bar])
76
+ expect({ 'foo_Bar' => 'example', bazBar: 'string' }.symbolize_and_underscore_keys!.keys).to eq([:foo_bar, :baz_bar])
77
+ end
78
+ end
79
+
80
+ end
@@ -0,0 +1,171 @@
1
+ require 'spec_helper'
2
+
3
+ describe Numeric do
4
+
5
+ describe "#multiple_of?" do
6
+ it "to be true" do
7
+ expect(9.multiple_of?(3)).to eq(true)
8
+ end
9
+
10
+ it "to be false" do
11
+ expect(10.multiple_of?(3)).to eq(false)
12
+ end
13
+ end
14
+
15
+ describe "#negative?" do
16
+ it "to be true" do
17
+ expect(-1.negative?).to eq(true)
18
+ end
19
+
20
+ it "to be false" do
21
+ expect(1.negative?).to eq(false)
22
+ end
23
+ end
24
+
25
+ describe "#positive?" do
26
+ it "to be true" do
27
+ expect(1.positive?).to eq(true)
28
+ end
29
+
30
+ it "to be false" do
31
+ expect(-1.positive?).to eq(false)
32
+ end
33
+ end
34
+
35
+ describe "#to_byte" do
36
+ it "to be 1" do
37
+ expect(1024.to_byte).to eq(1)
38
+ expect(1024.to_byte(:kb, :mb)).to eq(1)
39
+ end
40
+
41
+ it "to be 5" do
42
+ expect(5120.to_byte).to eq(5)
43
+ expect(5120.to_byte(:kb, :mb)).to eq(5)
44
+ end
45
+
46
+ it "to be 1024" do
47
+ expect(1024.to_byte(:kb, :kb)).to eq(1024)
48
+ expect(1.to_byte(:mb, :kb)).to eq(1024)
49
+ end
50
+
51
+ it "to be 1048576" do
52
+ expect(1.to_byte(:gb, :kb)).to eq(1048576)
53
+ end
54
+
55
+ it "to be 0.078125" do
56
+ expect(80.to_byte(:mb, :gb)).to eq(0.078125)
57
+ end
58
+ end
59
+
60
+ describe "#to_length" do
61
+ it "to be 0.039370078740157" do
62
+ expect(1.to_length).to eq(0.039370078740157)
63
+ end
64
+
65
+ it "to be 1" do
66
+ expect(1.to_length(:mm, :mm)).to eq(1)
67
+ expect(1.to_length(:in, :in)).to eq(1)
68
+ expect(10.to_length(:mm, :cm)).to eq(1)
69
+ expect(12.to_length(:in, :ft)).to eq(1)
70
+ end
71
+
72
+ it "to be 10" do
73
+ expect(1.to_length(:cm, :mm)).to eq(10)
74
+ end
75
+
76
+ it "to be 12" do
77
+ expect(1.to_length(:ft, :in)).to eq(12)
78
+ end
79
+
80
+ it "to be 30.48" do
81
+ expect(1.to_length(:ft, :cm)).to eq(30.48)
82
+ end
83
+
84
+ it "to be 1093.6132983377001" do
85
+ expect(1.to_length(:km, :yd)).to eq(1093.6132983377001)
86
+ end
87
+
88
+ it "to be 6076.116666666666" do
89
+ expect(1.to_length(:nm, :ft)).to eq(6076.116666666666)
90
+ end
91
+ end
92
+
93
+ describe "#to_time" do
94
+ it "to be 2" do
95
+ expect(120.to_time_unit).to eq(2)
96
+ expect(120.to_time_unit(:sec, :min)).to eq(2)
97
+ end
98
+
99
+ it "to be 40" do
100
+ expect(2400.to_time_unit(:min, :hr)).to eq(40)
101
+ end
102
+
103
+ it "to be 3" do
104
+ expect(72.to_time_unit(:hr, :day)).to eq(3)
105
+ end
106
+
107
+ it "to be 5" do
108
+ expect(1825.to_time_unit(:day, :yr)).to eq(5)
109
+ end
110
+
111
+ it "to be 172800" do
112
+ expect(2.to_time_unit(:day, :sec)).to eq(172800)
113
+ end
114
+ end
115
+
116
+ describe "#to_temperature" do
117
+ it "to be 212" do
118
+ expect(100.to_temperature).to eq(212)
119
+ expect(100.to_temperature(:c, :f)).to eq(212)
120
+ expect(100.to_temperature(:celcius, :fahrenheit)).to eq(212)
121
+ end
122
+
123
+ it "to be 100" do
124
+ expect(100.to_temperature(:c, :c)).to eq(100)
125
+ expect(212.to_temperature(:f, :c)).to eq(100)
126
+ end
127
+
128
+ it "to be 373.15" do
129
+ expect(100.to_temperature(:c, :k)).to eq(373.15)
130
+ expect(212.to_temperature(:f, :k)).to eq(373.15)
131
+ end
132
+ end
133
+
134
+ describe "#to_weight" do
135
+ it "to be 0.035273961949580004" do
136
+ expect(1.to_weight).to eq(0.035273961949580004)
137
+ end
138
+
139
+ it "to be 1" do
140
+ expect(1.to_weight(:mg, :mg)).to eq(1)
141
+ expect(1.to_weight(:oz, :oz)).to eq(1)
142
+ expect(10.to_weight(:mg, :cg)).to eq(1)
143
+ expect(16.to_weight(:oz, :lb)).to eq(1)
144
+ end
145
+
146
+ it "to be 1.3607771100000001" do
147
+ expect(3.to_weight(:lb, :kg)).to eq(1.3607771100000001)
148
+ end
149
+
150
+ it "to be 1.1023113109243998" do
151
+ expect(1.to_weight(:mt, :tn)).to eq(1.1023113109243998)
152
+ end
153
+
154
+ it "to be 2.2046226218488" do
155
+ expect(1.to_weight(:kg, :lb)).to eq(2.2046226218488)
156
+ end
157
+
158
+ it "to be 10" do
159
+ expect(1.to_weight(:cg, :mg)).to eq(10)
160
+ end
161
+
162
+ it "to be 16" do
163
+ expect(1.to_weight(:lb, :oz)).to eq(16)
164
+ end
165
+
166
+ it "to be 64000" do
167
+ expect(2.to_weight(:tn, :lb)).to eq(4000)
168
+ end
169
+ end
170
+
171
+ end
@@ -0,0 +1,79 @@
1
+ require 'spec_helper'
2
+
3
+ describe Object do
4
+
5
+ describe "#blank?" do
6
+ it "to be true" do
7
+ expect("".blank?).to eq(true)
8
+ expect([].blank?).to eq(true)
9
+ expect({}.blank?).to eq(true)
10
+ expect(false.blank?).to eq(true)
11
+ end
12
+
13
+ it "to be false" do
14
+ expect("x".blank?).to eq(false)
15
+ expect("foo bar".blank?).to eq(false)
16
+ expect("19".blank?).to eq(false)
17
+ expect(true.blank?).to eq(false)
18
+ end
19
+ end
20
+
21
+ describe "#present?" do
22
+ it "to be true" do
23
+ expect("x".present?).to eq(true)
24
+ expect("foo bar".present?).to eq(true)
25
+ expect("19".present?).to eq(true)
26
+ expect(true.present?).to eq(true)
27
+ end
28
+
29
+ it "to be false" do
30
+ expect("".present?).to eq(false)
31
+ expect([].present?).to eq(false)
32
+ expect({}.present?).to eq(false)
33
+ expect(false.present?).to eq(false)
34
+ end
35
+ end
36
+
37
+ describe "#try" do
38
+ it "to be upcase" do
39
+ expect("example".try(:upcase)).to eq("EXAMPLE")
40
+ end
41
+
42
+ it "to be nil" do
43
+ expect("example".try(:fake_method)).to eq(nil)
44
+ end
45
+ end
46
+
47
+ describe "#numeric?" do
48
+ it "to be true" do
49
+ expect(5.numeric?).to eq(true)
50
+ expect(0.numeric?).to eq(true)
51
+ expect(-37.3.numeric?).to eq(true)
52
+ expect(51.45.numeric?).to eq(true)
53
+ expect("+256.375".numeric?).to eq(true)
54
+ expect("-37.3".numeric?).to eq(true)
55
+ end
56
+
57
+ it "to be false" do
58
+ expect("".numeric?).to eq(false)
59
+ expect(" ".numeric?).to eq(false)
60
+ expect("2.3.3".numeric?).to eq(false)
61
+ expect("$9.86".numeric?).to eq(false)
62
+ expect("x".numeric?).to eq(false)
63
+ expect("foo".numeric?).to eq(false)
64
+ end
65
+ end
66
+
67
+ describe "#palindrome?" do
68
+ it "to be true" do
69
+ expect("racecar".palindrome?).to eq(true)
70
+ expect(12321.palindrome?).to eq(true)
71
+ end
72
+
73
+ it "to be false" do
74
+ expect("example".palindrome?).to eq(false)
75
+ expect(12345.palindrome?).to eq(false)
76
+ end
77
+ end
78
+
79
+ end