grab 0.0.1 → 0.0.2
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/README.md +13 -11
- data/lib/grab.rb +10 -4
- data/lib/grab/version.rb +1 -1
- metadata +1 -1
data/README.md
CHANGED
@@ -2,20 +2,22 @@
|
|
2
2
|
|
3
3
|
Grab provides an clean way to fetch multiple values from a hash. Instead of:
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
end
|
10
|
-
```
|
5
|
+
def initialize(params)
|
6
|
+
foo = params.fetch(:foo)
|
7
|
+
bar = params.fetch(:bar)
|
8
|
+
end
|
11
9
|
|
12
10
|
you can write:
|
13
11
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
12
|
+
def initialize(params)
|
13
|
+
foo, bar = params.grab(:foo, :bar)
|
14
|
+
end
|
15
|
+
|
16
|
+
or if some of your params are optional:
|
17
|
+
|
18
|
+
def initialize(params)
|
19
|
+
foo, bar = params.values(:foo, :bar)
|
20
|
+
end
|
19
21
|
|
20
22
|
## Installation
|
21
23
|
|
data/lib/grab.rb
CHANGED
@@ -1,11 +1,17 @@
|
|
1
1
|
require "grab/version"
|
2
2
|
|
3
|
-
|
3
|
+
class Hash
|
4
|
+
alias_method :old_values, :values
|
5
|
+
|
4
6
|
def grab(*keys)
|
5
7
|
keys.map { |k| self.fetch(k) }
|
6
8
|
end
|
7
|
-
end
|
8
9
|
|
9
|
-
|
10
|
-
|
10
|
+
def values(*args)
|
11
|
+
if args.empty?
|
12
|
+
old_values
|
13
|
+
else
|
14
|
+
args.map { |k| self[k] }
|
15
|
+
end
|
16
|
+
end
|
11
17
|
end
|
data/lib/grab/version.rb
CHANGED