lazier 2.8.1 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. data/Gemfile +3 -3
  2. data/doc/Lazier.html +69 -99
  3. data/doc/Lazier/Boolean.html +6 -6
  4. data/doc/Lazier/DateTime.html +35 -39
  5. data/doc/Lazier/DateTime/ClassMethods.html +59 -85
  6. data/doc/Lazier/Exceptions.html +6 -6
  7. data/doc/Lazier/Exceptions/Debug.html +133 -0
  8. data/doc/Lazier/Exceptions/MissingTranslation.html +8 -8
  9. data/doc/Lazier/Hash.html +5 -270
  10. data/doc/Lazier/I18n.html +68 -26
  11. data/doc/Lazier/Localizer.html +17 -21
  12. data/doc/Lazier/Math.html +5 -5
  13. data/doc/Lazier/Math/ClassMethods.html +5 -5
  14. data/doc/Lazier/Object.html +681 -218
  15. data/doc/Lazier/Pathname.html +7 -11
  16. data/doc/Lazier/Settings.html +166 -262
  17. data/doc/Lazier/String.html +21 -25
  18. data/doc/Lazier/TimeZone.html +119 -130
  19. data/doc/Lazier/TimeZone/ClassMethods.html +75 -83
  20. data/doc/Lazier/Version.html +8 -8
  21. data/doc/_index.html +7 -7
  22. data/doc/class_list.html +2 -2
  23. data/doc/file.README.html +5 -5
  24. data/doc/file_list.html +1 -1
  25. data/doc/frames.html +1 -1
  26. data/doc/index.html +5 -5
  27. data/doc/js/full_list.js +6 -1
  28. data/doc/method_list.html +134 -324
  29. data/doc/top-level-namespace.html +5 -5
  30. data/lazier.gemspec +4 -3
  31. data/lib/lazier.rb +9 -23
  32. data/lib/lazier/boolean.rb +1 -1
  33. data/lib/lazier/datetime.rb +47 -79
  34. data/lib/lazier/exceptions.rb +4 -4
  35. data/lib/lazier/hash.rb +0 -24
  36. data/lib/lazier/i18n.rb +13 -6
  37. data/lib/lazier/localizer.rb +3 -5
  38. data/lib/lazier/object.rb +107 -78
  39. data/lib/lazier/pathname.rb +1 -3
  40. data/lib/lazier/settings.rb +22 -30
  41. data/lib/lazier/string.rb +3 -5
  42. data/lib/lazier/version.rb +3 -3
  43. data/spec/lazier/datetime_spec.rb +8 -8
  44. data/spec/lazier/hash_spec.rb +9 -10
  45. data/spec/lazier/object_spec.rb +85 -11
  46. data/spec/lazier/settings_spec.rb +10 -17
  47. data/spec/lazier_spec.rb +1 -1
  48. metadata +26 -9
data/lib/lazier/string.rb CHANGED
@@ -18,9 +18,7 @@ module Lazier
18
18
  #
19
19
  # @return The string with all accents removed.
20
20
  def remove_accents
21
- silence_warnings {
22
- self.mb_chars.normalize(:kd).gsub(/[^\x00-\x7F]/n, "").to_s
23
- }
21
+ silence_warnings { mb_chars.normalize(:kd).gsub(/[^\x00-\x7F]/n, "").to_s }
24
22
  end
25
23
 
26
24
  # Returns the tagged version of a string.
@@ -34,14 +32,14 @@ module Lazier
34
32
  #
35
33
  # @return [String] The untitleized version of the string.
36
34
  def untitleize
37
- self.downcase.gsub(" ", "-")
35
+ downcase.gsub(" ", "-")
38
36
  end
39
37
 
40
38
  # Returns the string with all `&` replaced with `&`.
41
39
  #
42
40
  # @return [String] The string with all `&` replaced with `&`.
43
41
  def replace_ampersands
44
- self.gsub(/&(\S+);/, "&\\1;")
42
+ gsub(/&(\S+);/, "&\\1;")
45
43
  end
46
44
 
47
45
  # Returns the string itself for use in form helpers.
@@ -10,13 +10,13 @@ module Lazier
10
10
  # @see http://semver.org
11
11
  module Version
12
12
  # The major version.
13
- MAJOR = 2
13
+ MAJOR = 3
14
14
 
15
15
  # The minor version.
16
- MINOR = 8
16
+ MINOR = 0
17
17
 
18
18
  # The patch version.
19
- PATCH = 1
19
+ PATCH = 0
20
20
 
21
21
  # The current version of lazier.
22
22
  STRING = [MAJOR, MINOR, PATCH].compact.join(".")
@@ -19,30 +19,30 @@ describe Lazier::DateTime do
19
19
 
20
20
  describe ".days" do
21
21
  it "should return the list of the days of the week" do
22
- expect(::DateTime.days).to be_kind_of(::Array)
22
+ expect(::DateTime.days).to be_a(::Array)
23
23
  expect(::DateTime.days[3]).to eq({value: "4", label: "Wed"})
24
- expect(::DateTime.days(false)).to be_kind_of(::Array)
24
+ expect(::DateTime.days(false)).to be_a(::Array)
25
25
  expect(::DateTime.days(false)[3]).to eq({value: "4", label: "Wednesday"})
26
26
 
27
27
  ::Lazier.settings.setup_date_names(nil, nil, 7.times.collect {|i| (i + 1).to_s * 2}, 7.times.collect {|i| (i + 1).to_s})
28
- expect(::DateTime.days).to be_kind_of(::Array)
28
+ expect(::DateTime.days).to be_a(::Array)
29
29
  expect(::DateTime.days[3]).to eq({value: "4", label: "4"})
30
- expect(::DateTime.days(false)).to be_kind_of(::Array)
30
+ expect(::DateTime.days(false)).to be_a(::Array)
31
31
  expect(::DateTime.days(false)[3]).to eq({value: "4", label: "44"})
32
32
  end
33
33
  end
34
34
 
35
35
  describe ".months" do
36
36
  it "should return the list of the months of the year" do
37
- expect(::DateTime.months).to be_kind_of(::Array)
37
+ expect(::DateTime.months).to be_a(::Array)
38
38
  expect(::DateTime.months[6]).to eq({value: "07", label: "Jul"})
39
- expect(::DateTime.months(false)).to be_kind_of(::Array)
39
+ expect(::DateTime.months(false)).to be_a(::Array)
40
40
  expect(::DateTime.months(false)[6]).to eq({value: "07", label: "July"})
41
41
 
42
42
  ::Lazier.settings.setup_date_names(12.times.collect {|i| (i + 1).to_s * 2}, 12.times.collect {|i| (i + 1).to_s}, nil, nil)
43
- expect(::DateTime.months).to be_kind_of(::Array)
43
+ expect(::DateTime.months).to be_a(::Array)
44
44
  expect(::DateTime.months[6]).to eq({value: "07", label: "7"})
45
- expect(::DateTime.months(false)).to be_kind_of(::Array)
45
+ expect(::DateTime.months(false)).to be_a(::Array)
46
46
  expect(::DateTime.months(false)[6]).to eq({value: "07", label: "77"})
47
47
  end
48
48
 
@@ -8,23 +8,22 @@ require "spec_helper"
8
8
 
9
9
  describe Lazier::Hash do
10
10
  let(:reference) {
11
- rv = {a: 1, "b" => 2}
11
+ rv = {a: 1, "b" => {c: 2, d: {"e" => 3}}}
12
12
  rv.default = 0
13
13
  rv
14
14
  }
15
15
 
16
- before(:all) do
16
+ before(:each) do
17
17
  ::Lazier.load!
18
18
  end
19
19
 
20
- describe "allows access to keys using method syntax" do
21
- it "should allow method reference for symbol key" do expect(reference.b).to eq(2) end
22
- it "should use super for missing key" do expect {reference.c}.to raise_error(NoMethodError) end
23
- end
20
+ describe "allows access to keys using dotted notation" do
21
+ it "should allow method reference for symbol key" do
22
+ reference.b.f = 4
24
23
 
25
- describe "#respond_to" do
26
- it "should return true for string key" do expect(reference.respond_to?(:a)).to be_true end
27
- it "should return true for symbol key" do expect(reference.respond_to?(:b)).to be_true end
28
- it "should return false for missing key" do expect(reference.respond_to?(:c)).to be_false end
24
+ expect(reference.a).to eq(1)
25
+ expect(reference.b.c).to eq(2)
26
+ expect(reference["b"]["f"]).to eq(4)
27
+ end
29
28
  end
30
29
  end
@@ -96,6 +96,26 @@ describe Lazier::Object do
96
96
  end
97
97
  end
98
98
 
99
+ describe "#ensure" do
100
+ it "should assign a default value to an object" do
101
+ expect(nil.ensure("VALUE")).to eq("VALUE")
102
+ expect([].ensure("VALUE")).to eq("VALUE")
103
+ expect({}.ensure("VALUE")).to eq("VALUE")
104
+ expect("".ensure("VALUE")).to eq("VALUE")
105
+ end
106
+
107
+ it "should use a different verifier" do
108
+ expect(nil.ensure("VALUE", :present?)).to be_nil
109
+ expect("".ensure("VALUE", :present?)).to eq("")
110
+ expect("STRING".ensure("VALUE", :present?)).to eq("VALUE")
111
+ end
112
+
113
+ it "should use the provided block as verifier" do
114
+ expect([].ensure("VALUE") {|o| o.length == 1} ).to eq([])
115
+ expect(["STRING"].ensure("VALUE") {|o| o.length == 1} ).to eq("VALUE")
116
+ end
117
+ end
118
+
99
119
  describe "#ensure_string" do
100
120
  it "should correctly handle strings" do
101
121
  expect(" abc ".ensure_string).to eq(" abc ")
@@ -104,6 +124,55 @@ describe Lazier::Object do
104
124
  expect(:abc.ensure_string).to eq("abc")
105
125
  expect(nil.ensure_string).to eq("")
106
126
  end
127
+
128
+ it "should default to the default value for nil values" do
129
+ expect(nil.ensure_string("DEFAULT")).to eq("DEFAULT")
130
+ end
131
+
132
+ it "should use a different method or the provided block to stringify value" do
133
+ expect([1, 2, 3].ensure_string("", :join)).to eq("123")
134
+ expect([1, 2, 3].ensure_string("-") {|a, v| a.join(v)} ).to eq("1-2-3")
135
+ end
136
+ end
137
+
138
+ describe "#ensure_array" do
139
+ it "should convert to an array with the object it self or a default value" do
140
+ expect(nil.ensure_array).to eq([nil])
141
+ expect("A".ensure_array).to eq(["A"])
142
+ expect({a: "b"}.ensure_array).to eq([{a: "b"}])
143
+ expect(nil.ensure_array(["1"])).to eq(["1"])
144
+ expect("A".ensure_array(["2"])).to eq(["2"])
145
+ expect({a: "b"}.ensure_array(["3"])).to eq(["3"])
146
+ end
147
+
148
+ it "should sanitize elements of the array using a method or a block" do
149
+ expect(" 123 ".ensure_array).to eq([" 123 "])
150
+ expect(" 123 ".ensure_array(nil, false, false, :strip)).to eq(["123"])
151
+ expect(" 123 ".ensure_array(nil, false, false) { |e| e.reverse }).to eq([" 321 "])
152
+ end
153
+
154
+ it "should unicize and compact array if requested to" do
155
+ expect([1, 2, 3, nil, 3, 2, 1].ensure_array(nil, true, false)).to eq([1, 2, 3, nil])
156
+ expect([1, 2, 3, nil, 3, 2, 1].ensure_array(nil, false, true)).to eq([1, 2, 3, 3, 2, 1])
157
+ expect([1, 2, 3, nil, 3, 2, 1].ensure_array(nil, true, true)).to eq([1, 2, 3])
158
+ end
159
+ end
160
+
161
+ describe "#ensure_hash" do
162
+ it "should return an hash" do
163
+ expect({a: "b"}.ensure_hash).to eq({a: "b"})
164
+ expect(nil.ensure_hash({a: "b"})).to eq({a: "b"})
165
+
166
+ expect(1.ensure_hash).to eq({key: 1})
167
+ expect(1.ensure_hash(:test)).to eq({test: 1})
168
+ expect(1.ensure_hash("test")).to eq({"test" => 1})
169
+ expect(1.ensure_hash(2)).to eq({key: 1})
170
+ end
171
+
172
+ it "should sanitize values" do
173
+ expect(" 1 ".ensure_hash(nil, &:strip)).to eq({key: "1"})
174
+ expect(1.ensure_hash(nil) { |v| v * 2 }).to eq({key: 2})
175
+ end
107
176
  end
108
177
 
109
178
  describe "#to_float" do
@@ -177,10 +246,10 @@ describe Lazier::Object do
177
246
  expect(123.456789.round_to_precision(2)).to eq("123.46")
178
247
  expect(123.456789.round_to_precision(0)).to eq("123")
179
248
  expect("123.456789".round_to_precision(2)).to eq("123.46")
249
+ expect(123.456789.round_to_precision(-1)).to eq("123")
180
250
  end
181
251
 
182
252
  it "should return nil for non numeric values" do
183
- expect(123.456789.round_to_precision(-1)).to eq(nil)
184
253
  expect("abc".round_to_precision(-1)).to eq(nil)
185
254
  end
186
255
  end
@@ -195,10 +264,12 @@ describe Lazier::Object do
195
264
 
196
265
  Lazier.settings.setup_format_number(5, ",", "£", ".")
197
266
  expect(123123.456789.format_number).to eq("123.123,45679 £")
267
+
268
+ Lazier.settings.setup_format_number(2)
269
+ expect(123.456789.format_number(-1)).to eq("123")
198
270
  end
199
271
 
200
272
  it "should return nil for non numeric values" do
201
- expect(123.456789.format_number(-1)).to eq(nil)
202
273
  expect("abc".format_number(-1)).to eq(nil)
203
274
  end
204
275
  end
@@ -224,21 +295,24 @@ describe Lazier::Object do
224
295
  end
225
296
  end
226
297
 
227
- describe "#debug_dump" do
228
- it "should return the correct representation for an object" do
229
- reference = {a: "b"}
230
- expect(reference.debug_dump(:json, false)).to eq(reference.to_json)
231
- expect(reference.debug_dump(:pretty_json, false)).to eq(::JSON.pretty_generate(reference))
232
- expect(reference.debug_dump(:yaml, false)).to eq(reference.to_yaml)
298
+ describe "#indexize" do
299
+ it "should format for printing" do
300
+ expect(1.indexize).to eq("01")
301
+ expect(21.indexize(3, "A")).to eq("A21")
302
+ expect(21.indexize(3, "A", :ljust)).to eq("21A")
233
303
  end
304
+ end
234
305
 
235
- it "should inspect the object if the format is not recognized" do
306
+ describe "#analyze" do
307
+ it "should return the correct representation for an object" do
236
308
  reference = {a: "b"}
237
- expect(reference.debug_dump(:unknown, false)).to eq(reference.inspect)
309
+ expect(reference.analyze(:json, false)).to eq(reference.to_json)
310
+ expect(reference.analyze(:pretty_json, false)).to eq(::JSON.pretty_generate(reference))
311
+ expect(reference.analyze(:yaml, false)).to eq(reference.to_yaml)
238
312
  end
239
313
 
240
314
  it "should raise an exception if requested" do
241
- expect { {a: "b"}.debug_dump }.to raise_error(::Lazier::Exceptions::Dump)
315
+ expect { {a: "b"}.analyze }.to raise_error(::Lazier::Exceptions::Debug)
242
316
  end
243
317
  end
244
318
  end
@@ -36,17 +36,10 @@ describe Lazier::Settings do
36
36
  describe "#initialize" do
37
37
  it "should create good defaults" do
38
38
  settings = ::Lazier::Settings.new
39
- expect(settings.format_number).to be_a(Hash)
40
- expect(settings.boolean_names).to be_a(Hash)
41
- expect(settings.date_names).to be_a(Hash)
42
- expect(settings.date_formats).to be_a(Hash)
43
- end
44
-
45
- it "should create good defaults for the singleton" do
46
- expect(reference.format_number).to be_a(Hash)
47
- expect(reference.boolean_names).to be_a(Hash)
48
- expect(reference.date_names).to be_a(Hash)
49
- expect(reference.date_formats).to be_a(Hash)
39
+ expect(settings.format_number).to be_a(::HashWithIndifferentAccess)
40
+ expect(settings.boolean_names).to be_a(::Hash)
41
+ expect(settings.date_names).to be_a(::HashWithIndifferentAccess)
42
+ expect(settings.date_formats).to be_a(::HashWithIndifferentAccess)
50
43
  end
51
44
  end
52
45
 
@@ -91,18 +84,18 @@ describe Lazier::Settings do
91
84
 
92
85
  reference.setup_date_formats({c1: "%Y"})
93
86
  expect(date_reference.lstrftime(:ct_date)).to eq(date_reference.strftime("%Y-%m-%d"))
94
- expect(date_reference.lstrftime(:c1)).to eq(date_reference.year.to_s)
87
+ expect(date_reference.lstrftime("c1")).to eq(date_reference.year.to_s)
95
88
 
96
89
  reference.setup_date_formats({c1: "%Y"}, true)
97
- expect(date_reference.lstrftime(:ct_date)).to eq("ct_date")
90
+ expect(date_reference.lstrftime("ct_date")).to eq("ct_date")
98
91
  expect(date_reference.lstrftime(:c1)).to eq(date_reference.year.to_s)
99
92
 
100
93
  reference.setup_date_formats()
101
94
  expect(date_reference.lstrftime(:ct_date)).to eq(date_reference.strftime("%Y-%m-%d"))
102
- expect(date_reference.lstrftime(:c1)).to eq(date_reference.year.to_s)
95
+ expect(date_reference.lstrftime("c1")).to eq(date_reference.year.to_s)
103
96
 
104
97
  reference.setup_date_formats(nil, true)
105
- expect(date_reference.lstrftime(:ct_date)).to eq(date_reference.strftime("%Y-%m-%d"))
98
+ expect(date_reference.lstrftime("ct_date")).to eq(date_reference.strftime("%Y-%m-%d"))
106
99
  expect(date_reference.lstrftime(:c1)).to eq("c1")
107
100
  end
108
101
  end
@@ -122,13 +115,13 @@ describe Lazier::Settings do
122
115
  expect(date_reference.lstrftime(:sdn)).to eq("66 Jun Tuesday Tue")
123
116
 
124
117
  reference.setup_date_names(long_months, short_months)
125
- expect(date_reference.lstrftime(:sdn)).to eq("66 6 Tuesday Tue")
118
+ expect(date_reference.lstrftime("sdn")).to eq("66 6 Tuesday Tue")
126
119
 
127
120
  reference.setup_date_names(long_months, short_months, long_days)
128
121
  expect(date_reference.lstrftime(:sdn)).to eq("66 6 33 Tue")
129
122
 
130
123
  reference.setup_date_names(long_months, short_months, long_days, short_days)
131
- expect(date_reference.lstrftime(:sdn)).to eq("66 6 33 3")
124
+ expect(date_reference.lstrftime("sdn")).to eq("66 6 33 3")
132
125
 
133
126
  reference.setup_date_names
134
127
  expect(date_reference.lstrftime(:sdn)).to eq("June Jun Tuesday Tue")
data/spec/lazier_spec.rb CHANGED
@@ -31,7 +31,7 @@ describe Lazier do
31
31
  end
32
32
 
33
33
  it "for Object" do
34
- expect(0).to respond_to("debug_dump")
34
+ expect(0).to respond_to("analyze")
35
35
  end
36
36
 
37
37
  it "for Pathname" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lazier
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.8.1
4
+ version: 3.0.0
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: 2013-04-13 00:00:00.000000000 Z
12
+ date: 2013-05-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
@@ -18,7 +18,7 @@ dependencies:
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: 1.7.7
21
+ version: 1.8.0
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ~>
28
28
  - !ruby/object:Gem::Version
29
- version: 1.7.7
29
+ version: 1.8.0
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: actionpack
32
32
  requirement: !ruby/object:Gem::Requirement
@@ -50,7 +50,7 @@ dependencies:
50
50
  requirements:
51
51
  - - ~>
52
52
  - !ruby/object:Gem::Version
53
- version: 0.3.35
53
+ version: 0.3.37
54
54
  type: :runtime
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
@@ -58,7 +58,7 @@ dependencies:
58
58
  requirements:
59
59
  - - ~>
60
60
  - !ruby/object:Gem::Version
61
- version: 0.3.35
61
+ version: 0.3.37
62
62
  - !ruby/object:Gem::Dependency
63
63
  name: r18n-desktop
64
64
  requirement: !ruby/object:Gem::Requirement
@@ -66,7 +66,7 @@ dependencies:
66
66
  requirements:
67
67
  - - ~>
68
68
  - !ruby/object:Gem::Version
69
- version: 1.1.4
69
+ version: 1.1.5
70
70
  type: :runtime
71
71
  prerelease: false
72
72
  version_requirements: !ruby/object:Gem::Requirement
@@ -74,7 +74,23 @@ dependencies:
74
74
  requirements:
75
75
  - - ~>
76
76
  - !ruby/object:Gem::Version
77
- version: 1.1.4
77
+ version: 1.1.5
78
+ - !ruby/object:Gem::Dependency
79
+ name: hashie
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: 2.0.5
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 2.0.5
78
94
  description: Several Ruby object enhancements.
79
95
  email:
80
96
  - shogun_panda@me.com
@@ -94,6 +110,7 @@ files:
94
110
  - doc/Lazier/DateTime.html
95
111
  - doc/Lazier/DateTime/ClassMethods.html
96
112
  - doc/Lazier/Exceptions.html
113
+ - doc/Lazier/Exceptions/Debug.html
97
114
  - doc/Lazier/Exceptions/Dump.html
98
115
  - doc/Lazier/Exceptions/MissingTranslation.html
99
116
  - doc/Lazier/Hash.html
@@ -171,7 +188,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
171
188
  version: '0'
172
189
  segments:
173
190
  - 0
174
- hash: 4187585535154153470
191
+ hash: -1367305075114908839
175
192
  requirements: []
176
193
  rubyforge_project: lazier
177
194
  rubygems_version: 1.8.25