better_dig 0.5.1 → 0.6.0

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
  SHA256:
3
- metadata.gz: 00dd287b295165d8dcc2933d1038b76e64590499b6a1a96e96dfa2767be643f8
4
- data.tar.gz: 85ed605bb5d67cda01c4dbcf978c2c4c017298ae66532d82882eb45b41b44a19
3
+ metadata.gz: 61055566fc7407f29bc2dfd61e80f1a43ec818726fd8b9fb8143519dbbe720fc
4
+ data.tar.gz: d1ba361fd12f903bc63f24d02e036a18ba4a53d2cef7f987bcaa769eae37f212
5
5
  SHA512:
6
- metadata.gz: 0e5d1080dd414c4108b020c037badedfa4b9e306e37be5637115554f5b444ee710fb974c46ea136c18b538609efb69c6a34469ee3282225db005479397fc7e18
7
- data.tar.gz: 8107bc66ee32e362d59ac7d711b9ea67e576e412c80c4e1bae3079d412d411de10cb7b71a28e5ec006e5299f788d217bf24664e2f8178ca1d78d414da6aaba55
6
+ metadata.gz: 58a3fa77509621a49e857e4928871ad7c4eb0203cebdd0dd9c5f9c3e91529b0d8dd12a025683d3ef54f2a3cd002f0e9ca7d75f3a6e2ce8a543a7982afe7739dd
7
+ data.tar.gz: 9f959be9d845dafd81aa7d31f86c741873fb8cce520dcd9f97a65a19e420781a947c677fe9b2b984bbed25d96d16568222b6c92071df823c6e1b52d01b386b1c
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- better_dig (0.5.1)
4
+ better_dig (0.6.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  [![Gem Version](https://badge.fury.io/rb/better_dig.svg)](https://badge.fury.io/rb/better_dig)
2
+ [![Downloads](https://img.shields.io/gem/dt/better_dig.svg)](https://rubygems.org/gems/better_dig)
2
3
  [![Maintainability](https://api.codeclimate.com/v1/badges/6bc69601493caa67e52e/maintainability)](https://codeclimate.com/github/berniechiu/better_dig/maintainability)
3
4
  [![Test Coverage](https://api.codeclimate.com/v1/badges/6bc69601493caa67e52e/test_coverage)](https://codeclimate.com/github/berniechiu/better_dig/test_coverage)
4
5
  [![Build Status](https://travis-ci.org/berniechiu/better_dig.svg?branch=master)](https://travis-ci.org/berniechiu/better_dig)
@@ -32,24 +33,43 @@ Or install it yourself as:
32
33
 
33
34
  ## Usage
34
35
 
36
+ Basic Examples
37
+
35
38
  ```ruby
36
39
  hash = { hello: 'world', nested: { hello: 'world' }, nested_array: ['hello', 'world'] }
37
40
 
38
- hash.digg(:hello) # => 'world'
39
- hash.digg('hello') # => 'world'
40
- hash.digg('nested', 'hello') # => 'world'
41
- hash.digg('nested_array', 0) # => 'hello'
41
+ # Fetch by digg
42
+ hash.digg(:hello) #=> 'world'
43
+ hash.digg('hello') #=> 'world'
44
+ hash.digg('nested', 'hello') #=> 'world'
45
+ hash.digg('nested_array', 0) #=> 'hello'
46
+ hash.digg('nested_array', '0') #=> 'hello'
47
+ hash.digg(:hello, 0, :not_found) #=> nil
48
+
49
+ # Fetch by path
50
+ hash.fetch_path('nested/hello') #=> 'world'
51
+ hash.fetch_path('nested_array/0') #=> 'hello'
52
+ hash.fetch_path('nested.hello', delimeter: '.') #=> 'hello'
53
+ hash.fetch_path('nested/hello/none', default: 'zh-tw.not_found') # => 'zh-tw.not_found'
42
54
 
43
- # Since `nil` will be returned for not found value instead of exception,
44
- # we can use conditional block now without breaking the application
55
+ # Since `nil` will be returned for not found value instead of TypeError if we are digging too far
56
+ # we can use conditional block without breaking the application
45
57
 
46
58
  if hash.digg(:nested, :hello, :world) # => nil
47
- # DO SOMETHING
59
+ # DO SOMETHING WHEN FOUND
48
60
  else
49
- # DO SOMETHING
61
+ # DO SOMETHING WHEN NOT FOUND
50
62
  end
51
63
  ```
52
64
 
65
+ Or probably in the Rails views
66
+
67
+ ```erb
68
+ <%= object.hash_data.digg(:snapshot, :price) %>
69
+
70
+ ```
71
+
72
+
53
73
  ## Contributing
54
74
 
55
75
  Bug reports and pull requests are welcome on GitHub at https://github.com/berniechiu/better_dig.
data/lib/better_dig.rb CHANGED
@@ -12,6 +12,10 @@ module BetterDig
12
12
  nil
13
13
  end
14
14
 
15
+ def fetch_path(path, default: nil, delimeter: '/')
16
+ digg(*path.split(delimeter)) || default
17
+ end
18
+
15
19
  private
16
20
 
17
21
  def match_array?(key)
@@ -1,3 +1,3 @@
1
1
  module BetterDig
2
- VERSION = "0.5.1"
2
+ VERSION = "0.6.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: better_dig
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bernie Chiu
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-03-03 00:00:00.000000000 Z
11
+ date: 2018-03-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler