fat 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.
- checksums.yaml +4 -4
- data/README.md +67 -0
- data/ext/fat/fat.c +4 -0
- data/fat.gemspec +1 -1
- data/test/fat_test.rb +17 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b8c0e66c3046460eab9b11b0722c51e9641b081c
|
4
|
+
data.tar.gz: b5855fc95174b566607dc8a0b17c374d3b1a7da6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3829b79d698b14644b50e8728bc03d596b856fe2e95836b00039975a8a9a908211c7062a4079400ce5508a73db698f8713d9702ada4a15e0c9cb8e7d9f984d78
|
7
|
+
data.tar.gz: 74f292a0719f7784e572477052d0810f9fe67f606b6d31f8401f372acc5eaacfabd2bddbfdbc1191017e377fbbee27af792e12b8488b7ebbd686f335460ab177
|
data/README.md
CHANGED
@@ -5,3 +5,70 @@ C extension to find values in nested hashes without pain
|
|
5
5
|
|
6
6
|
The name is an acronym for "find at". It helps you avoid that nasty `undefined method [] for nil` when looking for values in a hash.
|
7
7
|
|
8
|
+
# Use
|
9
|
+
|
10
|
+
Say you have the following hash
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
hash = {
|
14
|
+
"foo" => {
|
15
|
+
"bar" => {
|
16
|
+
"baz" => :value
|
17
|
+
}
|
18
|
+
}
|
19
|
+
}
|
20
|
+
```
|
21
|
+
|
22
|
+
To get your `:value` you usually do `hash["foo"]["bar"]["baz"]`. But what happens if `"bar"` doesn't exist? Yeap, BOOM!
|
23
|
+
|
24
|
+
I find more comfortable to ask if I can walk to `:value` using the keys `"foo"`, `"bar"`, `"baz"`. If I can't, give me some `nil`.
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
require "fat"
|
28
|
+
|
29
|
+
Fat.at(hash, "foo", "bar", "baz")
|
30
|
+
# => :value
|
31
|
+
|
32
|
+
Fat.at(hash, "foo", "not", "here")
|
33
|
+
# => nil
|
34
|
+
```
|
35
|
+
|
36
|
+
It's the same with Symbols
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
hash = {
|
40
|
+
"foo" => {
|
41
|
+
:bar => {
|
42
|
+
"baz" => :value
|
43
|
+
}
|
44
|
+
}
|
45
|
+
}
|
46
|
+
|
47
|
+
Fat.at(hash, "foo", :bar, "baz")
|
48
|
+
# => :value
|
49
|
+
```
|
50
|
+
|
51
|
+
If you prefer to call `hash.at` you only need to include `Fat` into `Hash`.
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
Hash.include(Fat)
|
55
|
+
|
56
|
+
hash.at("foo", "bar", "baz")
|
57
|
+
# => :value
|
58
|
+
```
|
59
|
+
|
60
|
+
And the last one! If all your keys are Strings you can *namespace* them with dots.
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
Hash.include(Fat)
|
64
|
+
|
65
|
+
hash.at("foo.bar.baz")
|
66
|
+
# => :value
|
67
|
+
```
|
68
|
+
|
69
|
+
# Install
|
70
|
+
|
71
|
+
```bash
|
72
|
+
$ gem install fat
|
73
|
+
```
|
74
|
+
|
data/ext/fat/fat.c
CHANGED
data/fat.gemspec
CHANGED
data/test/fat_test.rb
CHANGED
@@ -60,3 +60,20 @@ scope do
|
|
60
60
|
end
|
61
61
|
end
|
62
62
|
|
63
|
+
scope do
|
64
|
+
setup do
|
65
|
+
{
|
66
|
+
"foo" => {
|
67
|
+
"not_a_hash" => :wat,
|
68
|
+
"bar" => {
|
69
|
+
"baz" => :found
|
70
|
+
}
|
71
|
+
}
|
72
|
+
}
|
73
|
+
end
|
74
|
+
|
75
|
+
test "break if a key doesn't hold a hash" do |hash|
|
76
|
+
assert_equal :wat, Fat.at(hash, "foo.not_a_hash.baz")
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|