lazier 3.2.7 → 3.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -103,7 +103,7 @@
103
103
  </div>
104
104
 
105
105
  <div id="footer">
106
- Generated on Sat Jul 20 12:56:18 2013 by
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>
@@ -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
@@ -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 default_value [Hash|Object|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`).
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 = {}, sanitizer = nil)
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.
@@ -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
@@ -13,10 +13,10 @@ module Lazier
13
13
  MAJOR = 3
14
14
 
15
15
  # The minor version.
16
- MINOR = 2
16
+ MINOR = 3
17
17
 
18
18
  # The patch version.
19
- PATCH = 7
19
+ PATCH = 0
20
20
 
21
21
  # The current version of lazier.
22
22
  STRING = [MAJOR, MINOR, PATCH].compact.join(".")
@@ -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
@@ -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.2.7
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: -1967236641489505845
194
+ hash: 1071624567568461025
195
195
  requirements: []
196
196
  rubyforge_project: lazier
197
197
  rubygems_version: 1.8.25