hash_dot 2.1.0 → 2.2.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 +4 -4
- data/README.md +16 -0
- data/hash_dot.gemspec +1 -1
- data/lib/hash.rb +14 -6
- data/lib/hash_dot/version.rb +1 -1
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e41159a2e0f3156c53e8c66f6b769ba59c4740e3
|
4
|
+
data.tar.gz: eb171a61ccfdbb299de59d2856eb0e8607226db2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dc9222cf1cd7d977883d98b3b4613a571468d3820aaecec553771cd903fbdd59ab68b582f512d00229d29caa51a8583502696ee73112b94691e0ee419ae4ca88
|
7
|
+
data.tar.gz: 530dbd4dcefa3610347bbaaee131d1e62d88802512477647af55e8116115e0ffb216a0708357f3cf4dcb99f87435f40a81ff4d1e238c566c0d673c4d7ebc06c3
|
data/README.md
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
[](https://travis-ci.org/adsteel/hash_dot)
|
2
|
+
|
1
3
|
# HashDot
|
2
4
|
|
3
5
|
HashDot allows you to get and set your Ruby hash properties with a dot syntax.
|
@@ -27,6 +29,20 @@ Hash.use_dot_syntax = true
|
|
27
29
|
{name: 'Pat'}.name #=> 'Pat'
|
28
30
|
```
|
29
31
|
|
32
|
+
By default HashDot raises a `NoMethodError` when accessing a non-existent key with the dot syntax. The value of `Hash#default` can be used instead, either globally or per-instance, via:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
# globally
|
36
|
+
Hash.use_dot_syntax = true
|
37
|
+
Hash.hash_dot_use_default = true
|
38
|
+
{}.a #=> 'default'
|
39
|
+
|
40
|
+
# per instance
|
41
|
+
h = Hash.new('default').to_dot(use_default: true)
|
42
|
+
h.a #=> 'default'
|
43
|
+
{}.to_dot.a #=> NoMethodError
|
44
|
+
```
|
45
|
+
|
30
46
|
|
31
47
|
## Installation
|
32
48
|
|
data/hash_dot.gemspec
CHANGED
@@ -32,5 +32,5 @@ Gem::Specification.new do |spec|
|
|
32
32
|
spec.add_development_dependency "rspec"
|
33
33
|
spec.add_development_dependency "json"
|
34
34
|
spec.add_development_dependency "pry"
|
35
|
-
spec.add_development_dependency "rubocop", "~> 0.
|
35
|
+
spec.add_development_dependency "rubocop", "~> 0.51"
|
36
36
|
end
|
data/lib/hash.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
1
|
class Hash
|
2
2
|
class << self
|
3
3
|
attr_accessor :use_dot_syntax
|
4
|
+
attr_accessor :hash_dot_use_default
|
4
5
|
end
|
5
6
|
|
6
7
|
attr_accessor :use_dot_syntax
|
8
|
+
attr_accessor :hash_dot_use_default
|
7
9
|
|
8
|
-
def to_dot
|
9
|
-
dotify_hash(self)
|
10
|
+
def to_dot(use_default: false)
|
11
|
+
dotify_hash(self, use_default: use_default)
|
10
12
|
|
11
13
|
self
|
12
14
|
end
|
@@ -18,9 +20,10 @@ class Hash
|
|
18
20
|
|
19
21
|
if setter?(method)
|
20
22
|
self[prop] = args.first
|
21
|
-
|
22
|
-
super(method, args) && return unless key?(prop)
|
23
|
+
elsif key?(prop) || use_default?
|
23
24
|
self[prop]
|
25
|
+
else
|
26
|
+
super(method, args)
|
24
27
|
end
|
25
28
|
end
|
26
29
|
|
@@ -33,16 +36,21 @@ class Hash
|
|
33
36
|
|
34
37
|
private
|
35
38
|
|
36
|
-
def dotify_hash(hash)
|
39
|
+
def dotify_hash(hash, use_default: false)
|
37
40
|
hash.use_dot_syntax = true
|
41
|
+
hash.hash_dot_use_default = use_default
|
38
42
|
|
39
|
-
hash.keys.each { |key| dotify_hash(hash[key]) if hash[key].is_a?(Hash) }
|
43
|
+
hash.keys.each { |key| dotify_hash(hash[key], use_default: use_default) if hash[key].is_a?(Hash) }
|
40
44
|
end
|
41
45
|
|
42
46
|
def to_dot?
|
43
47
|
use_dot_syntax || self.class.use_dot_syntax
|
44
48
|
end
|
45
49
|
|
50
|
+
def use_default?
|
51
|
+
hash_dot_use_default || self.class.hash_dot_use_default
|
52
|
+
end
|
53
|
+
|
46
54
|
def setter?(method)
|
47
55
|
method.last == "="
|
48
56
|
end
|
data/lib/hash_dot/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash_dot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Steel
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-11-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -86,14 +86,14 @@ dependencies:
|
|
86
86
|
requirements:
|
87
87
|
- - "~>"
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version: 0.
|
89
|
+
version: '0.51'
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
94
|
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version: 0.
|
96
|
+
version: '0.51'
|
97
97
|
description: HashDot allows you to call hash properties with a dot the same way you
|
98
98
|
would call, say, ActiveRecord relationships and attributes. More traversable and
|
99
99
|
often faster than OpenStruct.
|
@@ -140,7 +140,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
140
140
|
version: '0'
|
141
141
|
requirements: []
|
142
142
|
rubyforge_project:
|
143
|
-
rubygems_version: 2.
|
143
|
+
rubygems_version: 2.5.2
|
144
144
|
signing_key:
|
145
145
|
specification_version: 4
|
146
146
|
summary: Use dot syntax with Ruby hashes.
|