fat-rb 0.1.1 → 1.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 +20 -1
  3. data/fat-rb.gemspec +2 -2
  4. data/lib/fat.rb +22 -6
  5. data/test/fat_test.rb +29 -18
  6. metadata +5 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6fd16c3522392fab0e6d2655da869ec3ba5682af
4
- data.tar.gz: 886a2032fd52b09f37ef802d2183f2521035d503
3
+ metadata.gz: bdcab4d41d15436be2044afe9bfca50b7b0a5bc8
4
+ data.tar.gz: a8511fdb9a72033f2177c3a5e02883f5f793f8b9
5
5
  SHA512:
6
- metadata.gz: 63678b362e56b8e5f20f6b55e75e0eca54decb6269890c4ff6240f9bcc6a4cc6da859b28a8448d7909ce3a700e520368007b973665dba3a5e47c6a6cd72e235e
7
- data.tar.gz: 8d5ea85dd5187834070cdf5722fc32f3c43f9abdf4c48f0587e94dcba321a6812be65a37dc99ab5a9b14b85d00899e696721b6457dcd484bc4e6268c8f2221d3
6
+ metadata.gz: 237740a7a6614dc056f41be23c46762f8aff6ceeb1ebcc1cb7373ef05ef02184d033821f43742ad8724dac581ca2080cbfe4beb91c84808714ef331b1c771005
7
+ data.tar.gz: f08e8bb9f6ed4c6cbddd29208974a38927cc1055d8b7c70f7084d080142ddd16e8b14ed9f5db4b62ca359897984edd01fde3a37dbc781bfe65407d1171502d90
data/README.md CHANGED
@@ -59,13 +59,32 @@ hash.at("foo", "bar", "baz")
59
59
  # => :value
60
60
  ```
61
61
 
62
- If all your keys are Strings you can *namespace* them with dots.
62
+ You can also *namespace* the keys with dots `.` or colons `:` if they are all Strings or Symbols, respectively.
63
63
 
64
64
  ```ruby
65
65
  Hash.include(Fat)
66
66
 
67
+ hash = {
68
+ "foo" => {
69
+ "bar" => {
70
+ "baz" => :value
71
+ }
72
+ }
73
+ }
74
+
67
75
  hash.at("foo.bar.baz")
68
76
  # => :value
77
+
78
+ hash = {
79
+ foo: {
80
+ bar: {
81
+ baz: :value
82
+ }
83
+ }
84
+ }
85
+
86
+ hash.at("foo:bar:baz")
87
+ # => :value
69
88
  ```
70
89
 
71
90
  # Install
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "fat-rb"
3
- s.version = "0.1.1"
3
+ s.version = "1.0.0"
4
4
  s.summary = "Ruby implementation of the Fat gem"
5
5
  s.description = s.summary
6
6
  s.authors = ["Lucas Tolchinsky"]
@@ -10,6 +10,6 @@ Gem::Specification.new do |s|
10
10
 
11
11
  s.files = `git ls-files`.split("\n")
12
12
 
13
- s.add_development_dependency "cutest"
13
+ s.add_development_dependency "cutest", ">= 1.2.2"
14
14
  end
15
15
 
data/lib/fat.rb CHANGED
@@ -2,18 +2,34 @@ module Fat
2
2
  FatError = Class.new(StandardError)
3
3
 
4
4
  def self.at(hash, *args)
5
- fields = args.length == 1 ? args.first.split(".") : args
6
-
7
- value = hash
5
+ fields = parse_args(*args)
8
6
 
9
7
  fields[0..-1].each_with_index do |field, index|
10
- value = value[field]
11
- if index != fields.length - 1 && !value.kind_of?(Hash)
8
+ hash = hash[field]
9
+ if index != fields.length - 1 && !hash.kind_of?(Hash)
12
10
  raise Fat::FatError, "No hash found at #{fields[0..index].join(".")}"
13
11
  end
14
12
  end
15
13
 
16
- value
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
17
33
  end
18
34
 
19
35
  def at(*args)
@@ -13,12 +13,20 @@ scope do
13
13
  end
14
14
 
15
15
  test "honor key type" do |hash|
16
- assert_raise(Fat::FatError) { Fat.at(hash, :foo, :not, :found) }
16
+ exception = assert_raise(Fat::FatError) { Fat.at(hash, :foo, :bar, :found) }
17
+ assert_equal "No hash found at foo.bar", exception.message
17
18
 
18
19
  assert_equal :found, Fat.at(hash, :foo, "bar", :baz)
19
20
  end
20
21
  end
21
22
 
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
29
+
22
30
  scope do
23
31
  setup do
24
32
  {
@@ -30,41 +38,39 @@ scope do
30
38
  }
31
39
  end
32
40
 
33
- test "namespaced string keys" do |hash|
34
- assert_raise(Fat::FatError) { Fat.at(hash, "foo", :not, :found) }
35
-
41
+ test "namespaced strings" do |hash|
36
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
37
46
  end
38
47
  end
39
48
 
40
49
  scope do
41
50
  setup do
42
- Hash.include(Fat)
43
-
44
51
  {
45
- "foo" => {
46
- "bar" => {
47
- "baz" => :found
52
+ foo: {
53
+ bar: {
54
+ baz: :found
48
55
  }
49
56
  }
50
57
  }
51
58
  end
52
59
 
53
- test "include the module" do |hash|
54
- assert hash.respond_to?(:at)
55
- end
60
+ test "namespaced symbols" do |hash|
61
+ assert_equal :found, Fat.at(hash, "foo:bar:baz")
56
62
 
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")
63
+ exception = assert_raise(Fat::FatError) { Fat.at(hash, "foo:not:baz") }
64
+ assert_equal "No hash found at foo.not", exception.message
60
65
  end
61
66
  end
62
67
 
63
68
  scope do
64
69
  setup do
70
+ Hash.include(Fat)
71
+
65
72
  {
66
73
  "foo" => {
67
- "not_a_hash" => :wat,
68
74
  "bar" => {
69
75
  "baz" => :found
70
76
  }
@@ -72,8 +78,13 @@ scope do
72
78
  }
73
79
  end
74
80
 
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") }
81
+ test "include the module" do |hash|
82
+ assert hash.respond_to?(:at)
83
+ end
84
+
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")
77
88
  end
78
89
  end
79
90
 
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: 0.1.1
4
+ version: 1.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-10-31 00:00:00.000000000 Z
11
+ date: 2014-11-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cutest
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: 1.2.2
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: 1.2.2
27
27
  description: Ruby implementation of the Fat gem
28
28
  email:
29
29
  - lucas.tolchinsky@gmail.com
@@ -58,7 +58,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
58
58
  version: '0'
59
59
  requirements: []
60
60
  rubyforge_project:
61
- rubygems_version: 2.2.0
61
+ rubygems_version: 2.2.2
62
62
  signing_key:
63
63
  specification_version: 4
64
64
  summary: Ruby implementation of the Fat gem