fetchable 0.1.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/fetchable.rb +8 -21
- data/lib/fetchable/version.rb +1 -1
- metadata +2 -2
data/lib/fetchable.rb
CHANGED
@@ -1,33 +1,20 @@
|
|
1
1
|
module Fetchable
|
2
|
-
|
3
|
-
|
2
|
+
NO_DEFAULT_GIVEN = Object.new
|
3
|
+
|
4
|
+
def fetch(key, not_found_value = NO_DEFAULT_GIVEN)
|
5
|
+
if not_found_value != NO_DEFAULT_GIVEN && block_given?
|
4
6
|
raise ArgumentError.new("Cannot provide both a default arg and block to #fetch")
|
5
7
|
end
|
6
8
|
|
7
9
|
result = public_send(:[], key)
|
8
10
|
|
9
11
|
if result.nil?
|
10
|
-
|
11
|
-
|
12
|
-
result
|
13
|
-
end
|
14
|
-
end
|
12
|
+
return yield(key) if block_given?
|
13
|
+
return not_found_value unless not_found_value == NO_DEFAULT_GIVEN
|
15
14
|
|
16
|
-
|
17
|
-
|
18
|
-
def no_default_given
|
19
|
-
@default ||= Object.new
|
20
|
-
end
|
21
|
-
|
22
|
-
def default_value(key, not_found_value, &block)
|
23
|
-
if not_found_value == no_default_given
|
24
|
-
if block
|
25
|
-
block.call(key)
|
26
|
-
else
|
27
|
-
raise KeyError.new("key not found #{key}")
|
28
|
-
end
|
15
|
+
raise KeyError.new("key not found #{key}")
|
29
16
|
else
|
30
|
-
|
17
|
+
result
|
31
18
|
end
|
32
19
|
end
|
33
20
|
end
|
data/lib/fetchable/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fetchable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2013-
|
13
|
+
date: 2013-11-07 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: bundler
|