lazier 3.5.7 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. checksums.yaml +4 -4
  2. data/.travis-gemfile +5 -4
  3. data/.travis.yml +2 -2
  4. data/CHANGELOG.md +65 -8
  5. data/Gemfile +9 -8
  6. data/README.md +4 -0
  7. data/doc/Lazier.html +178 -110
  8. data/doc/Lazier/Boolean.html +7 -7
  9. data/doc/Lazier/Configuration.html +24 -50
  10. data/doc/Lazier/DateTime.html +50 -305
  11. data/doc/Lazier/DateTime/ClassMethods.html +116 -806
  12. data/doc/Lazier/Exceptions.html +2 -2
  13. data/doc/Lazier/Exceptions/Debug.html +1 -1
  14. data/doc/Lazier/Exceptions/MissingTranslation.html +18 -14
  15. data/doc/Lazier/Exceptions/TranslationExceptionHandler.html +213 -0
  16. data/doc/Lazier/Hash.html +19 -155
  17. data/doc/Lazier/I18n.html +1735 -230
  18. data/doc/Lazier/Math.html +1 -1
  19. data/doc/Lazier/Math/ClassMethods.html +13 -13
  20. data/doc/Lazier/Object.html +353 -339
  21. data/doc/Lazier/Pathname.html +4 -4
  22. data/doc/Lazier/Settings.html +150 -304
  23. data/doc/Lazier/String.html +39 -199
  24. data/doc/Lazier/TimeZone.html +244 -746
  25. data/doc/Lazier/TimeZone/ClassMethods.html +109 -127
  26. data/doc/Lazier/Version.html +4 -4
  27. data/doc/_index.html +15 -15
  28. data/doc/class_list.html +1 -1
  29. data/doc/file.README.html +5 -1
  30. data/doc/index.html +5 -1
  31. data/doc/method_list.html +97 -169
  32. data/doc/top-level-namespace.html +1 -1
  33. data/lazier.gemspec +9 -6
  34. data/lib/lazier.rb +41 -50
  35. data/lib/lazier/boolean.rb +0 -1
  36. data/lib/lazier/configuration.rb +26 -28
  37. data/lib/lazier/datetime.rb +33 -127
  38. data/lib/lazier/exceptions.rb +14 -6
  39. data/lib/lazier/hash.rb +7 -15
  40. data/lib/lazier/i18n.rb +130 -48
  41. data/lib/lazier/math.rb +6 -7
  42. data/lib/lazier/object.rb +79 -97
  43. data/lib/lazier/pathname.rb +0 -1
  44. data/lib/lazier/settings.rb +12 -25
  45. data/lib/lazier/string.rb +17 -38
  46. data/lib/lazier/timezone.rb +168 -164
  47. data/lib/lazier/version.rb +3 -4
  48. data/locales/en.yml +52 -51
  49. data/locales/it.yml +51 -50
  50. data/spec/coverage_helper.rb +0 -1
  51. data/spec/lazier/boolean_spec.rb +2 -3
  52. data/spec/lazier/configuration_spec.rb +3 -5
  53. data/spec/lazier/datetime_spec.rb +34 -95
  54. data/spec/lazier/exceptions_spec.rb +25 -0
  55. data/spec/lazier/hash_spec.rb +0 -21
  56. data/spec/lazier/i18n_spec.rb +135 -51
  57. data/spec/lazier/math_spec.rb +0 -1
  58. data/spec/lazier/object_spec.rb +105 -100
  59. data/spec/lazier/pathname_spec.rb +0 -1
  60. data/spec/lazier/settings_spec.rb +25 -28
  61. data/spec/lazier/string_spec.rb +7 -20
  62. data/spec/lazier/timezone_spec.rb +101 -87
  63. data/spec/lazier_spec.rb +25 -8
  64. data/spec/spec_helper.rb +3 -2
  65. metadata +15 -44
  66. data/doc/Lazier/Localizer.html +0 -545
  67. data/lib/lazier/localizer.rb +0 -41
  68. data/spec/lazier/localizer_spec.rb +0 -45
@@ -0,0 +1,25 @@
1
+ #
2
+ # This file is part of the lazier gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>.
3
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
4
+ #
5
+
6
+ require "spec_helper"
7
+
8
+ describe Lazier::Exceptions::TranslationExceptionHandler do
9
+ describe "#call" do
10
+ it "should correctly raise exception" do
11
+ subject = Lazier::Exceptions::TranslationExceptionHandler.new
12
+ expect { subject.call(::I18n::MissingTranslation.new(1, 2, {}), 1, 2, 3) }.to raise_error(::I18n::MissingTranslationData)
13
+ expect { subject.call(RuntimeError.new, 1, 2, 3) }.to raise_error(RuntimeError)
14
+ end
15
+ end
16
+ end
17
+
18
+ describe Lazier::Exceptions::MissingTranslation do
19
+ describe "#initialize" do
20
+ it "should initialize a good exception" do
21
+ subject = Lazier::Exceptions::MissingTranslation.new("LOCALE", "MESSAGE")
22
+ expect(subject.message).to eq("Unable to load the translation \"MESSAGE\" for the locale \"LOCALE\".")
23
+ end
24
+ end
25
+ end
@@ -1,4 +1,3 @@
1
- # encoding: utf-8
2
1
  #
3
2
  # This file is part of the lazier gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>.
4
3
  # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
@@ -17,26 +16,6 @@ describe Object do
17
16
  ::Lazier.load!
18
17
  end
19
18
 
20
- describe "method access" do
21
- it "it is not enabled by default" do
22
- expect { subject.b }.to raise_error(NoMethodError)
23
- end
24
- end
25
-
26
- describe "allows access to keys using dotted notation" do
27
- before(:each) do
28
- ::Lazier.load!(:hash_method_access)
29
- end
30
-
31
- it "should allow method subject for symbol key" do
32
- subject.b.f = 4
33
-
34
- expect(subject.a).to eq(1)
35
- expect(subject.b.c).to eq(2)
36
- expect(subject["b"]["f"]).to eq(4)
37
- end
38
- end
39
-
40
19
  describe "#compact" do
41
20
  it "should remove blank keys" do
42
21
  expect({a: 1, b: nil}.compact).to eq({a: 1})
@@ -1,4 +1,3 @@
1
- # encoding: utf-8
2
1
  #
3
2
  # This file is part of the lazier gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>.
4
3
  # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
@@ -7,76 +6,161 @@
7
6
  require "spec_helper"
8
7
 
9
8
  describe Lazier::I18n do
10
- subject {
11
- c = ::Object.new
12
- c.class_eval { include Lazier::I18n }
13
- c
14
- }
9
+ before(:each) do
10
+ Lazier::I18n.default_locale = :en
11
+ end
15
12
 
16
- let(:root_path) { ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/") }
13
+ subject! { Lazier::I18n.instance(force: true) }
17
14
 
18
- before(:each) do
19
- ENV["LANG"] = "en"
20
- allow(::R18n::I18n).to receive(:system_locale).and_return("en")
15
+ describe ".instance" do
16
+ it "should return the singleton instance" do
17
+ expect(subject).to be_a(Lazier::I18n)
18
+ expect(Lazier::I18n.instance).to be(subject)
19
+ end
20
+
21
+ it "should return a new instance" do
22
+ another = Lazier::I18n.instance(force: true)
23
+ expect(another).not_to be(subject)
24
+ end
21
25
  end
22
26
 
23
- describe "#i18n_setup" do
24
- it "should set the root and the path" do
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)
27
+ describe "#initialize" do
28
+ it "should save attributes" do
29
+ subject = Lazier::I18n.new("it", root: "foo", path: "/dev/abc/..")
30
+ expect(subject.locale).to eq(:it)
31
+ expect(subject.root).to eq(:foo)
32
+ expect(subject.path).to eq("/dev")
33
+ end
34
+
35
+ it "should fallback to the default locale" do
36
+ allow(::I18n).to receive("locale=")
37
+ Lazier::I18n.default_locale = "pt"
38
+ expect(Lazier::I18n.new.locale).to eq(:pt)
39
+ end
40
+
41
+ describe "should fallback to the system locale" do
42
+ before(:each) do
43
+ allow(::I18n).to receive("locale=")
44
+ Lazier::I18n.default_locale = nil
45
+ end
46
+
47
+ it "in JRuby" do
48
+ expect(Lazier).to receive(:platform).exactly(2).and_return(:java)
49
+
50
+ if RUBY_PLATFORM !~ /java/
51
+ stub_const("Java", Object.new)
52
+ allow(Java).to receive(:java).and_return(Object.new)
53
+ allow(Java.java).to receive(:util).and_return(Object.new)
54
+ allow(Java.java.util).to receive(:Locale).and_return(Object.new)
55
+ allow(Java.java.util.Locale).to receive(:getDefault).and_return(Object.new)
56
+ end
57
+
58
+ allow(Java.java.util.Locale.getDefault).to receive(:toString).and_return("jp")
59
+
60
+ expect(Lazier::I18n.new.locale).to eq(:jp)
61
+ end
62
+
63
+ it "in OSX" do
64
+ allow_any_instance_of(Lazier::I18n).to receive(:`).with("defaults read .GlobalPreferences AppleLanguages | awk 'NR==2{gsub(/[ ,]/, \"\");print}'").and_return("in")
65
+
66
+ expect(Lazier).to receive(:platform).exactly(2).and_return(:osx)
67
+ expect(Lazier::I18n.new.locale).to eq(:in)
68
+ end
69
+
70
+ it "in UNIX" do
71
+ old_env = ENV["LANG"]
72
+
73
+ ENV["LANG"] = "it"
74
+ expect(Lazier).to receive(:platform).exactly(2).and_return(:posix)
75
+ expect(Lazier::I18n.new.locale).to eq(:it)
76
+ ENV["LANG"] = old_env
77
+ end
78
+
79
+ it "falling back to English" do
80
+ expect(Lazier).to receive(:platform).and_return(:other)
81
+
82
+ expect(Lazier::I18n.new.locale).to eq(:en)
83
+ end
84
+ end
85
+
86
+ it "should setup the backend" do
87
+ open("/tmp/it.yml", "w") {|f| f.write("---\nit:\n lazier:") }
88
+ subject = Lazier::I18n.new("it", path: "/tmp")
89
+
90
+ expect(::I18n.enforce_available_locales).to be_truthy
91
+ expect(::I18n.load_path).to include("/tmp/it.yml")
92
+ expect(::I18n.exception_handler).to be_a(::Lazier::Exceptions::TranslationExceptionHandler)
93
+ expect(subject.backend).to be_a(::I18n::Backend::Simple)
94
+ File.unlink("/tmp/it.yml")
95
+ ::I18n.load_path.delete("/tmp/it.yml")
96
+ end
97
+ end
98
+
99
+ describe "#reload" do
100
+ it "should reload translations" do
101
+ expect_any_instance_of(::I18n::Backend::Simple).to receive(:load_translations).and_call_original
102
+ subject.reload
28
103
  end
29
104
  end
30
105
 
31
- describe "#i18n" do
32
- it "should call the private method if nothing is set" do
33
- subject.instance_variable_set(:@i18n, nil)
34
- expect(subject).to receive(:i18n_load_locale)
35
- subject.i18n
106
+ describe "#translations" do
107
+ it "should return the list of translations" do
108
+ expect(subject.translations.keys).to eq([:date, :time, :support, :number, :lazier])
36
109
  end
37
110
  end
38
111
 
39
- describe "#i18n=" do
40
- it "should call the private method if nothing is set" do
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")
112
+ describe "#locale=" do
113
+ it "should assign the new locale" do
114
+ expect(::I18n).to receive("locale=").with(:it)
115
+ subject.locale = "it"
116
+ expect(subject.locale).to eq(:it)
44
117
  end
45
118
  end
46
119
 
47
- describe "#i18n_load_locale" do
48
- it "should set using system locale if called without arguments" do
49
- subject.i18n_setup("lazier", root_path)
50
- expect(::R18n::I18n).to receive(:new).with([ENV["LANG"]].compact.uniq, root_path).and_call_original
51
- subject.i18n = nil
120
+ describe "#locales" do
121
+ it "should return the list of locales" do
122
+ expect(subject.locales).to eq([:en, :it])
52
123
  end
124
+ end
53
125
 
54
- it "should set the requested locale" do
55
- subject.i18n_setup("lazier", root_path)
56
- expect(::R18n::I18n).to receive(:new).with(["it", ENV["LANG"]].compact.uniq, root_path).and_call_original
57
- subject.i18n = :it
126
+ describe "#translate" do
127
+ it "should return the translation" do
128
+ expect(subject.translate("configuration.not_defined", name: "foo", class: "bar")).to eq("Property foo is not defined for bar.")
129
+ expect(subject.translate(".date.formats")).to eq({:default=>"%Y-%m-%d", :short=>"%b %d", :long=>"%B %d, %Y"})
130
+ expect { subject.translate("::configuration.not_defined", property_name: "foo", class: "bar") }.to raise_error(Lazier::Exceptions::MissingTranslation)
58
131
  end
132
+ end
59
133
 
60
- it "should call the root" do
61
- Lazier.load!
62
- t = ::Object.new
63
- expect_any_instance_of(::R18n::I18n).to receive(:t).and_return(t)
64
- expect(t).to receive("ROOT")
65
- subject.i18n_setup("ROOT", root_path)
66
- subject.i18n = :it
134
+ describe "#translate_in_locale" do
135
+ it "should return the translation in the desired locale" do
136
+ expect(subject.translate_in_locale(:it, "configuration.not_defined", name: "foo", class: "bar")).to eq("La proprietà foo non è definita per bar.")
67
137
  end
138
+ end
68
139
 
69
- it "should only pass valid translations" do
70
- subject.i18n_setup("lazier", root_path)
71
- expect(::R18n::I18n).to receive(:new).with([ENV["LANG"]].compact.uniq, root_path).and_call_original
72
- subject.i18n = "INVALID"
140
+ describe "#with_locale" do
141
+ it "should execute a block with the new locale and then set the old locale back" do
142
+ new_locale = nil
143
+
144
+ subject.with_locale(:it) do
145
+ new_locale = subject.locale
146
+ end
147
+
148
+ expect(new_locale).to eq(:it)
149
+ expect(subject.locale).to eq(:en)
73
150
  end
74
151
 
75
- it "should raise an exception if no valid translation are found" do
76
- ENV["LANG"] = "INVALID"
77
- allow(::R18n::I18n).to receive(:system_locale).and_return("INVALID")
78
- subject.i18n_setup("ROOT", "/dev/")
79
- expect { subject.i18n = "INVALID" }.to raise_error(::Lazier::Exceptions::MissingTranslation)
152
+ it "should raise exception after having restored the old locale" do
153
+ new_locale = nil
154
+
155
+ expect {
156
+ subject.with_locale(:it) do
157
+ new_locale = subject.locale
158
+ raise RuntimeError
159
+ end
160
+ }.to raise_error(RuntimeError)
161
+
162
+ expect(new_locale).to eq(:it)
163
+ expect(subject.locale).to eq(:en)
80
164
  end
81
165
  end
82
- end
166
+ end
@@ -1,4 +1,3 @@
1
- # encoding: utf-8
2
1
  #
3
2
  # This file is part of the lazier gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>.
4
3
  # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
@@ -1,4 +1,3 @@
1
- # encoding: utf-8
2
1
  #
3
2
  # This file is part of the lazier gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>.
4
3
  # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
@@ -25,90 +24,78 @@ describe Lazier::Object do
25
24
  end
26
25
  end
27
26
 
28
- describe "#is_number?" do
27
+ describe "#number?" do
29
28
  it "should return true for a valid number" do
30
- expect("123.45".is_number?).to be_true
31
- expect("1,23.45".is_number?).to be_true
32
- expect("-1.23.45".is_number?).to be_true
33
- expect("+123.45".is_number?).to be_true
34
- expect("+1.231,45".is_number?).to be_true
29
+ expect("123.45".number?).to be_truthy
30
+ expect("1,23.45".number?).to be_truthy
31
+ expect("-1.23.45".number?).to be_truthy
32
+ expect("+123.45".number?).to be_truthy
33
+ expect("+1.231,45".number?).to be_truthy
34
+ expect(true.number?).to be_truthy
35
+ expect(false.number?).to be_truthy
36
+ expect(nil.number?).to be_truthy
37
+ expect("a".number?(Float, /[a-z]/)).to be_truthy
35
38
  end
36
39
 
37
40
  it "should return false for a invalid number" do
38
- expect("s213".is_number?).to be_false
41
+ expect("s213".number?).to be_falsey
39
42
  end
40
43
  end
41
44
 
42
- describe "#is_numeric?" do
45
+ describe "#integer?" do
43
46
  it "should return true for a valid number" do
44
- expect(true.is_numeric?).to be_true
45
- expect(false.is_numeric?).to be_true
46
- expect(nil.is_numeric?).to be_true
47
- expect("123".is_numeric?).to be_true
48
- expect("-123".is_numeric?).to be_true
49
- expect("+123.45".is_numeric?(Float, ::Lazier::Object::FLOAT_MATCHER)).to be_true
47
+ expect(true.integer?).to be_truthy
48
+ expect(false.integer?).to be_truthy
49
+ expect(nil.integer?).to be_truthy
50
+ expect("123".integer?).to be_truthy
51
+ expect("-123".integer?).to be_truthy
52
+ expect("+123".integer?).to be_truthy
50
53
  end
51
54
 
52
55
  it "should return false for a invalid number" do
53
- expect("s123".is_numeric?).to be_false
54
- expect("123.12".is_numeric?).to be_false
55
- end
56
- end
57
-
58
- describe "#is_integer?" do
59
- it "should return true for a valid number" do
60
- expect(true.is_integer?).to be_true
61
- expect(false.is_integer?).to be_true
62
- expect(nil.is_integer?).to be_true
63
- expect("123".is_integer?).to be_true
64
- expect("-123".is_integer?).to be_true
65
- expect("+123".is_integer?).to be_true
66
- end
67
-
68
- it "should return false for a invalid number" do
69
- expect("s123".is_integer?).to be_false
70
- expect("123.12".is_integer?).to be_false
56
+ expect("s123".integer?).to be_falsey
57
+ expect("123.12".integer?).to be_falsey
71
58
  end
72
59
  end
73
60
 
74
- describe "#is_float?" do
61
+ describe "#float?" do
75
62
  it "should return true for a valid number" do
76
- expect(true.is_float?).to be_true
77
- expect(false.is_float?).to be_true
78
- expect(nil.is_float?).to be_true
79
- expect("123.45".is_float?).to be_true
80
- expect("1,23.45".is_float?).to be_true
81
- expect("-1.23.45".is_float?).to be_true
82
- expect("+123.45".is_float?).to be_true
83
- expect("+1.231,45".is_float?).to be_true
63
+ expect(true.float?).to be_truthy
64
+ expect(false.float?).to be_truthy
65
+ expect(nil.float?).to be_truthy
66
+ expect("123.45".float?).to be_truthy
67
+ expect("1,23.45".float?).to be_truthy
68
+ expect("-1.23.45".float?).to be_truthy
69
+ expect("+123.45".float?).to be_truthy
70
+ expect("+1.231,45".float?).to be_truthy
84
71
  end
85
72
 
86
73
  it "should return false for a invalid number" do
87
- expect("s213".is_float?).to be_false
74
+ expect("s213".float?).to be_falsey
88
75
  end
89
76
  end
90
77
 
91
- describe "#is_boolean?" do
78
+ describe "#boolean?" do
92
79
  it "should return true for a valid boolean" do
93
- expect(true.is_boolean?).to be_true
94
- expect(false.is_boolean?).to be_true
95
- expect(nil.is_boolean?).to be_true
96
- expect("y".is_boolean?).to be_true
97
- expect("n".is_boolean?).to be_true
98
- expect("yes".is_boolean?).to be_true
99
- expect("no".is_boolean?).to be_true
100
- expect("1".is_boolean?).to be_true
101
- expect("0".is_boolean?).to be_true
102
- expect("true".is_boolean?).to be_true
103
- expect("false".is_boolean?).to be_true
104
- expect("t".is_boolean?).to be_true
105
- expect("f".is_boolean?).to be_true
106
- expect(1.is_boolean?).to be_true
107
- expect(0.is_boolean?).to be_true
80
+ expect(true.boolean?).to be_truthy
81
+ expect(false.boolean?).to be_truthy
82
+ expect(nil.boolean?).to be_truthy
83
+ expect("y".boolean?).to be_truthy
84
+ expect("n".boolean?).to be_truthy
85
+ expect("yes".boolean?).to be_truthy
86
+ expect("no".boolean?).to be_truthy
87
+ expect("1".boolean?).to be_truthy
88
+ expect("0".boolean?).to be_truthy
89
+ expect("true".boolean?).to be_truthy
90
+ expect("false".boolean?).to be_truthy
91
+ expect("t".boolean?).to be_truthy
92
+ expect("f".boolean?).to be_truthy
93
+ expect(1.boolean?).to be_truthy
94
+ expect(0.boolean?).to be_truthy
108
95
  end
109
96
 
110
97
  it "should return false for a invalid boolean" do
111
- expect("11".is_boolean?).to be_false
98
+ expect("11".boolean?).to be_falsey
112
99
  end
113
100
  end
114
101
 
@@ -167,46 +154,46 @@ describe Lazier::Object do
167
154
  expect(nil.ensure_array).to eq([])
168
155
  expect("A".ensure_array).to eq(["A"])
169
156
  expect({a: "b"}.ensure_array).to eq([{a: "b"}])
170
- expect(nil.ensure_array(["1"])).to eq(["1"])
171
- expect("A".ensure_array(["2"])).to eq(["2"])
172
- expect({a: "b"}.ensure_array(["3"])).to eq(["3"])
157
+ expect(nil.ensure_array(default: ["1"])).to eq(["1"])
158
+ expect("A".ensure_array(default: ["2"])).to eq(["2"])
159
+ expect({a: "b"}.ensure_array(default: ["3"])).to eq(["3"])
173
160
  end
174
161
 
175
162
  it "should sanitize elements of the array using a method or a block" do
176
163
  expect(" 123 ".ensure_array).to eq([" 123 "])
177
- expect(" 123 ".ensure_array(nil, false, false, false, :strip)).to eq(["123"])
178
- expect(" 123 ".ensure_array(nil, false, false) { |e| e.reverse }).to eq([" 321 "])
164
+ expect(" 123 ".ensure_array(sanitizer: :strip)).to eq(["123"])
165
+ expect(" 123 ".ensure_array { |e| e.reverse }).to eq([" 321 "])
179
166
  end
180
167
 
181
168
  it "should unicize, compact and flatten, array if requested to" do
182
- expect([1, 2, 3, nil, 3, 2, 1, [4]].ensure_array(nil, true, false)).to eq([1, 2, 3, nil, [4]])
183
- expect([1, 2, 3, nil, 3, 2, 1, [4]].ensure_array(nil, false, true)).to eq([1, 2, 3, 3, 2, 1, [4]])
184
- expect([1, 2, 3, nil, 3, 2, 1, [4]].ensure_array(nil, true, true)).to eq([1, 2, 3, [4]])
185
- expect([1, 2, 3, nil, 3, 2, 1, [4]].ensure_array(nil, true, true, true)).to eq([1, 2, 3, 4])
169
+ expect([1, 2, 3, nil, 3, 2, 1, [4]].ensure_array(no_duplicates: true)).to eq([1, 2, 3, nil, [4]])
170
+ expect([1, 2, 3, nil, 3, 2, 1, [4]].ensure_array(compact: true)).to eq([1, 2, 3, 3, 2, 1, [4]])
171
+ expect([1, 2, 3, nil, 3, 2, 1, [4]].ensure_array(no_duplicates: true, compact: true)).to eq([1, 2, 3, [4]])
172
+ expect([1, 2, 3, nil, 3, 2, 1, [4]].ensure_array(no_duplicates: true, compact: true, flatten: true)).to eq([1, 2, 3, 4])
186
173
  end
187
174
  end
188
175
 
189
176
  describe "#ensure_hash" do
190
177
  it "should return an hash" do
191
178
  expect({a: "b"}.ensure_hash).to eq({a: "b"})
192
- expect(nil.ensure_hash(nil, {a: "b"})).to eq({a: "b"})
179
+ expect(nil.ensure_hash(default: {a: "b"})).to eq({a: "b"})
193
180
 
194
181
  expect(1.ensure_hash).to eq({})
195
- expect(1.ensure_hash(nil, :test)).to eq({test: 1})
196
- expect(1.ensure_hash(nil, "test")).to eq({"test" => 1})
197
- expect(1.ensure_hash(nil, 2)).to eq({key: 1})
182
+ expect(1.ensure_hash(default: :test)).to eq({test: 1})
183
+ expect(1.ensure_hash(default: "test")).to eq({"test" => 1})
184
+ expect(1.ensure_hash(default: 2)).to eq({key: 1})
198
185
  end
199
186
 
200
187
  it "should sanitize values" do
201
- expect(" 1 ".ensure_hash(nil, :key, &:strip)).to eq({key: "1"})
202
- expect(1.ensure_hash(nil, :key) { |v| v * 2 }).to eq({key: 2})
188
+ expect(" 1 ".ensure_hash(default: nil, sanitizer: :strip)).to eq({key: "1"})
189
+ expect(1.ensure_hash(default: nil) { |v| v * 2 }).to eq({key: 2})
203
190
  end
204
191
 
205
192
  it "should grant access" do
206
193
  subject = {a: "b"}
207
194
 
208
- expect(subject ).to receive(:ensure_access).with("ACCESS")
209
- subject .ensure_hash("ACCESS")
195
+ expect(subject ).to receive(:ensure_access).with(["ACCESS"])
196
+ subject.ensure_hash(accesses: "ACCESS")
210
197
  end
211
198
  end
212
199
 
@@ -224,7 +211,7 @@ describe Lazier::Object do
224
211
  expect("+1.231,45".to_float).to eq(1231.45)
225
212
  end
226
213
 
227
- it "should return 0.0 for a invalid number without a default" do
214
+ it "should return 0.0 for a invalid number? without a default" do
228
215
  expect("s213".to_float).to eq(0.0)
229
216
  end
230
217
 
@@ -244,7 +231,7 @@ describe Lazier::Object do
244
231
  expect("-123".to_integer).to eq(-123)
245
232
  end
246
233
 
247
- it "should return 0 for a invalid number without a default" do
234
+ it "should return 0 for a invalid number? without a default" do
248
235
  expect("s213".to_integer).to eq(0)
249
236
  end
250
237
 
@@ -292,25 +279,25 @@ describe Lazier::Object do
292
279
 
293
280
  describe "#format_number" do
294
281
  it "should format number" do
295
- expect(123.format_number(0)).to eq("123")
282
+ expect(123.format_number(precision: 0)).to eq("123")
296
283
  expect(123.456789.format_number).to eq("123.46")
297
284
  expect(12312.456789.format_number).to eq("12,312.46")
298
285
  expect(123123.456789.format_number).to eq("123,123.46")
299
286
  expect(1123123.456789.format_number).to eq("1,123,123.46")
300
- expect(123123.456789.format_number(2)).to eq("123,123.46")
301
- expect(123123.456789.format_number(3, "@")).to eq("123,123@457")
302
- expect(123123.456789.format_number(3, "@", "$")).to eq("123,123@457 $")
303
- expect("123123.456789".format_number(3, "@", "$", "!")).to eq("123!123@457 $")
287
+ expect(123123.456789.format_number(precision: 2)).to eq("123,123.46")
288
+ expect(123123.456789.format_number(precision: 3, decimal_separator: "@")).to eq("123,123@457")
289
+ expect(123123.456789.format_number(precision: 3, decimal_separator: "@", add_string: "$")).to eq("123,123@457 $")
290
+ expect("123123.456789".format_number(precision: 3, decimal_separator: "@", add_string: "$", k_separator: "!")).to eq("123!123@457 $")
304
291
 
305
- Lazier.settings.setup_format_number(5, ",", "£", ".")
292
+ Lazier.settings.setup_format_number(precision: 5, decimal_separator: ",", add_string: "£", k_separator:".")
306
293
  expect(123123.456789.format_number).to eq("123.123,45679 £")
307
294
 
308
- Lazier.settings.setup_format_number(2)
309
- expect(123.456789.format_number(-1)).to eq("123")
295
+ Lazier.settings.setup_format_number(precision: 2)
296
+ expect(123.456789.format_number(precision: -1)).to eq("123")
310
297
  end
311
298
 
312
299
  it "should return nil for non numeric values" do
313
- expect("abc".format_number(-1)).to eq(nil)
300
+ expect("abc".format_number(precision: -1)).to eq(nil)
314
301
  end
315
302
  end
316
303
 
@@ -321,38 +308,56 @@ describe Lazier::Object do
321
308
  end
322
309
 
323
310
  it "should support localization" do
324
- Lazier.settings.setup_boolean_names("YYY", "NNN")
311
+ Lazier.settings.setup_boolean_names(true_name: "YYY", false_name: "NNN")
325
312
  expect("yes".format_boolean).to eq("YYY")
326
313
  expect("abc".format_boolean).to eq("NNN")
327
314
  end
328
315
 
329
316
  it "should support overrides" do
330
317
  Lazier.settings.setup_boolean_names
331
- expect("yes".format_boolean("TTT")).to eq("TTT")
332
- expect("yes".format_boolean(nil, "FFF")).to eq("Yes")
333
- expect("abc".format_boolean("TTT")).to eq("No")
334
- expect("abc".format_boolean(nil, "FFF")).to eq("FFF")
318
+ expect("yes".format_boolean(true_name: "TTT")).to eq("TTT")
319
+ expect("yes".format_boolean(false_name: "FFF")).to eq("Yes")
320
+ expect("abc".format_boolean(true_name: "TTT")).to eq("No")
321
+ expect("abc".format_boolean(false_name: "FFF")).to eq("FFF")
335
322
  end
336
323
  end
337
324
 
338
325
  describe "#indexize" do
339
326
  it "should format for printing" do
340
327
  expect(1.indexize).to eq("01")
341
- expect(21.indexize(3, "A")).to eq("A21")
342
- expect(21.indexize(3, "A", :ljust)).to eq("21A")
328
+ expect(21.indexize(length: 3, filler: "A")).to eq("A21")
329
+ expect(21.indexize(length: 3, filler: "A", formatter: :ljust)).to eq("21A")
330
+ end
331
+ end
332
+
333
+ describe "#to_pretty_json" do
334
+ subject { {a: "b", c: [1, 2, "3"]} }
335
+
336
+ it "should use json gem for JRuby" do
337
+ allow(Lazier).to receive(:platform).and_return(:java)
338
+ stub_const("JSON", "JSON")
339
+ expect(JSON).to receive(:pretty_generate).with(subject).and_return("JSON")
340
+ expect(subject.to_pretty_json).to eq("JSON")
341
+ end
342
+
343
+ it "should use Oj gem for other implementations" do
344
+ allow(Lazier).to receive(:platform).and_return(:posix)
345
+ stub_const("Oj", "OJ")
346
+ expect(Oj).to receive(:dump).with(subject, mode: :compat, indent: 2).and_return("JSON")
347
+ expect(subject.to_pretty_json).to eq("JSON")
343
348
  end
344
349
  end
345
350
 
346
- describe "#for_debug" do
351
+ describe "#to_debug" do
347
352
  it "should return the correct representation for an object" do
348
353
  subject = {a: "b"}
349
- expect(subject.for_debug(:json, false)).to eq(subject .to_json)
350
- expect(subject.for_debug(:pretty_json, false)).to eq(::JSON.pretty_generate(subject ))
351
- expect(subject.for_debug(:yaml, false)).to eq(subject .to_yaml)
354
+ expect(subject.to_debug(format: :json, as_exception: false)).to eq(subject.to_json)
355
+ expect(subject.to_debug(format: :pretty_json, as_exception: false)).to eq(subject.to_pretty_json)
356
+ expect(subject.to_debug(format: :yaml, as_exception: false)).to eq(subject.to_yaml)
352
357
  end
353
358
 
354
359
  it "should raise an exception if requested" do
355
- expect { {a: "b"}.for_debug }.to raise_error(::Lazier::Exceptions::Debug)
360
+ expect { {a: "b"}.to_debug }.to raise_error(::Lazier::Exceptions::Debug)
356
361
  end
357
362
  end
358
363
  end