lazier 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. data/.gitignore +6 -0
  2. data/.travis.yml +8 -0
  3. data/.yardopts +1 -0
  4. data/Gemfile +9 -0
  5. data/Gemfile.lock +82 -0
  6. data/README.md +33 -0
  7. data/Rakefile +18 -0
  8. data/doc/Lazier.html +557 -0
  9. data/doc/Lazier/Boolean.html +297 -0
  10. data/doc/Lazier/DateTime.html +787 -0
  11. data/doc/Lazier/DateTime/ClassMethods.html +1592 -0
  12. data/doc/Lazier/Exceptions.html +125 -0
  13. data/doc/Lazier/Exceptions/Dump.html +133 -0
  14. data/doc/Lazier/Hash.html +393 -0
  15. data/doc/Lazier/Math.html +130 -0
  16. data/doc/Lazier/Math/ClassMethods.html +362 -0
  17. data/doc/Lazier/Object.html +1565 -0
  18. data/doc/Lazier/Pathname.html +225 -0
  19. data/doc/Lazier/Settings.html +1249 -0
  20. data/doc/Lazier/String.html +471 -0
  21. data/doc/Lazier/TimeZone.html +1675 -0
  22. data/doc/Lazier/TimeZone/ClassMethods.html +1055 -0
  23. data/doc/Lazier/Version.html +189 -0
  24. data/doc/_index.html +306 -0
  25. data/doc/class_list.html +53 -0
  26. data/doc/css/common.css +1 -0
  27. data/doc/css/full_list.css +57 -0
  28. data/doc/css/style.css +328 -0
  29. data/doc/file.README.html +107 -0
  30. data/doc/file_list.html +55 -0
  31. data/doc/frames.html +28 -0
  32. data/doc/index.html +107 -0
  33. data/doc/js/app.js +214 -0
  34. data/doc/js/full_list.js +173 -0
  35. data/doc/js/jquery.js +4 -0
  36. data/doc/method_list.html +652 -0
  37. data/doc/top-level-namespace.html +112 -0
  38. data/lazier.gemspec +36 -0
  39. data/lib/lazier.rb +127 -0
  40. data/lib/lazier/boolean.rb +26 -0
  41. data/lib/lazier/datetime.rb +548 -0
  42. data/lib/lazier/exceptions.rb +14 -0
  43. data/lib/lazier/hash.rb +40 -0
  44. data/lib/lazier/math.rb +47 -0
  45. data/lib/lazier/object.rb +163 -0
  46. data/lib/lazier/pathname.rb +26 -0
  47. data/lib/lazier/settings.rb +118 -0
  48. data/lib/lazier/string.rb +54 -0
  49. data/lib/lazier/version.rb +24 -0
  50. data/spec/coverage_helper.rb +19 -0
  51. data/spec/cowtech-extensions/boolean_spec.rb +30 -0
  52. data/spec/cowtech-extensions/datetime_spec.rb +352 -0
  53. data/spec/cowtech-extensions/hash_spec.rb +30 -0
  54. data/spec/cowtech-extensions/math_spec.rb +41 -0
  55. data/spec/cowtech-extensions/object_spec.rb +231 -0
  56. data/spec/cowtech-extensions/pathname_spec.rb +21 -0
  57. data/spec/cowtech-extensions/settings_spec.rb +118 -0
  58. data/spec/cowtech-extensions/string_spec.rb +45 -0
  59. data/spec/lazier_spec.rb +57 -0
  60. data/spec/spec_helper.rb +16 -0
  61. metadata +292 -0
@@ -0,0 +1,41 @@
1
+ # encoding: utf-8
2
+ #
3
+ # This file is part of the lazier gem. Copyright (C) 2011 and above Shogun <shogun_panda@me.com>.
4
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
+ #
6
+
7
+ require "spec_helper"
8
+
9
+ describe Lazier::Math do
10
+ let(:first) { 1 }
11
+ let(:second) { 2 }
12
+ let(:third) { 0 }
13
+
14
+ before(:all) do
15
+ ::Lazier.load!
16
+ end
17
+
18
+ describe "::min" do
19
+ it "should return the minimum argument" do
20
+ expect(::Math.min(first)).to eq(first)
21
+ expect(::Math.min(first, second)).to eq(first)
22
+ expect(::Math.min([first, [second, third]])).to eq(third)
23
+ end
24
+
25
+ it "should return nil for an empty array" do
26
+ expect(::Math.min()).to be_nil
27
+ end
28
+ end
29
+
30
+ describe "::max" do
31
+ it "should return the maximum argument" do
32
+ expect(::Math.max(first)).to eq(first)
33
+ expect(::Math.max(first, second)).to eq(second)
34
+ expect(::Math.max([first, [second, third]])).to eq(second)
35
+ end
36
+
37
+ it "should return nil for an empty array" do
38
+ expect(::Math.max()).to be_nil
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,231 @@
1
+ # encoding: utf-8
2
+ #
3
+ # This file is part of the lazier gem. Copyright (C) 2011 and above Shogun <shogun_panda@me.com>.
4
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
+ #
6
+
7
+ require "spec_helper"
8
+
9
+ describe Lazier::Object do
10
+ before(:all) do
11
+ ::Lazier.load!
12
+ end
13
+
14
+ describe "#normalize_number" do
15
+ it "should correctly sanitize numbers" do
16
+ expect(123.normalize_number).to eq("123")
17
+ expect("123.45".normalize_number).to eq("123.45")
18
+ expect("1,23.45".normalize_number).to eq("123.45")
19
+ expect("-1.23.45".normalize_number).to eq("-123.45")
20
+ expect("+123.45".normalize_number).to eq("+123.45")
21
+ expect("+1.231,45".normalize_number).to eq("+1231.45")
22
+ end
23
+ end
24
+
25
+ describe "#is_number?" do
26
+ it "should return true for a valid number" do
27
+ expect("123.45".is_number?).to be_true
28
+ expect("1,23.45".is_number?).to be_true
29
+ expect("-1.23.45".is_number?).to be_true
30
+ expect("+123.45".is_number?).to be_true
31
+ expect("+1.231,45".is_number?).to be_true
32
+ end
33
+
34
+ it "should return true for a invalid number" do
35
+ expect("s213".is_number?).to be_false
36
+ expect(nil.is_number?).to be_false
37
+ end
38
+ end
39
+
40
+ describe "#is_integer?" do
41
+ it "should return true for a valid number" do
42
+ expect("123".is_integer?).to be_true
43
+ expect("-123".is_integer?).to be_true
44
+ expect("+123".is_integer?).to be_true
45
+ end
46
+
47
+ it "should return true for a invalid number" do
48
+ expect("s123".is_integer?).to be_false
49
+ expect("123.12".is_integer?).to be_false
50
+ end
51
+ end
52
+
53
+ describe "#is_float?" do
54
+ it "should return true for a valid number" do
55
+ expect("123.45".is_float?).to be_true
56
+ expect("1,23.45".is_float?).to be_true
57
+ expect("-1.23.45".is_float?).to be_true
58
+ expect("+123.45".is_float?).to be_true
59
+ expect("+1.231,45".is_float?).to be_true
60
+ end
61
+
62
+ it "should return true for a invalid number" do
63
+ expect("s213".is_float?).to be_false
64
+ expect(nil.is_float?).to be_false
65
+ end
66
+ end
67
+
68
+ describe "#is_boolean?" do
69
+ it "should return true for a valid boolean" do
70
+ expect(true.is_boolean?).to be_true
71
+ expect(false.is_boolean?).to be_true
72
+ expect(nil.is_boolean?).to be_true
73
+ expect("y".is_boolean?).to be_true
74
+ expect("n".is_boolean?).to be_true
75
+ expect("yes".is_boolean?).to be_true
76
+ expect("no".is_boolean?).to be_true
77
+ expect("1".is_boolean?).to be_true
78
+ expect("0".is_boolean?).to be_true
79
+ expect("true".is_boolean?).to be_true
80
+ expect("false".is_boolean?).to be_true
81
+ expect("t".is_boolean?).to be_true
82
+ expect("f".is_boolean?).to be_true
83
+ expect(1.is_boolean?).to be_true
84
+ expect(0.is_boolean?).to be_true
85
+ end
86
+
87
+ it "should return true for a invalid boolean" do
88
+ expect("11".is_boolean?).to be_false
89
+ end
90
+ end
91
+
92
+ describe "#ensure_string" do
93
+ it "should correctly handle strings" do
94
+ expect(" abc ".ensure_string).to eq(" abc ")
95
+ 1.ensure_string == "1"
96
+ expect(1.0.ensure_string).to eq("1.0")
97
+ expect(:abc.ensure_string).to eq("abc")
98
+ expect(nil.ensure_string).to eq("")
99
+ end
100
+ end
101
+
102
+ describe "#to_float" do
103
+ it "should correctly convert number" do
104
+ expect(123.45.to_float).to eq(123.45)
105
+ expect(123.to_float).to eq(123.00)
106
+ expect("123.45".to_float).to eq(123.45)
107
+ expect("1,23.45".to_float).to eq(123.45)
108
+ expect("-1.23.45".to_float).to eq(-123.45)
109
+ expect("+123.45".to_float).to eq(123.45)
110
+ expect("+1.231,45".to_float).to eq(1231.45)
111
+ end
112
+
113
+ it "should return 0.0 for a invalid number without a default" do
114
+ expect("s213".to_float).to eq(0.0)
115
+ end
116
+
117
+ it "should return the default for a invalid number" do
118
+ expect("s213".to_float(1.0)).to eq(1.0)
119
+ end
120
+ end
121
+
122
+ describe "#to_integer" do
123
+ it "should correctly convert number" do
124
+ expect(123.45.to_integer).to eq(123)
125
+ expect(123.to_integer).to eq(123)
126
+ expect("+123".to_integer).to eq(123)
127
+ expect("-123".to_integer).to eq(-123)
128
+ end
129
+
130
+ it "should return 0 for a invalid number without a default" do
131
+ expect("s213".to_integer).to eq(0)
132
+ end
133
+
134
+ it "should return the default for a invalid number" do
135
+ expect("s213".to_integer(1)).to eq(1)
136
+ end
137
+ end
138
+
139
+ describe "#to_boolean" do
140
+ it "should return true for a valid true boolean" do
141
+ expect(true.to_boolean).to be_true
142
+ expect("y".to_boolean).to be_true
143
+ expect("yes".to_boolean).to be_true
144
+ expect("1".to_boolean).to be_true
145
+ expect(1.to_boolean).to be_true
146
+ expect(1.0.to_boolean).to be_true
147
+ end
148
+
149
+ it "should return false for all other values" do
150
+ expect(false.to_boolean).to be_false
151
+ expect(nil.to_boolean).to be_false
152
+ expect("n".to_boolean).to be_false
153
+ expect("no".to_boolean).to be_false
154
+ expect("2".to_boolean).to be_false
155
+ expect(2.0.to_boolean).to be_false
156
+ expect("false".to_boolean).to be_false
157
+ expect("abc".to_boolean).to be_false
158
+ expect(0.to_boolean).to be_false
159
+ end
160
+ end
161
+
162
+ describe "#round_to_precision" do
163
+ it "should round number" do
164
+ expect(123.456789.round_to_precision(2)).to eq("123.46")
165
+ expect(123.456789.round_to_precision(0)).to eq("123")
166
+ expect("123.456789".round_to_precision(2)).to eq("123.46")
167
+ end
168
+
169
+ it "should return nil for non numeric values" do
170
+ expect(123.456789.round_to_precision(-1)).to eq(nil)
171
+ expect("abc".round_to_precision(-1)).to eq(nil)
172
+ end
173
+ end
174
+
175
+ describe "#format_number" do
176
+ it "should format number" do
177
+ expect(123123.456789.format_number).to eq("123,123.46")
178
+ expect(123123.456789.format_number(2)).to eq("123,123.46")
179
+ expect(123123.456789.format_number(3, "@")).to eq("123,123@457")
180
+ expect(123123.456789.format_number(3, "@", "$")).to eq("123,123@457 $")
181
+ expect("123123.456789".format_number(3, "@", "$", "!")).to eq("123!123@457 $")
182
+
183
+ Lazier.settings.setup_format_number(5, ",", "£", ".")
184
+ expect(123123.456789.format_number).to eq("123.123,45679 £")
185
+ end
186
+
187
+ it "should return nil for non numeric values" do
188
+ expect(123.456789.format_number(-1)).to eq(nil)
189
+ expect("abc".format_number(-1)).to eq(nil)
190
+ end
191
+ end
192
+
193
+ describe "#format_boolean" do
194
+ it "should return the correct string for all values" do
195
+ expect("yes".format_boolean).to eq("Yes")
196
+ expect("abc".format_boolean).to eq("No")
197
+ end
198
+
199
+ it "should support localization" do
200
+ Lazier.settings.setup_boolean_names("YYY", "NNN")
201
+ expect("yes".format_boolean).to eq("YYY")
202
+ expect("abc".format_boolean).to eq("NNN")
203
+ end
204
+
205
+ it "should support overrides" do
206
+ Lazier.settings.setup_boolean_names
207
+ expect("yes".format_boolean("TTT")).to eq("TTT")
208
+ expect("yes".format_boolean(nil, "FFF")).to eq("Yes")
209
+ expect("abc".format_boolean("TTT")).to eq("No")
210
+ expect("abc".format_boolean(nil, "FFF")).to eq("FFF")
211
+ end
212
+ end
213
+
214
+ describe "#debug_dump" do
215
+ it "should return the correct representation for an object" do
216
+ reference = {:a => "b"}
217
+ expect(reference.debug_dump(:json, false)).to eq(reference.to_json)
218
+ expect(reference.debug_dump(:pretty_json, false)).to eq(::JSON.pretty_generate(reference))
219
+ expect(reference.debug_dump(:yaml, false)).to eq(reference.to_yaml)
220
+ end
221
+
222
+ it "should inspect the object if the format is not recognized" do
223
+ reference = {:a => "b"}
224
+ expect(reference.debug_dump(:unknown, false)).to eq(reference.inspect)
225
+ end
226
+
227
+ it "should raise an exception if requested" do
228
+ expect { {:a => "b"}.debug_dump }.to raise_error(::Lazier::Exceptions::Dump)
229
+ end
230
+ end
231
+ end
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+ #
3
+ # This file is part of the lazier gem. Copyright (C) 2011 and above Shogun <shogun_panda@me.com>.
4
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
+ #
6
+
7
+ require "spec_helper"
8
+
9
+ describe Lazier::Pathname do
10
+ let(:reference) { ::Pathname.new($0) }
11
+
12
+ before(:all) do
13
+ ::Lazier.load!
14
+ end
15
+
16
+ describe "#components" do
17
+ it "should return the components of the path" do
18
+ expect([""] + reference.components).to eq(reference.to_s.split("/"))
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,118 @@
1
+ # encoding: utf-8
2
+ #
3
+ # This file is part of the lazier gem. Copyright (C) 2011 and above Shogun <shogun_panda@me.com>.
4
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
+ #
6
+
7
+ require "spec_helper"
8
+
9
+ describe Lazier::Settings do
10
+ let(:reference) { ::Lazier::Settings.instance }
11
+ let(:number_reference) { 123456.654321 }
12
+ let(:date_reference) { DateTime.civil(2005, 6, 7, 8, 9, 10, DateTime.rationalize_offset(25200)) }
13
+
14
+ before(:all) do
15
+ Lazier.load!
16
+ end
17
+
18
+ describe "#initialize" do
19
+ it "should create good defaults" do
20
+ settings = ::Lazier::Settings.new
21
+ expect(settings.format_number).to be_a(Hash)
22
+ expect(settings.boolean_names).to be_a(Hash)
23
+ expect(settings.date_names).to be_a(Hash)
24
+ expect(settings.date_formats).to be_a(Hash)
25
+ end
26
+
27
+ it "should create good defaults for the singleton" do
28
+ expect(reference.format_number).to be_a(Hash)
29
+ expect(reference.boolean_names).to be_a(Hash)
30
+ expect(reference.date_names).to be_a(Hash)
31
+ expect(reference.date_formats).to be_a(Hash)
32
+ end
33
+ end
34
+
35
+ describe "#setup_format_number" do
36
+ it "should save format numbering options for usage" do
37
+ reference.setup_format_number(2)
38
+ expect(number_reference.format_number).to eq("123,456.65")
39
+
40
+ reference.setup_format_number(3, "A")
41
+ expect(number_reference.format_number).to eq("123,456A654")
42
+
43
+ reference.setup_format_number(4, "A", "B")
44
+ expect(number_reference.format_number).to eq("123,456A6543 B")
45
+
46
+ reference.setup_format_number(5, "A", "B", "C")
47
+ expect(number_reference.format_number).to eq("123C456A65432 B")
48
+
49
+ reference.setup_format_number
50
+ expect(number_reference.format_number).to eq("123,456.65")
51
+ end
52
+ end
53
+
54
+ describe "#setup_boolean_names" do
55
+ it "should save names for boolean values" do
56
+ reference.setup_boolean_names("TRUE1")
57
+ expect([true.format_boolean, false.format_boolean]).to eq(["TRUE1", "No"])
58
+
59
+ reference.setup_boolean_names(nil, "FALSE1")
60
+ expect([true.format_boolean, false.format_boolean]).to eq(["Yes", "FALSE1"])
61
+
62
+ reference.setup_boolean_names("TRUE2", "FALSE2")
63
+ expect([true.format_boolean, false.format_boolean]).to eq(["TRUE2", "FALSE2"])
64
+
65
+ reference.setup_boolean_names
66
+ expect([true.format_boolean, false.format_boolean]).to eq(["Yes", "No"])
67
+ end
68
+ end
69
+
70
+ describe "#setup_date_formats" do
71
+ it "should save formats for date formatting" do
72
+ reference.setup_date_formats(nil, true)
73
+
74
+ reference.setup_date_formats({:c1 => "%Y"})
75
+ expect(date_reference.lstrftime(:ct_date)).to eq(date_reference.strftime("%Y-%m-%d"))
76
+ expect(date_reference.lstrftime(:c1)).to eq(date_reference.year.to_s)
77
+
78
+ reference.setup_date_formats({:c1 => "%Y"}, true)
79
+ expect(date_reference.lstrftime(:ct_date)).to eq("ct_date")
80
+ expect(date_reference.lstrftime(:c1)).to eq(date_reference.year.to_s)
81
+
82
+ reference.setup_date_formats()
83
+ expect(date_reference.lstrftime(:ct_date)).to eq(date_reference.strftime("%Y-%m-%d"))
84
+ expect(date_reference.lstrftime(:c1)).to eq(date_reference.year.to_s)
85
+
86
+ reference.setup_date_formats(nil, true)
87
+ expect(date_reference.lstrftime(:ct_date)).to eq(date_reference.strftime("%Y-%m-%d"))
88
+ expect(date_reference.lstrftime(:c1)).to eq("c1")
89
+ end
90
+ end
91
+
92
+ describe "#setup_date_names" do
93
+ it "should save names for days and months" do
94
+ reference.setup_date_names
95
+ reference.setup_date_formats({:sdn => "%B %b %A %a"})
96
+
97
+ long_months = 12.times.collect {|i| (i + 1).to_s * 2}
98
+ short_months = 12.times.collect {|i| (i + 1).to_s}
99
+ long_days = 7.times.collect {|i| (i + 1).to_s * 2}
100
+ short_days = 7.times.collect {|i| (i + 1).to_s}
101
+
102
+ reference.setup_date_names(long_months)
103
+ expect(date_reference.lstrftime(:sdn)).to eq("66 Jun Tuesday Tue")
104
+
105
+ reference.setup_date_names(long_months, short_months)
106
+ expect(date_reference.lstrftime(:sdn)).to eq("66 6 Tuesday Tue")
107
+
108
+ reference.setup_date_names(long_months, short_months, long_days)
109
+ expect(date_reference.lstrftime(:sdn)).to eq("66 6 33 Tue")
110
+
111
+ reference.setup_date_names(long_months, short_months, long_days, short_days)
112
+ expect(date_reference.lstrftime(:sdn)).to eq("66 6 33 3")
113
+
114
+ reference.setup_date_names
115
+ expect(date_reference.lstrftime(:sdn)).to eq("June Jun Tuesday Tue")
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,45 @@
1
+ # encoding: utf-8
2
+ #
3
+ # This file is part of the lazier gem. Copyright (C) 2011 and above Shogun <shogun_panda@me.com>.
4
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
+ #
6
+
7
+ require "spec_helper"
8
+
9
+ describe Lazier::String do
10
+ let(:reference) { "abc òùà èé &amp;gt;" }
11
+ let(:translated_reference) { "abc oua ee &amp;gt;" }
12
+ let(:untitleized_reference) { "abc-òùà-èé-&amp;gt;" }
13
+ let(:amp_reference) { "abc òùà èé &gt;" }
14
+
15
+ before(:all) do
16
+ ::Lazier.load!
17
+ end
18
+
19
+ describe "#remove_accents" do
20
+ it "should translate accents" do
21
+ expect(reference.remove_accents).to eq(translated_reference)
22
+ end
23
+ end
24
+
25
+ describe "#untitleize" do
26
+ it "should convert spaces to dashes" do
27
+ expect(reference.untitleize).to eq(untitleized_reference)
28
+ end
29
+ end
30
+
31
+ describe "#replace_ampersands" do
32
+ it "should remove HTML ampersands" do
33
+ expect(reference.replace_ampersands).to eq(amp_reference)
34
+ end
35
+ end
36
+
37
+ describe "#value" do
38
+ it "should return the string itself" do
39
+ expect(reference.value).to eq(reference)
40
+ expect(translated_reference.value).to eq(translated_reference)
41
+ expect(untitleized_reference.value).to eq(untitleized_reference)
42
+ expect(amp_reference.value).to eq(amp_reference)
43
+ end
44
+ end
45
+ end