fat-rb 1.0.0 → 2.0.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.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -83
  3. data/fat-rb.gemspec +1 -1
  4. data/lib/fat.rb +14 -28
  5. data/test/fat_test.rb +31 -54
  6. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bdcab4d41d15436be2044afe9bfca50b7b0a5bc8
4
- data.tar.gz: a8511fdb9a72033f2177c3a5e02883f5f793f8b9
3
+ metadata.gz: 12f093f4b8df095cdd3b0b4eb0ba1a2acdcc81b5
4
+ data.tar.gz: ccfd4bd0e8c7903d67142d00ffa2446adc12542d
5
5
  SHA512:
6
- metadata.gz: 237740a7a6614dc056f41be23c46762f8aff6ceeb1ebcc1cb7373ef05ef02184d033821f43742ad8724dac581ca2080cbfe4beb91c84808714ef331b1c771005
7
- data.tar.gz: f08e8bb9f6ed4c6cbddd29208974a38927cc1055d8b7c70f7084d080142ddd16e8b14ed9f5db4b62ca359897984edd01fde3a37dbc781bfe65407d1171502d90
6
+ metadata.gz: cc9f4281017434079d02f9c42a332e0f83fef95f7e6f01e5fd13cba110d18dd2296e871d482ff32ad5e361be6947ae2405f936f8c3b93d16c49d9692e9747a7e
7
+ data.tar.gz: 20de6a4911dff4a50f9c727b4869057bffe77672ba7695c2b0b645d17c86f1befe8016ebd2c1ce7889dbb050ab1e234b62105e2c8cbf97de15d3d71c1a61f41f
data/README.md CHANGED
@@ -3,89 +3,7 @@ fat
3
3
 
4
4
  Ruby implementation for [Fat](https://github.com/tonchis/fat), to support non-MRI.
5
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-rb"
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
- You can also *namespace* the keys with dots `.` or colons `:` if they are all Strings or Symbols, respectively.
63
-
64
- ```ruby
65
- Hash.include(Fat)
66
-
67
- hash = {
68
- "foo" => {
69
- "bar" => {
70
- "baz" => :value
71
- }
72
- }
73
- }
74
-
75
- hash.at("foo.bar.baz")
76
- # => :value
77
-
78
- hash = {
79
- foo: {
80
- bar: {
81
- baz: :value
82
- }
83
- }
84
- }
85
-
86
- hash.at("foo:bar:baz")
87
- # => :value
88
- ```
6
+ Please look at the [README](https://github.com/tonchis/fat/blob/master/README.md) in that repository to learn about `Fat`.
89
7
 
90
8
  # Install
91
9
 
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "fat-rb"
3
- s.version = "1.0.0"
3
+ s.version = "2.0.0"
4
4
  s.summary = "Ruby implementation of the Fat gem"
5
5
  s.description = s.summary
6
6
  s.authors = ["Lucas Tolchinsky"]
data/lib/fat.rb CHANGED
@@ -1,39 +1,25 @@
1
1
  module Fat
2
2
  FatError = Class.new(StandardError)
3
3
 
4
- def self.at(hash, *args)
5
- fields = parse_args(*args)
6
-
7
- fields[0..-1].each_with_index do |field, index|
8
- hash = hash[field]
9
- if index != fields.length - 1 && !hash.kind_of?(Hash)
10
- raise Fat::FatError, "No hash found at #{fields[0..index].join(".")}"
4
+ def self.at(hash, *args, **keywords)
5
+ value = hash
6
+
7
+ args.each_with_index do |field, index|
8
+ value = value[field]
9
+ if value.nil?
10
+ if !keywords.empty?
11
+ return keywords[:default]
12
+ else
13
+ raise Fat::FatError, "#{args[0..index].join(".")} is nil"
14
+ end
11
15
  end
12
16
  end
13
17
 
14
- hash
15
- end
16
-
17
- def self.parse_args(*args)
18
- return args if args.length > 1
19
-
20
- fields = args.first.split(".")
21
-
22
- if fields.length == 1
23
- fields = args.first.split(":")
24
-
25
- if fields.length == 1
26
- raise FatError, "Single argument expected to be a namespace with dots (.) or colons (:)"
27
- else
28
- fields.map(&:to_sym)
29
- end
30
- else
31
- fields
32
- end
18
+ value
33
19
  end
34
20
 
35
- def at(*args)
36
- Fat.at(self, *args)
21
+ def at(*args, **keywords)
22
+ Fat.at(self, *args, keywords)
37
23
  end
38
24
  end
39
25
 
@@ -4,87 +4,60 @@ require_relative "../lib/fat"
4
4
  scope do
5
5
  setup do
6
6
  {
7
- foo: {
7
+ "foo" => {
8
8
  "bar" => {
9
- baz: :found
9
+ "baz" => :found
10
10
  }
11
11
  }
12
12
  }
13
13
  end
14
14
 
15
- test "honor key type" do |hash|
16
- exception = assert_raise(Fat::FatError) { Fat.at(hash, :foo, :bar, :found) }
17
- assert_equal "No hash found at foo.bar", exception.message
18
-
19
- assert_equal :found, Fat.at(hash, :foo, "bar", :baz)
15
+ test "find value" do |hash|
16
+ assert_equal :found, Fat.at(hash, "foo", "bar", "baz")
20
17
  end
21
- end
22
18
 
23
- scope do
24
- test "single argument must be a namespace" do
25
- exception = assert_raise(Fat::FatError) { Fat.at({"foo" => "bar"}, "foo") }
26
- assert_equal "Single argument expected to be a namespace with dots (.) or colons (:)", exception.message
27
- end
28
- end
19
+ test "don't find value" do |hash|
20
+ exception = assert_raise(Fat::FatError) { Fat.at(hash, "foo", "bar", "wat") }
21
+ assert_equal "foo.bar.wat is nil", exception.message
29
22
 
30
- scope do
31
- setup do
32
- {
33
- "foo" => {
34
- "bar" => {
35
- "baz" => :found
36
- }
37
- }
38
- }
23
+ exception = assert_raise(Fat::FatError) { Fat.at(hash, "foo", "wat", "baz") }
24
+ assert_equal "foo.wat is nil", exception.message
39
25
  end
40
26
 
41
- test "namespaced strings" do |hash|
42
- assert_equal :found, Fat.at(hash, "foo.bar.baz")
43
-
44
- exception = assert_raise(Fat::FatError) { Fat.at(hash, "foo.not.baz") }
45
- assert_equal "No hash found at foo.not", exception.message
27
+ test "return default value" do |hash|
28
+ assert_equal "default", Fat.at(hash, "foo", "wat", "baz", default: "default")
29
+ assert_equal nil, Fat.at(hash, "foo", "bar", "wat", default: nil)
46
30
  end
47
- end
48
31
 
49
- scope do
50
- setup do
51
- {
52
- foo: {
53
- bar: {
54
- baz: :found
55
- }
56
- }
57
- }
58
- end
32
+ test "include the module" do |hash|
33
+ Hash.include(Fat)
34
+
35
+ assert hash.respond_to?(:at)
36
+ assert_equal :found, hash.at("foo", "bar", "baz")
59
37
 
60
- test "namespaced symbols" do |hash|
61
- assert_equal :found, Fat.at(hash, "foo:bar:baz")
38
+ exception = assert_raise(Fat::FatError) { hash.at("foo", "wat", "baz") }
39
+ assert_equal "foo.wat is nil", exception.message
62
40
 
63
- exception = assert_raise(Fat::FatError) { Fat.at(hash, "foo:not:baz") }
64
- assert_equal "No hash found at foo.not", exception.message
41
+ assert_equal nil, hash.at("foo", "bar", "wat", default: nil)
65
42
  end
66
43
  end
67
44
 
68
45
  scope do
69
46
  setup do
70
- Hash.include(Fat)
71
-
72
47
  {
73
- "foo" => {
48
+ foo: {
74
49
  "bar" => {
75
- "baz" => :found
50
+ baz: :found
76
51
  }
77
52
  }
78
53
  }
79
54
  end
80
55
 
81
- test "include the module" do |hash|
82
- assert hash.respond_to?(:at)
83
- end
56
+ test "honor key type" do |hash|
57
+ exception = assert_raise(Fat::FatError) { Fat.at(hash, :foo, :bar, :found) }
58
+ assert_equal "foo.bar is nil", exception.message
84
59
 
85
- test "honor Fat interface" do |hash|
86
- assert_equal :found, hash.at("foo", "bar", "baz")
87
- assert_equal :found, hash.at("foo.bar.baz")
60
+ assert_equal :found, Fat.at(hash, :foo, "bar", :baz)
88
61
  end
89
62
  end
90
63
 
@@ -93,9 +66,13 @@ scope do
93
66
  Hash.include(Fat)
94
67
  end
95
68
 
96
- test "corner case" do
69
+ test "corner case: empty hashes" do
97
70
  assert_raise(Fat::FatError) { {}.at(:foo, :bar) }
98
71
  assert_raise(Fat::FatError) { Fat.at({}, :foo, :bar) }
99
72
  end
73
+
74
+ test "corner case: lookup a single key" do
75
+ assert_equal :found, {"foo" => :found}.at("foo")
76
+ end
100
77
  end
101
78
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fat-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lucas Tolchinsky
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-06 00:00:00.000000000 Z
11
+ date: 2015-12-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cutest