lazier 3.5.0 → 3.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.DS_Store +0 -0
- data/CHANGELOG.md +4 -0
- data/doc/Lazier.html +1 -1
- data/doc/Lazier/Boolean.html +1 -1
- data/doc/Lazier/Configuration.html +1 -1
- data/doc/Lazier/DateTime.html +1 -1
- data/doc/Lazier/DateTime/ClassMethods.html +1 -1
- data/doc/Lazier/Exceptions.html +1 -1
- data/doc/Lazier/Exceptions/Debug.html +1 -1
- data/doc/Lazier/Exceptions/MissingTranslation.html +1 -1
- data/doc/Lazier/Hash.html +69 -36
- data/doc/Lazier/I18n.html +1 -1
- data/doc/Lazier/Localizer.html +1 -1
- data/doc/Lazier/Math.html +1 -1
- data/doc/Lazier/Math/ClassMethods.html +1 -1
- data/doc/Lazier/Object.html +1 -1
- data/doc/Lazier/Pathname.html +1 -1
- data/doc/Lazier/Settings.html +1 -1
- data/doc/Lazier/String.html +1 -1
- data/doc/Lazier/TimeZone.html +1 -1
- data/doc/Lazier/TimeZone/ClassMethods.html +1 -1
- data/doc/Lazier/Version.html +2 -2
- data/doc/_index.html +1 -1
- data/doc/file.README.html +1 -1
- data/doc/index.html +1 -1
- data/doc/top-level-namespace.html +1 -1
- data/lib/lazier/hash.rb +15 -4
- data/lib/lazier/version.rb +1 -1
- data/spec/lazier/configuration_spec.rb +3 -3
- data/spec/lazier/datetime_spec.rb +59 -59
- data/spec/lazier/hash_spec.rb +28 -27
- data/spec/lazier/i18n_spec.rb +21 -21
- data/spec/lazier/object_spec.rb +7 -7
- data/spec/lazier/pathname_spec.rb +2 -2
- data/spec/lazier/settings_spec.rb +43 -43
- data/spec/lazier/string_spec.rb +11 -11
- metadata +4 -3
data/lib/lazier/hash.rb
CHANGED
@@ -7,6 +7,14 @@
|
|
7
7
|
module Lazier
|
8
8
|
# Extensions for Hash objects.
|
9
9
|
module Hash
|
10
|
+
# The supported accesses for #ensure_access
|
11
|
+
VALID_ACCESSES = {
|
12
|
+
strings: :stringify_keys,
|
13
|
+
symbols: :symbolize_keys,
|
14
|
+
indifferent: :with_indifferent_access,
|
15
|
+
dotted: :enable_dotted_access
|
16
|
+
}
|
17
|
+
|
10
18
|
extend ::ActiveSupport::Concern
|
11
19
|
|
12
20
|
# Returns a new hash, removing all keys which values are blank.
|
@@ -28,11 +36,14 @@ module Lazier
|
|
28
36
|
|
29
37
|
# Makes sure that the keys of the hash are accessible in the desired way.
|
30
38
|
#
|
31
|
-
# @param
|
39
|
+
# @param accesses [Array] The requested access for the keys. Can be `:strings`, `:symbols` or `:indifferent`. If `nil` the keys are not modified.
|
32
40
|
# @return [Hash] The current hash with keys modified.
|
33
|
-
def ensure_access(
|
34
|
-
|
35
|
-
|
41
|
+
def ensure_access(*accesses)
|
42
|
+
accesses.compact.inject(self) do |rv, access|
|
43
|
+
method = VALID_ACCESSES.fetch(access.ensure_string.to_sym, nil)
|
44
|
+
rv = rv.send(method) if method
|
45
|
+
rv
|
46
|
+
end
|
36
47
|
end
|
37
48
|
|
38
49
|
# Makes sure that the hash is accessible using dotted notation. This is also applied to every embedded hash.
|
data/lib/lazier/version.rb
CHANGED
@@ -22,10 +22,10 @@ describe Lazier::Configuration do
|
|
22
22
|
end
|
23
23
|
|
24
24
|
it "should not allow writing readonly properties" do
|
25
|
-
|
25
|
+
subject = ConfigurationSpecSample.new(required: 1)
|
26
26
|
|
27
|
-
expect {
|
28
|
-
expect(
|
27
|
+
expect { subject .readonly = "4" }.to raise_error(ArgumentError)
|
28
|
+
expect(subject .readonly).to eq("2")
|
29
29
|
end
|
30
30
|
|
31
31
|
it "should blow up for require properties" do
|
@@ -7,9 +7,9 @@
|
|
7
7
|
require "spec_helper"
|
8
8
|
|
9
9
|
describe Lazier::DateTime do
|
10
|
-
let(:
|
11
|
-
let(:
|
12
|
-
let(:
|
10
|
+
let(:random_subject) { ::DateTime.civil(1990 + rand(30), 1 + rand(10), 1 + rand(25), 1 + rand(20), 1 + rand(58), 1 + rand(58)).in_time_zone }
|
11
|
+
let(:fixed_subject){ ::DateTime.civil(2005, 6, 7, 8, 9, 10, ::DateTime.rationalize_offset(25200)) }
|
12
|
+
let(:subject_zone) { ::ActiveSupport::TimeZone["Mountain Time (US & Canada)"] }
|
13
13
|
let(:zone_without_dst) { ::ActiveSupport::TimeZone["International Date Line West"] }
|
14
14
|
|
15
15
|
before(:all) do
|
@@ -73,7 +73,7 @@ describe Lazier::DateTime do
|
|
73
73
|
describe ".find_timezone" do
|
74
74
|
it "should forward to ActiveSupport::TimeZone" do
|
75
75
|
expect(::ActiveSupport::TimeZone).to receive(:find)
|
76
|
-
::DateTime.find_timezone(
|
76
|
+
::DateTime.find_timezone(subject_zone.name)
|
77
77
|
end
|
78
78
|
end
|
79
79
|
|
@@ -87,14 +87,14 @@ describe Lazier::DateTime do
|
|
87
87
|
describe ".parameterize_zone" do
|
88
88
|
it "should forward to ActiveSupport::TimeZone" do
|
89
89
|
expect(::ActiveSupport::TimeZone).to receive(:parameterize_zone)
|
90
|
-
::DateTime.parameterize_zone(
|
90
|
+
::DateTime.parameterize_zone(subject_zone)
|
91
91
|
end
|
92
92
|
end
|
93
93
|
|
94
94
|
describe ".unparameterize_zone" do
|
95
95
|
it "should forward to ActiveSupport::TimeZone" do
|
96
96
|
expect(::ActiveSupport::TimeZone).to receive(:unparameterize_zone)
|
97
|
-
::DateTime.unparameterize_zone(
|
97
|
+
::DateTime.unparameterize_zone(subject_zone)
|
98
98
|
end
|
99
99
|
end
|
100
100
|
|
@@ -136,40 +136,40 @@ describe Lazier::DateTime do
|
|
136
136
|
|
137
137
|
describe "#utc_time" do
|
138
138
|
it "should convert to UTC Time" do
|
139
|
-
expect(
|
139
|
+
expect(random_subject.utc_time).to be_a(::Time)
|
140
140
|
end
|
141
141
|
end
|
142
142
|
|
143
143
|
describe "#in_months" do
|
144
|
-
it "should return the amount of months passed since the start of the
|
144
|
+
it "should return the amount of months passed since the start of the subject year" do
|
145
145
|
expect(::Date.today.in_months).to eq(::Date.today.month)
|
146
|
-
expect(
|
146
|
+
expect(fixed_subject.in_months(2000)).to eq(66)
|
147
147
|
end
|
148
148
|
end
|
149
149
|
|
150
150
|
describe "#padded_month" do
|
151
151
|
it "should pad the month number" do
|
152
|
-
expect(
|
152
|
+
expect(random_subject.padded_month).to eq(random_subject.month.to_s.rjust(2, "0"))
|
153
153
|
expect(::Date.civil(2000, 8, 8).padded_month).to eq("08")
|
154
154
|
end
|
155
155
|
end
|
156
156
|
|
157
157
|
describe "#lstrftime" do
|
158
158
|
it "should return corrected formatted string" do
|
159
|
-
expect(
|
160
|
-
expect(
|
161
|
-
expect(
|
159
|
+
expect(fixed_subject.lstrftime(:db)).to eq("db")
|
160
|
+
expect(fixed_subject.lstrftime("%F")).to eq("2005-06-07")
|
161
|
+
expect(fixed_subject.lstrftime(:ct_iso_8601)).to eq("2005-06-07T08:09:10+0700")
|
162
162
|
|
163
163
|
::Lazier.settings.setup_date_names
|
164
164
|
::Lazier.settings.setup_date_formats({ct_local_test: "%a %A %b %B %d %Y %H"})
|
165
|
-
expect(
|
165
|
+
expect(fixed_subject.lstrftime(:ct_local_test)).to eq("Tue Tuesday Jun June 07 2005 08")
|
166
166
|
|
167
167
|
::Lazier.settings.setup_date_names(
|
168
168
|
12.times.map {|i| (i + 1).to_s * 2}, 12.times.map {|i| (i + 1).to_s},
|
169
169
|
7.times.map {|i| (i + 1).to_s * 2}, 7.times.map {|i| (i + 1).to_s}
|
170
170
|
)
|
171
171
|
|
172
|
-
expect(
|
172
|
+
expect(fixed_subject.lstrftime(:ct_local_test)).to eq("3 33 6 66 07 2005 08")
|
173
173
|
end
|
174
174
|
end
|
175
175
|
|
@@ -177,7 +177,7 @@ describe Lazier::DateTime do
|
|
177
177
|
it "should retrieve the date in the current timezone" do
|
178
178
|
::Time.zone = ::ActiveSupport::TimeZone[0]
|
179
179
|
::Lazier.settings.setup_date_formats({ct_local_test: "%a %A %b %B %d %Y %H"})
|
180
|
-
expect(
|
180
|
+
expect(fixed_subject.local_strftime(:ct_local_test)).to eq("Tue Tuesday Jun June 07 2005 01")
|
181
181
|
end
|
182
182
|
end
|
183
183
|
|
@@ -193,14 +193,14 @@ describe Lazier::DateTime do
|
|
193
193
|
7.times.map {|i| (i + 1).to_s * 2}, 7.times.map {|i| (i + 1).to_s}
|
194
194
|
)
|
195
195
|
|
196
|
-
expect(
|
196
|
+
expect(fixed_subject.local_lstrftime(:ct_local_test)).to eq("3 33 6 66 07 2005 01")
|
197
197
|
end
|
198
198
|
end
|
199
199
|
end
|
200
200
|
|
201
201
|
describe Lazier::TimeZone do
|
202
|
-
let(:
|
203
|
-
let(:
|
202
|
+
let(:subject_zone) { ::ActiveSupport::TimeZone["Mountain Time (US & Canada)"] }
|
203
|
+
let(:subject_zone) { ::ActiveSupport::TimeZone["Mountain Time (US & Canada)"] }
|
204
204
|
let(:zone_without_dst) { ::ActiveSupport::TimeZone["International Date Line West"] }
|
205
205
|
|
206
206
|
before(:all) do
|
@@ -225,28 +225,28 @@ describe Lazier::TimeZone do
|
|
225
225
|
|
226
226
|
describe ".parameterize_zone" do
|
227
227
|
it "should return the parameterized version of the zone" do
|
228
|
-
expect(::ActiveSupport::TimeZone.parameterize_zone(
|
229
|
-
expect(::ActiveSupport::TimeZone.parameterize_zone(
|
230
|
-
expect(::ActiveSupport::TimeZone.parameterize_zone(
|
228
|
+
expect(::ActiveSupport::TimeZone.parameterize_zone(subject_zone.to_str)).to eq(subject_zone.to_str_parameterized)
|
229
|
+
expect(::ActiveSupport::TimeZone.parameterize_zone(subject_zone.to_str)).to eq(subject_zone.to_str_parameterized)
|
230
|
+
expect(::ActiveSupport::TimeZone.parameterize_zone(subject_zone.to_str, false)).to eq(subject_zone.to_str_parameterized(false))
|
231
231
|
expect(::ActiveSupport::TimeZone.parameterize_zone("INVALID")).to eq("invalid")
|
232
232
|
end
|
233
233
|
end
|
234
234
|
|
235
235
|
describe ".unparameterize_zone" do
|
236
236
|
it "should return the parameterized version of the zone" do
|
237
|
-
expect(::ActiveSupport::TimeZone.unparameterize_zone(
|
238
|
-
expect(::ActiveSupport::TimeZone.unparameterize_zone(
|
239
|
-
expect(::ActiveSupport::TimeZone.unparameterize_zone(
|
240
|
-
expect(::ActiveSupport::TimeZone.unparameterize_zone(
|
237
|
+
expect(::ActiveSupport::TimeZone.unparameterize_zone(subject_zone.to_str_parameterized)).to eq(subject_zone)
|
238
|
+
expect(::ActiveSupport::TimeZone.unparameterize_zone(subject_zone.to_str_parameterized, true)).to eq(subject_zone.to_str)
|
239
|
+
expect(::ActiveSupport::TimeZone.unparameterize_zone(subject_zone.to_str_with_dst_parameterized)).to eq(subject_zone)
|
240
|
+
expect(::ActiveSupport::TimeZone.unparameterize_zone(subject_zone.to_str_with_dst_parameterized, true)).to eq(subject_zone.to_str_with_dst)
|
241
241
|
expect(::ActiveSupport::TimeZone.unparameterize_zone("INVALID")).to eq(nil)
|
242
242
|
end
|
243
243
|
end
|
244
244
|
|
245
245
|
describe ".find" do
|
246
246
|
it "should find timezones" do
|
247
|
-
expect(::ActiveSupport::TimeZone.find("(GMT-07:00) Mountain Time (US & Canada)")).to eq(
|
248
|
-
expect(::ActiveSupport::TimeZone.find("(GMT-06:00) Mountain Time (US & Canada) (DST)")).to eq(
|
249
|
-
expect(::ActiveSupport::TimeZone.find("(GMT-06:00) Mountain Time (US & Canada) Daylight Saving Time", "Daylight Saving Time")).to eq(
|
247
|
+
expect(::ActiveSupport::TimeZone.find("(GMT-07:00) Mountain Time (US & Canada)")).to eq(subject_zone)
|
248
|
+
expect(::ActiveSupport::TimeZone.find("(GMT-06:00) Mountain Time (US & Canada) (DST)")).to eq(subject_zone)
|
249
|
+
expect(::ActiveSupport::TimeZone.find("(GMT-06:00) Mountain Time (US & Canada) Daylight Saving Time", "Daylight Saving Time")).to eq(subject_zone)
|
250
250
|
expect(::ActiveSupport::TimeZone.find("INVALID", "INVALID")).to be_nil
|
251
251
|
end
|
252
252
|
end
|
@@ -254,21 +254,21 @@ describe Lazier::TimeZone do
|
|
254
254
|
describe ".list_all" do
|
255
255
|
it "should list all timezones" do
|
256
256
|
expect(::ActiveSupport::TimeZone.list_all(false)).to eq(::ActiveSupport::TimeZone.all.map(&:to_s))
|
257
|
-
expect(::ActiveSupport::TimeZone.list_all(true)).to include("(GMT-06:00) #{
|
258
|
-
expect(::ActiveSupport::TimeZone.list_all(true, "Daylight Saving Time")).to include("(GMT-06:00) #{
|
257
|
+
expect(::ActiveSupport::TimeZone.list_all(true)).to include("(GMT-06:00) #{subject_zone.aliases.first} (DST)")
|
258
|
+
expect(::ActiveSupport::TimeZone.list_all(true, "Daylight Saving Time")).to include("(GMT-06:00) #{subject_zone.aliases.first} Daylight Saving Time")
|
259
259
|
end
|
260
260
|
end
|
261
261
|
|
262
262
|
describe "#offset" do
|
263
263
|
it "should correctly return zone offset" do
|
264
|
-
expect(
|
264
|
+
expect(subject_zone.offset).to eq(subject_zone.utc_offset)
|
265
265
|
end
|
266
266
|
end
|
267
267
|
|
268
268
|
describe "#current_offset" do
|
269
269
|
it "should correctly return current zone offset" do
|
270
|
-
expect(
|
271
|
-
expect(
|
270
|
+
expect(subject_zone.current_offset(false, ::DateTime.civil(2012, 1, 15))).to eq(subject_zone.offset)
|
271
|
+
expect(subject_zone.current_offset(true, ::DateTime.civil(2012, 7, 15))).to eq(subject_zone.dst_offset(true))
|
272
272
|
end
|
273
273
|
end
|
274
274
|
|
@@ -283,44 +283,44 @@ describe Lazier::TimeZone do
|
|
283
283
|
|
284
284
|
describe "#dst_period" do
|
285
285
|
it "should correctly return zone offset" do
|
286
|
-
expect(
|
287
|
-
expect(
|
286
|
+
expect(subject_zone.dst_period).to be_a(::TZInfo::TimezonePeriod)
|
287
|
+
expect(subject_zone.dst_period(1000)).to be_nil
|
288
288
|
expect(zone_without_dst.dst_period).to be_nil
|
289
289
|
end
|
290
290
|
end
|
291
291
|
|
292
292
|
describe "#uses_dst?" do
|
293
293
|
it "should correctly detect offset usage" do
|
294
|
-
expect(
|
295
|
-
expect(
|
296
|
-
expect(
|
297
|
-
expect(
|
294
|
+
expect(subject_zone.uses_dst?).to be_true
|
295
|
+
expect(subject_zone.uses_dst?(::DateTime.civil(2012, 7, 15))).to be_true
|
296
|
+
expect(subject_zone.uses_dst?(::DateTime.civil(2012, 1, 15))).to be_false
|
297
|
+
expect(subject_zone.uses_dst?(1000)).to be_false
|
298
298
|
expect(zone_without_dst.uses_dst?).to be_false
|
299
299
|
end
|
300
300
|
end
|
301
301
|
|
302
302
|
describe "#dst_name" do
|
303
303
|
it "should correctly get zone name with Daylight Saving Time" do
|
304
|
-
expect(
|
305
|
-
expect(
|
306
|
-
expect(
|
304
|
+
expect(subject_zone.dst_name).to eq("Mountain Time (US & Canada) (DST)")
|
305
|
+
expect(subject_zone.dst_name("Daylight Saving Time")).to eq("Mountain Time (US & Canada) Daylight Saving Time")
|
306
|
+
expect(subject_zone.dst_name(nil, 1000)).to be_nil
|
307
307
|
expect(zone_without_dst.to_str_with_dst).to be_nil
|
308
308
|
end
|
309
309
|
end
|
310
310
|
|
311
311
|
describe "#dst_correction" do
|
312
312
|
it "should correctly detect offset usage" do
|
313
|
-
expect(
|
314
|
-
expect(
|
315
|
-
expect(
|
313
|
+
expect(subject_zone.dst_correction).to eq(3600)
|
314
|
+
expect(subject_zone.dst_correction(true)).to eq(Rational(1, 24))
|
315
|
+
expect(subject_zone.dst_correction(false, 1000)).to eq(0)
|
316
316
|
expect(zone_without_dst.dst_correction).to eq(0)
|
317
317
|
end
|
318
318
|
end
|
319
319
|
|
320
320
|
describe "#dst_offset" do
|
321
321
|
it "should correctly return zone offset" do
|
322
|
-
expect(
|
323
|
-
expect(
|
322
|
+
expect(subject_zone.dst_offset).to eq(subject_zone.dst_correction + subject_zone.utc_offset)
|
323
|
+
expect(subject_zone.dst_offset(true)).to eq(::ActiveSupport::TimeZone.rationalize_offset(subject_zone.dst_correction + subject_zone.utc_offset))
|
324
324
|
expect(zone_without_dst.dst_offset(false, 1000)).to eq(0)
|
325
325
|
expect(zone_without_dst.dst_offset).to eq(0)
|
326
326
|
end
|
@@ -328,28 +328,28 @@ describe Lazier::TimeZone do
|
|
328
328
|
|
329
329
|
describe "#to_str_with_dst" do
|
330
330
|
it "should correctly format zone with Daylight Saving Time" do
|
331
|
-
expect(
|
332
|
-
expect(
|
333
|
-
expect(
|
334
|
-
expect(
|
331
|
+
expect(subject_zone.to_str_with_dst).to eq("(GMT-06:00) #{subject_zone.aliases.first} (DST)")
|
332
|
+
expect(subject_zone.to_str_with_dst("Daylight Saving Time")).to eq("(GMT-06:00) #{subject_zone.aliases.first} Daylight Saving Time")
|
333
|
+
expect(subject_zone.to_str_with_dst("Daylight Saving Time", nil, "NAME")).to eq("(GMT-06:00) NAME Daylight Saving Time")
|
334
|
+
expect(subject_zone.to_str_with_dst(nil, 1000)).to be_nil
|
335
335
|
expect(zone_without_dst.to_str_with_dst).to be_nil
|
336
336
|
end
|
337
337
|
end
|
338
338
|
|
339
339
|
describe "#to_str_parameterized" do
|
340
340
|
it "should correctly format (parameterized) zone" do
|
341
|
-
expect(
|
342
|
-
expect(
|
343
|
-
expect(
|
341
|
+
expect(subject_zone.to_str_parameterized).to eq(::ActiveSupport::TimeZone.parameterize_zone(subject_zone.to_str))
|
342
|
+
expect(subject_zone.to_str_parameterized(false)).to eq(::ActiveSupport::TimeZone.parameterize_zone(subject_zone.to_str, false))
|
343
|
+
expect(subject_zone.to_str_parameterized(false, "NAME SPACE")).to eq(::ActiveSupport::TimeZone.parameterize_zone("NAME SPACE", false))
|
344
344
|
end
|
345
345
|
end
|
346
346
|
|
347
347
|
describe "#to_str_with_dst_parameterized" do
|
348
348
|
it "should correctly format (parameterized) zone with Daylight Saving Time" do
|
349
|
-
expect(
|
350
|
-
expect(
|
351
|
-
expect(
|
352
|
-
expect(
|
349
|
+
expect(subject_zone.to_str_with_dst_parameterized).to eq("-0600@america-denver-dst")
|
350
|
+
expect(subject_zone.to_str_with_dst_parameterized("Daylight Saving Time")).to eq("-0600@america-denver-daylight-saving-time")
|
351
|
+
expect(subject_zone.to_str_with_dst_parameterized(nil, 1000)).to be_nil
|
352
|
+
expect(subject_zone.to_str_with_dst_parameterized("Daylight Saving Time", nil, "NAME SPACE")).to eq("-0600@name-space-daylight-saving-time")
|
353
353
|
expect(zone_without_dst.to_str_with_dst_parameterized).to be_nil
|
354
354
|
end
|
355
355
|
end
|
data/spec/lazier/hash_spec.rb
CHANGED
@@ -7,7 +7,7 @@
|
|
7
7
|
require "spec_helper"
|
8
8
|
|
9
9
|
describe Lazier::Hash do
|
10
|
-
|
10
|
+
subject {
|
11
11
|
rv = {a: 1, "b" => {c: 2, d: {"e" => 3}}}
|
12
12
|
rv.default = 0
|
13
13
|
rv
|
@@ -17,10 +17,9 @@ describe Lazier::Hash do
|
|
17
17
|
::Lazier.load!
|
18
18
|
end
|
19
19
|
|
20
|
-
|
21
20
|
describe "method access" do
|
22
21
|
it "it is not enabled by default" do
|
23
|
-
expect {
|
22
|
+
expect { subject.b }.to raise_error(NoMethodError)
|
24
23
|
end
|
25
24
|
end
|
26
25
|
|
@@ -29,12 +28,12 @@ describe Lazier::Hash do
|
|
29
28
|
::Lazier.load!(:hash_method_access)
|
30
29
|
end
|
31
30
|
|
32
|
-
it "should allow method
|
33
|
-
|
31
|
+
it "should allow method subject for symbol key" do
|
32
|
+
subject.b.f = 4
|
34
33
|
|
35
|
-
expect(
|
36
|
-
expect(
|
37
|
-
expect(
|
34
|
+
expect(subject.a).to eq(1)
|
35
|
+
expect(subject.b.c).to eq(2)
|
36
|
+
expect(subject["b"]["f"]).to eq(4)
|
38
37
|
end
|
39
38
|
end
|
40
39
|
|
@@ -48,23 +47,23 @@ describe Lazier::Hash do
|
|
48
47
|
end
|
49
48
|
|
50
49
|
it "should not be destructive" do
|
51
|
-
|
52
|
-
|
53
|
-
expect(
|
50
|
+
subject = {a: 1, b: nil}
|
51
|
+
subject.compact
|
52
|
+
expect(subject).to eq({a: 1, b: nil})
|
54
53
|
end
|
55
54
|
end
|
56
55
|
|
57
56
|
describe "#compact!" do
|
58
57
|
it "should remove blank keys" do
|
59
|
-
|
60
|
-
|
61
|
-
expect(
|
58
|
+
subject = {a: 1, b: nil}
|
59
|
+
subject.compact!
|
60
|
+
expect(subject).to eq({a: 1})
|
62
61
|
end
|
63
62
|
|
64
63
|
it "should use a custom validator" do
|
65
|
-
|
66
|
-
|
67
|
-
expect(
|
64
|
+
subject = {a: 1, b: nil, c: 3}
|
65
|
+
subject.compact! {|k, v| v == 1 || k == :c}
|
66
|
+
expect(subject).to eq({b: nil})
|
68
67
|
end
|
69
68
|
end
|
70
69
|
|
@@ -79,25 +78,27 @@ describe Lazier::Hash do
|
|
79
78
|
expect({a: "b"}.ensure_access(:indifferent)).to be_a(::HashWithIndifferentAccess)
|
80
79
|
expect({a: "b"}.ensure_access(:other)).to eq({a: "b"})
|
81
80
|
|
82
|
-
|
83
|
-
expect(
|
84
|
-
expect(
|
81
|
+
accessed = subject.ensure_access(:indifferent, :dotted)
|
82
|
+
expect(accessed[:a]).to eq(1)
|
83
|
+
expect(accessed["a"]).to eq(1)
|
84
|
+
expect(accessed.a).to eq(1)
|
85
|
+
expect(accessed.b.c).to eq(2)
|
85
86
|
end
|
86
87
|
end
|
87
88
|
|
88
89
|
describe "#with_dotted_access" do
|
89
90
|
it "should recursively enable dotted access on a hash" do
|
90
|
-
|
91
|
+
subject = {a: 1, b: {c: 3}, c: [1, {f: {g: 1}}]}
|
91
92
|
|
92
|
-
|
93
|
-
expect(
|
94
|
-
expect(
|
93
|
+
subject.enable_dotted_access
|
94
|
+
expect(subject.b.c).to eq(3)
|
95
|
+
expect(subject.c[1].f.g).to eq(1)
|
95
96
|
end
|
96
97
|
|
97
98
|
it "should also provide write access if asked" do
|
98
|
-
|
99
|
-
expect {
|
100
|
-
expect(
|
99
|
+
subject.enable_dotted_access(false)
|
100
|
+
expect { subject.b.f = 4 }.not_to raise_error
|
101
|
+
expect(subject["b"]["f"]).to eq(4)
|
101
102
|
end
|
102
103
|
end
|
103
104
|
end
|
data/spec/lazier/i18n_spec.rb
CHANGED
@@ -7,8 +7,8 @@
|
|
7
7
|
require "spec_helper"
|
8
8
|
|
9
9
|
describe Lazier::I18n do
|
10
|
-
|
11
|
-
c = Object.new
|
10
|
+
subject {
|
11
|
+
c = ::Object.new
|
12
12
|
c.class_eval { include Lazier::I18n }
|
13
13
|
c
|
14
14
|
}
|
@@ -22,39 +22,39 @@ describe Lazier::I18n do
|
|
22
22
|
|
23
23
|
describe "#i18n_setup" do
|
24
24
|
it "should set the root and the path" do
|
25
|
-
|
26
|
-
expect(
|
27
|
-
expect(
|
25
|
+
subject.i18n_setup("ROOT", root_path)
|
26
|
+
expect(subject.instance_variable_get(:@i18n_root)).to eq(:ROOT)
|
27
|
+
expect(subject.instance_variable_get(:@i18n_locales_path)).to eq(root_path)
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
31
31
|
describe "#i18n" do
|
32
32
|
it "should call the private method if nothing is set" do
|
33
|
-
|
34
|
-
expect(
|
35
|
-
|
33
|
+
subject.instance_variable_set(:@i18n, nil)
|
34
|
+
expect(subject).to receive(:i18n_load_locale)
|
35
|
+
subject.i18n
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
39
|
describe "#i18n=" do
|
40
40
|
it "should call the private method if nothing is set" do
|
41
|
-
expect(
|
42
|
-
|
43
|
-
expect(
|
41
|
+
expect(subject).to receive(:i18n_load_locale).and_return("LOCALE")
|
42
|
+
subject.i18n = :en
|
43
|
+
expect(subject.instance_variable_get(:@i18n)).to eq("LOCALE")
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
47
|
describe "#i18n_load_locale" do
|
48
48
|
it "should set using system locale if called without arguments" do
|
49
|
-
|
49
|
+
subject.i18n_setup("lazier", root_path)
|
50
50
|
expect(::R18n::I18n).to receive(:new).with([ENV["LANG"]].compact.uniq, root_path).and_call_original
|
51
|
-
|
51
|
+
subject.i18n = nil
|
52
52
|
end
|
53
53
|
|
54
54
|
it "should set the requested locale" do
|
55
|
-
|
55
|
+
subject.i18n_setup("lazier", root_path)
|
56
56
|
expect(::R18n::I18n).to receive(:new).with(["it", ENV["LANG"]].compact.uniq, root_path).and_call_original
|
57
|
-
|
57
|
+
subject.i18n = :it
|
58
58
|
end
|
59
59
|
|
60
60
|
it "should call the root" do
|
@@ -62,21 +62,21 @@ describe Lazier::I18n do
|
|
62
62
|
t = ::Object.new
|
63
63
|
expect_any_instance_of(::R18n::I18n).to receive(:t).and_return(t)
|
64
64
|
expect(t).to receive("ROOT")
|
65
|
-
|
66
|
-
|
65
|
+
subject.i18n_setup("ROOT", root_path)
|
66
|
+
subject.i18n = :it
|
67
67
|
end
|
68
68
|
|
69
69
|
it "should only pass valid translations" do
|
70
|
-
|
70
|
+
subject.i18n_setup("lazier", root_path)
|
71
71
|
expect(::R18n::I18n).to receive(:new).with([ENV["LANG"]].compact.uniq, root_path).and_call_original
|
72
|
-
|
72
|
+
subject.i18n = "INVALID"
|
73
73
|
end
|
74
74
|
|
75
75
|
it "should raise an exception if no valid translation are found" do
|
76
76
|
ENV["LANG"] = "INVALID"
|
77
77
|
allow(::R18n::I18n).to receive(:system_locale).and_return("INVALID")
|
78
|
-
|
79
|
-
expect {
|
78
|
+
subject.i18n_setup("ROOT", "/dev/")
|
79
|
+
expect { subject.i18n = "INVALID" }.to raise_error(::Lazier::Exceptions::MissingTranslation)
|
80
80
|
end
|
81
81
|
end
|
82
82
|
end
|