fat-rb 0.1.0
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 +7 -0
- data/.gems +1 -0
- data/LICENSE +22 -0
- data/Makefile +4 -0
- data/README.md +76 -0
- data/fat-rb.gemspec +15 -0
- data/lib/fat.rb +27 -0
- data/test/fat_test.rb +90 -0
- metadata +65 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4a5650524ecb53949a2c471268e675261833e67a
|
4
|
+
data.tar.gz: 01084a031199c786468516f8387614e632e3c53b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 59f8f95c7cb0a473bf9b1eb48fb482d3a3975ab113bcbea0e3777b380387c312d936bcdd5a378deaa0be974d3b3612e9d9f7c3888f3b51848a826af8950a48ed
|
7
|
+
data.tar.gz: 95af9ae4043f032d976781a55ae1428d0c4895328fe1b4ac69c424d2cda337eb7f71550cfeb00a05a6296fe4a5290913f1f968e3711ff6ba315f7c7ee17c1191
|
data/.gems
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
cutest -v 1.2.1
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2014 Lucas Tolchinsky
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
22
|
+
|
data/Makefile
ADDED
data/README.md
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
fat
|
2
|
+
===
|
3
|
+
|
4
|
+
Ruby implementation for [Fat](https://github.com/tonchis/fat), to support non-MRI.
|
5
|
+
|
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
|
+
|
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, tell me where the path ends.
|
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
|
+
# => Fat::FatError: No hash found at foo.not
|
34
|
+
```
|
35
|
+
|
36
|
+
The `Fat::FatError` let's you know that a `Hash` was expected as a value for the key `"not"`. Way more explicit than guessing from a `undefined method [] for nil`.
|
37
|
+
|
38
|
+
It's the same with Symbols
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
hash = {
|
42
|
+
"foo" => {
|
43
|
+
:bar => {
|
44
|
+
"baz" => :value
|
45
|
+
}
|
46
|
+
}
|
47
|
+
}
|
48
|
+
|
49
|
+
Fat.at(hash, "foo", :bar, "baz")
|
50
|
+
# => :value
|
51
|
+
```
|
52
|
+
|
53
|
+
If you prefer to call `hash.at` you only need to include `Fat` into `Hash`.
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
Hash.include(Fat)
|
57
|
+
|
58
|
+
hash.at("foo", "bar", "baz")
|
59
|
+
# => :value
|
60
|
+
```
|
61
|
+
|
62
|
+
If all your keys are Strings you can *namespace* them with dots.
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
Hash.include(Fat)
|
66
|
+
|
67
|
+
hash.at("foo.bar.baz")
|
68
|
+
# => :value
|
69
|
+
```
|
70
|
+
|
71
|
+
# Install
|
72
|
+
|
73
|
+
```bash
|
74
|
+
$ gem install fat-rb
|
75
|
+
```
|
76
|
+
|
data/fat-rb.gemspec
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "fat-rb"
|
3
|
+
s.version = "0.1.0"
|
4
|
+
s.summary = "Ruby implementation of the Fat gem"
|
5
|
+
s.description = s.summary
|
6
|
+
s.authors = ["Lucas Tolchinsky"]
|
7
|
+
s.email = ["lucas.tolchinsky@gmail.com"]
|
8
|
+
s.homepage = "https://github.com/tonchis/fat-rb"
|
9
|
+
s.license = "MIT"
|
10
|
+
|
11
|
+
s.files = `git ls-files`.split("\n")
|
12
|
+
|
13
|
+
s.add_development_dependency "cutest"
|
14
|
+
end
|
15
|
+
|
data/lib/fat.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
module Fat
|
2
|
+
FatError = Class.new(StandardError)
|
3
|
+
|
4
|
+
@fat ||= ->(hash, *args) do
|
5
|
+
fields = args.length == 1 ? args.first.split(".") : args
|
6
|
+
|
7
|
+
value = hash
|
8
|
+
|
9
|
+
fields[0..-1].each_with_index do |field, index|
|
10
|
+
value = value[field]
|
11
|
+
if index != fields.length - 1 && !value.kind_of?(Hash)
|
12
|
+
raise Fat::FatError, "No hash found at #{fields[0..index].join(".")}"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
value
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.at(hash, *args)
|
20
|
+
@fat.(hash, *args)
|
21
|
+
end
|
22
|
+
|
23
|
+
def at(*args)
|
24
|
+
Fat.instance_variable_get(:@fat).(self, *args)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
data/test/fat_test.rb
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
require "cutest"
|
2
|
+
require_relative "../lib/fat"
|
3
|
+
|
4
|
+
scope do
|
5
|
+
setup do
|
6
|
+
{
|
7
|
+
foo: {
|
8
|
+
"bar" => {
|
9
|
+
baz: :found
|
10
|
+
}
|
11
|
+
}
|
12
|
+
}
|
13
|
+
end
|
14
|
+
|
15
|
+
test "honor key type" do |hash|
|
16
|
+
assert_raise(Fat::FatError) { Fat.at(hash, :foo, :not, :found) }
|
17
|
+
|
18
|
+
assert_equal :found, Fat.at(hash, :foo, "bar", :baz)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
scope do
|
23
|
+
setup do
|
24
|
+
{
|
25
|
+
"foo" => {
|
26
|
+
"bar" => {
|
27
|
+
"baz" => :found
|
28
|
+
}
|
29
|
+
}
|
30
|
+
}
|
31
|
+
end
|
32
|
+
|
33
|
+
test "namespaced string keys" do |hash|
|
34
|
+
assert_raise(Fat::FatError) { Fat.at(hash, "foo", :not, :found) }
|
35
|
+
|
36
|
+
assert_equal :found, Fat.at(hash, "foo.bar.baz")
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
scope do
|
41
|
+
setup do
|
42
|
+
Hash.include(Fat)
|
43
|
+
|
44
|
+
{
|
45
|
+
"foo" => {
|
46
|
+
"bar" => {
|
47
|
+
"baz" => :found
|
48
|
+
}
|
49
|
+
}
|
50
|
+
}
|
51
|
+
end
|
52
|
+
|
53
|
+
test "include the module" do |hash|
|
54
|
+
assert hash.respond_to?(:at)
|
55
|
+
end
|
56
|
+
|
57
|
+
test "honor Fat interface" do |hash|
|
58
|
+
assert_equal :found, hash.at("foo", "bar", "baz")
|
59
|
+
assert_equal :found, hash.at("foo.bar.baz")
|
60
|
+
end
|
61
|
+
end
|
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 "raise error if a value is not a hash" do |hash|
|
76
|
+
assert_raise(Fat::FatError) { Fat.at(hash, "foo.not_a_hash.baz") }
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
scope do
|
81
|
+
setup do
|
82
|
+
Hash.include(Fat)
|
83
|
+
end
|
84
|
+
|
85
|
+
test "corner case" do
|
86
|
+
assert_raise(Fat::FatError) { {}.at(:foo, :bar) }
|
87
|
+
assert_raise(Fat::FatError) { Fat.at({}, :foo, :bar) }
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fat-rb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Lucas Tolchinsky
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-10-31 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: cutest
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: Ruby implementation of the Fat gem
|
28
|
+
email:
|
29
|
+
- lucas.tolchinsky@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".gems"
|
35
|
+
- LICENSE
|
36
|
+
- Makefile
|
37
|
+
- README.md
|
38
|
+
- fat-rb.gemspec
|
39
|
+
- lib/fat.rb
|
40
|
+
- test/fat_test.rb
|
41
|
+
homepage: https://github.com/tonchis/fat-rb
|
42
|
+
licenses:
|
43
|
+
- MIT
|
44
|
+
metadata: {}
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options: []
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
requirements: []
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 2.2.0
|
62
|
+
signing_key:
|
63
|
+
specification_version: 4
|
64
|
+
summary: Ruby implementation of the Fat gem
|
65
|
+
test_files: []
|