lazier 3.2.7 → 3.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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 +102 -1
- 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 +69 -48
- data/doc/Lazier/Pathname.html +1 -1
- data/doc/Lazier/Settings.html +7 -7
- 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 +3 -3
- data/doc/_index.html +1 -1
- data/doc/file.README.html +1 -1
- data/doc/index.html +1 -1
- data/doc/method_list.html +91 -85
- data/doc/top-level-namespace.html +1 -1
- data/lib/lazier/hash.rb +8 -0
- data/lib/lazier/object.rb +8 -5
- data/lib/lazier/settings.rb +1 -1
- data/lib/lazier/version.rb +2 -2
- data/spec/lazier/hash_spec.rb +13 -0
- data/spec/lazier/object_spec.rb +13 -6
- metadata +2 -2
@@ -103,7 +103,7 @@
|
|
103
103
|
</div>
|
104
104
|
|
105
105
|
<div id="footer">
|
106
|
-
Generated on Sat Jul 20
|
106
|
+
Generated on Sat Jul 20 13:22:19 2013 by
|
107
107
|
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
108
108
|
0.8.6.2 (ruby-1.9.3).
|
109
109
|
</div>
|
data/lib/lazier/hash.rb
CHANGED
@@ -8,5 +8,13 @@ module Lazier
|
|
8
8
|
# Extensions for Hash objects.
|
9
9
|
module Hash
|
10
10
|
extend ::ActiveSupport::Concern
|
11
|
+
|
12
|
+
# Makes sure that the keys of the hash are accessible in the desired way.
|
13
|
+
#
|
14
|
+
# @param access [Symbol|NilClass] The requested access for the keys. Can be `:strings`, `:symbols` or `:indifferent`. If `nil` the keys are not modified.
|
15
|
+
def ensure_access(access)
|
16
|
+
method = {strings: :stringify_keys, symbols: :symbolize_keys, indifferent: :with_indifferent_access}.fetch(access, nil)
|
17
|
+
method ? send(method) : self
|
18
|
+
end
|
11
19
|
end
|
12
20
|
end
|
data/lib/lazier/object.rb
CHANGED
@@ -90,10 +90,13 @@ module Lazier
|
|
90
90
|
|
91
91
|
# Makes sure that the object is an hash. For non hash objects, return an hash basing on the `default_value` parameter.
|
92
92
|
#
|
93
|
-
# @param
|
93
|
+
# @param access [Symbol|NilClass] The requested access for the keys of the returned object. Can be `:strings`, `:symbols` or `indifferent`. If `nil` the keys are not modified.
|
94
|
+
# @param default_value [Hash|String|Symbol|NilClass] The default value to use. If it is an `Hash`, it is returned as value otherwise it is used to build as a key to build an hash with the current object as only value (everything but strings and symbols are mapped to `key`). Passing `nil` is equal to pass an empty Hash.
|
94
95
|
# @param sanitizer [Symbol|nil] If not `nil`, the method to use to sanitize values of the hash. *Ignored if a block is present.*
|
95
96
|
# @return [Hash] If the object is an hash, then the object itself, a hash with the object as single value otherwise.
|
96
|
-
def ensure_hash(default_value =
|
97
|
+
def ensure_hash(access = nil, default_value = nil, sanitizer = nil)
|
98
|
+
default_value = {} if default_value.nil?
|
99
|
+
|
97
100
|
rv = if is_a?(::Hash) then
|
98
101
|
self
|
99
102
|
elsif default_value.is_a?(::Hash) then
|
@@ -104,13 +107,13 @@ module Lazier
|
|
104
107
|
end
|
105
108
|
|
106
109
|
if block_given? || sanitizer then
|
107
|
-
rv.inject({}) {|h, (k, v)|
|
110
|
+
rv = rv.inject({}) {|h, (k, v)|
|
108
111
|
h[k] = block_given? ? yield(v) : v.send(sanitizer)
|
109
112
|
h
|
110
113
|
}
|
111
|
-
else
|
112
|
-
rv
|
113
114
|
end
|
115
|
+
|
116
|
+
rv.ensure_access(access)
|
114
117
|
end
|
115
118
|
|
116
119
|
# Converts the object to a boolean.
|
data/lib/lazier/settings.rb
CHANGED
@@ -88,7 +88,7 @@ module Lazier
|
|
88
88
|
def setup_date_formats(formats = nil, replace = false)
|
89
89
|
@date_formats = HashWithIndifferentAccess.new if replace || !@date_formats
|
90
90
|
|
91
|
-
@date_formats.merge!(formats.ensure_hash({ct_date: "%Y-%m-%d", ct_time: "%H:%M:%S", ct_date_time: "%F %T", ct_iso_8601: "%FT%T%z" }))
|
91
|
+
@date_formats.merge!(formats.ensure_hash(nil, {ct_date: "%Y-%m-%d", ct_time: "%H:%M:%S", ct_date_time: "%F %T", ct_iso_8601: "%FT%T%z" }))
|
92
92
|
::Time::DATE_FORMATS.merge!(@date_formats)
|
93
93
|
|
94
94
|
@date_formats
|
data/lib/lazier/version.rb
CHANGED
data/spec/lazier/hash_spec.rb
CHANGED
@@ -26,4 +26,17 @@ describe Lazier::Hash do
|
|
26
26
|
expect(reference["b"]["f"]).to eq(4)
|
27
27
|
end
|
28
28
|
end
|
29
|
+
|
30
|
+
describe "#ensure_access" do
|
31
|
+
it "should make sure that the requested access is granted" do
|
32
|
+
expect({"a" => "b"}.ensure_access(:strings)).to eq({"a" => "b"})
|
33
|
+
expect({"a" => "b"}.ensure_access(:symbols)).to eq({a: "b"})
|
34
|
+
expect({"a" => "b"}.ensure_access(:indifferent)).to be_a(::HashWithIndifferentAccess)
|
35
|
+
expect({"a" => "b"}.ensure_access(:other)).to eq({"a" => "b"})
|
36
|
+
expect({a: "b"}.ensure_access(:strings)).to eq({"a" => "b"})
|
37
|
+
expect({a: "b"}.ensure_access(:symbols)).to eq({a: "b"})
|
38
|
+
expect({a: "b"}.ensure_access(:indifferent)).to be_a(::HashWithIndifferentAccess)
|
39
|
+
expect({a: "b"}.ensure_access(:other)).to eq({a: "b"})
|
40
|
+
end
|
41
|
+
end
|
29
42
|
end
|
data/spec/lazier/object_spec.rb
CHANGED
@@ -162,17 +162,24 @@ describe Lazier::Object do
|
|
162
162
|
describe "#ensure_hash" do
|
163
163
|
it "should return an hash" do
|
164
164
|
expect({a: "b"}.ensure_hash).to eq({a: "b"})
|
165
|
-
expect(nil.ensure_hash({a: "b"})).to eq({a: "b"})
|
165
|
+
expect(nil.ensure_hash(nil, {a: "b"})).to eq({a: "b"})
|
166
166
|
|
167
167
|
expect(1.ensure_hash).to eq({})
|
168
|
-
expect(1.ensure_hash(:test)).to eq({test: 1})
|
169
|
-
expect(1.ensure_hash("test")).to eq({"test" => 1})
|
170
|
-
expect(1.ensure_hash(2)).to eq({key: 1})
|
168
|
+
expect(1.ensure_hash(nil, :test)).to eq({test: 1})
|
169
|
+
expect(1.ensure_hash(nil, "test")).to eq({"test" => 1})
|
170
|
+
expect(1.ensure_hash(nil, 2)).to eq({key: 1})
|
171
171
|
end
|
172
172
|
|
173
173
|
it "should sanitize values" do
|
174
|
-
expect(" 1 ".ensure_hash(nil, &:strip)).to eq({key: "1"})
|
175
|
-
expect(1.ensure_hash(nil) { |v| v * 2 }).to eq({key: 2})
|
174
|
+
expect(" 1 ".ensure_hash(nil, :key, &:strip)).to eq({key: "1"})
|
175
|
+
expect(1.ensure_hash(nil, :key) { |v| v * 2 }).to eq({key: 2})
|
176
|
+
end
|
177
|
+
|
178
|
+
it "should grant access" do
|
179
|
+
reference = {a: "b"}
|
180
|
+
|
181
|
+
expect(reference).to receive(:ensure_access).with("ACCESS")
|
182
|
+
reference.ensure_hash("ACCESS")
|
176
183
|
end
|
177
184
|
end
|
178
185
|
|
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: 3.
|
4
|
+
version: 3.3.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -191,7 +191,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
191
191
|
version: '0'
|
192
192
|
segments:
|
193
193
|
- 0
|
194
|
-
hash:
|
194
|
+
hash: 1071624567568461025
|
195
195
|
requirements: []
|
196
196
|
rubyforge_project: lazier
|
197
197
|
rubygems_version: 1.8.25
|