fat 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3a2a4f02825c7b90cc3693f53b6d54c0762e3065
4
- data.tar.gz: 6641e03bd1a109a5b65528da792e2c4e6df90406
3
+ metadata.gz: b8c0e66c3046460eab9b11b0722c51e9641b081c
4
+ data.tar.gz: b5855fc95174b566607dc8a0b17c374d3b1a7da6
5
5
  SHA512:
6
- metadata.gz: 76bffdd370590a7acad70d29dfdcc3c08b2c56cafc88439ce47c6aab4b7e090e5b46f3af4932021a087a9f88780e21d48a443e5bbf8c19488d0fb2d83f46d4e2
7
- data.tar.gz: 54b76753d47a02ee615d41a0f604b6fb0e4e6ad164c5b45565f8e2b815da434c3df1ebde86d6b2ee1848422cceb6cc8b624a493476652b379e087973ce809a9a
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
+
@@ -46,6 +46,10 @@ static VALUE fat(VALUE hash, VALUE fields) {
46
46
  if (value == Qnil) {
47
47
  return Qnil;
48
48
  }
49
+
50
+ if (TYPE(value) != T_HASH) {
51
+ return value;
52
+ }
49
53
  }
50
54
 
51
55
  return value;
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "fat"
3
- s.version = "0.0.1"
3
+ s.version = "0.0.2"
4
4
  s.summary = "C extension to find values in nested hashes without pain"
5
5
  s.description = s.summary
6
6
  s.authors = ["Lucas Tolchinsky"]
@@ -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
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lucas Tolchinsky