hash_dot 1.0.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +61 -3
- data/hash_dot.gemspec +1 -0
- data/lib/hash.rb +49 -0
- data/lib/hash_dot.rb +3 -38
- data/lib/hash_dot/version.rb +1 -1
- data/lib/symbol.rb +9 -0
- metadata +19 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 93b55369f46856b6cf664ec018d705709370e07d
|
4
|
+
data.tar.gz: 70c5e0fca4d8e0646da79c68fae4dacaf1d97dea
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 85ab5a04cd496a674ac07c4b5c463317ea0a3068b01b22eccc9333ebf97178afd568234eda2d1b34a329ba7b85bfca42e1c35b2a512a177a17249ba36491883d
|
7
|
+
data.tar.gz: 64526817b4fc243431acc698ce4aabd60d43396f4c1e1041c75223ca4000762093f254a8126fec2f6d41c53e11fb8cc34419508b36662a1042817292015c03a3
|
data/README.md
CHANGED
@@ -1,13 +1,25 @@
|
|
1
1
|
# HashDot
|
2
2
|
|
3
|
-
HashDot allows you to call your Ruby hash properties with a dot syntax.
|
3
|
+
HashDot allows you to call your Ruby hash properties with a dot syntax.
|
4
4
|
|
5
5
|
```ruby
|
6
|
-
|
6
|
+
require 'hash_dot'
|
7
|
+
|
8
|
+
user = {name: 'Anna', job: {title: 'Programmer'}}.to_dot
|
7
9
|
|
8
10
|
user.job.title #=> 'Programmer'
|
9
11
|
user.job.title = 'Senior Programmer'
|
10
12
|
user.job.title #=> 'Senior Programmer'
|
13
|
+
user.job.delete(:title)
|
14
|
+
user.job.title #=> NoMethodError
|
15
|
+
```
|
16
|
+
|
17
|
+
You can also allow dot syntax for all hashes via the class setting.
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
Hash.use_dot_syntax = true
|
21
|
+
|
22
|
+
{name: 'Pat'}.name #=> 'Pat'
|
11
23
|
```
|
12
24
|
|
13
25
|
|
@@ -28,9 +40,55 @@ Or install it yourself as:
|
|
28
40
|
$ gem install hash_dot
|
29
41
|
|
30
42
|
|
43
|
+
## Benchmarks
|
44
|
+
Benchmarks should be taken with a grain of salt, as always. For example, OpenStruct is much slower to initialize, but calls its methods faster once initialized. The OpenStruct solution also can't traverse more than a single dot without recursively instantiating all sub-hashes into OpenStructs.
|
45
|
+
|
46
|
+
```Ruby
|
47
|
+
require 'ostruct'
|
48
|
+
require 'benchmark'
|
49
|
+
require 'hash_dot'
|
50
|
+
|
51
|
+
user = { address: { category: { desc: 'Urban'}}}.to_dot
|
52
|
+
|
53
|
+
iterations = 50000
|
54
|
+
|
55
|
+
Benchmark.bm(8) do |bm|
|
56
|
+
bm.report("Default Notation :") {
|
57
|
+
iterations.times do; user[:address][:category][:desc]; end
|
58
|
+
}
|
59
|
+
|
60
|
+
bm.report("Dot Notation :") {
|
61
|
+
iterations.times do; user.address.category.desc; end
|
62
|
+
}
|
63
|
+
|
64
|
+
bm.report("OpenStruct :") {
|
65
|
+
iterations.times do; OpenStruct.new(user); end
|
66
|
+
}
|
67
|
+
|
68
|
+
# Minus OpenStruct instantiation cost
|
69
|
+
os_user = OpenStruct.new(user)
|
70
|
+
bm.report("OpenStruct Single :") {
|
71
|
+
iterations.times do; os_user.address; end
|
72
|
+
}
|
73
|
+
|
74
|
+
bm.report("Dot Notation Single:") {
|
75
|
+
iterations.times do; user.address; end
|
76
|
+
}
|
77
|
+
end
|
78
|
+
|
79
|
+
# Benchmark Example
|
80
|
+
# user system total real
|
81
|
+
Default Notation : 0.010000 0.000000 0.010000 ( 0.008807)
|
82
|
+
Dot Notation : 0.190000 0.000000 0.190000 ( 0.195819)
|
83
|
+
OpenStruct : 0.400000 0.010000 0.410000 ( 0.399542)
|
84
|
+
OpenStruct Single : 0.010000 0.000000 0.010000 ( 0.011259)
|
85
|
+
Dot Notation Single: 0.080000 0.000000 0.080000 ( 0.082606)
|
86
|
+
```
|
87
|
+
|
88
|
+
|
31
89
|
## Contributing
|
32
90
|
|
33
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/adsteel/hash_dot. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](
|
91
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/adsteel/hash_dot. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](CODE_OF_CONDUCT.md) code of conduct.
|
34
92
|
|
35
93
|
|
36
94
|
## License
|
data/hash_dot.gemspec
CHANGED
data/lib/hash.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
class Hash
|
2
|
+
class << self
|
3
|
+
attr_accessor :use_dot_syntax
|
4
|
+
end
|
5
|
+
|
6
|
+
attr_accessor :use_dot_syntax
|
7
|
+
|
8
|
+
def to_dot
|
9
|
+
dotify_hash(self)
|
10
|
+
|
11
|
+
self
|
12
|
+
end
|
13
|
+
|
14
|
+
def method_missing(method, *args)
|
15
|
+
return super(method, *args) unless to_dot?
|
16
|
+
|
17
|
+
prop = create_prop(method)
|
18
|
+
|
19
|
+
if self[prop].nil?
|
20
|
+
self[prop] = self.delete(prop.to_s)
|
21
|
+
end
|
22
|
+
|
23
|
+
super(method, args) and return if self[prop].nil?
|
24
|
+
|
25
|
+
if setter?(method)
|
26
|
+
self[prop] = args.first
|
27
|
+
else
|
28
|
+
self[prop]
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
private def dotify_hash(hash)
|
33
|
+
hash.use_dot_syntax = true
|
34
|
+
|
35
|
+
hash.keys.each { |key| dotify_hash(hash[key]) if hash[key].is_a?(Hash) }
|
36
|
+
end
|
37
|
+
|
38
|
+
private def to_dot?
|
39
|
+
self.use_dot_syntax || self.class.use_dot_syntax
|
40
|
+
end
|
41
|
+
|
42
|
+
private def setter?(method)
|
43
|
+
method.last == "="
|
44
|
+
end
|
45
|
+
|
46
|
+
private def create_prop(method)
|
47
|
+
setter?(method) ? method.chop : method
|
48
|
+
end
|
49
|
+
end
|
data/lib/hash_dot.rb
CHANGED
@@ -1,41 +1,6 @@
|
|
1
1
|
require "hash_dot/version"
|
2
|
-
require '
|
2
|
+
require 'symbol'
|
3
|
+
require 'hash'
|
3
4
|
|
4
|
-
|
5
|
-
def chop
|
6
|
-
to_s.chop.to_sym
|
7
|
-
end
|
8
|
-
|
9
|
-
def last
|
10
|
-
slice(-1, 1)
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
class Hash
|
15
|
-
def method_missing(method, *args)
|
16
|
-
prop = create_prop(method)
|
17
|
-
|
18
|
-
if self[prop].nil?
|
19
|
-
self[prop] = self[prop.to_s]
|
20
|
-
self.delete(prop.to_s)
|
21
|
-
end
|
22
|
-
|
23
|
-
super(method, args) and return if self[prop].nil?
|
24
|
-
|
25
|
-
if setter?(method)
|
26
|
-
self[prop] = args.first
|
27
|
-
else
|
28
|
-
self[prop]
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
private
|
33
|
-
|
34
|
-
def setter?(method)
|
35
|
-
method.last == "="
|
36
|
-
end
|
37
|
-
|
38
|
-
def create_prop(method)
|
39
|
-
setter?(method) ? method.chop : method
|
40
|
-
end
|
5
|
+
module HashDot
|
41
6
|
end
|
data/lib/hash_dot/version.rb
CHANGED
data/lib/symbol.rb
ADDED
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:
|
4
|
+
version: 2.0.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: 2015-
|
11
|
+
date: 2015-10-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -66,6 +66,20 @@ dependencies:
|
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
69
83
|
description: HashDot allows you to call hash properties with a dot the same way you
|
70
84
|
would call, say, ActiveRecord relationships and attributes. More traversable and
|
71
85
|
often faster than OpenStruct.
|
@@ -86,8 +100,10 @@ files:
|
|
86
100
|
- bin/console
|
87
101
|
- bin/setup
|
88
102
|
- hash_dot.gemspec
|
103
|
+
- lib/hash.rb
|
89
104
|
- lib/hash_dot.rb
|
90
105
|
- lib/hash_dot/version.rb
|
106
|
+
- lib/symbol.rb
|
91
107
|
homepage: https://github.com/adsteel/hash_dot
|
92
108
|
licenses:
|
93
109
|
- MIT
|
@@ -109,7 +125,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
109
125
|
version: '0'
|
110
126
|
requirements: []
|
111
127
|
rubyforge_project:
|
112
|
-
rubygems_version: 2.4.
|
128
|
+
rubygems_version: 2.4.8
|
113
129
|
signing_key:
|
114
130
|
specification_version: 4
|
115
131
|
summary: Use dot syntax with Ruby hashes.
|