hash_dot 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2a6e045e5ade1404d4e85300e3ecc4455860e499
4
- data.tar.gz: c847967cc0e5063b6ac825ba3da135bdab9cccb8
3
+ metadata.gz: 93b55369f46856b6cf664ec018d705709370e07d
4
+ data.tar.gz: 70c5e0fca4d8e0646da79c68fae4dacaf1d97dea
5
5
  SHA512:
6
- metadata.gz: a73bf2ca4e4977e8e9c180df33fd2468a0863144653b9b5608bf26f7568ded483fb22ada7b04dbd2df435775b56bc796cd12be2cd8c3e4986039a94eb217b845
7
- data.tar.gz: 191181c73c3eda75736d3d5a8967f9ea78facea5144268a9a4abe6b3007700f48c5451965e6cdf53da4502991b8a176fbde607cd42c4a2e9a30f2262f9ce5ce0
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. Simply include the gem in your project and starting using the dot syntax.
3
+ HashDot allows you to call your Ruby hash properties with a dot syntax.
4
4
 
5
5
  ```ruby
6
- user = {name: 'Anna', job: {title: 'Programmer'}}
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](contributor-covenant.org) code of conduct.
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
@@ -31,4 +31,5 @@ Gem::Specification.new do |spec|
31
31
  spec.add_development_dependency "rake", "~> 10.0"
32
32
  spec.add_development_dependency "rspec"
33
33
  spec.add_development_dependency "json"
34
+ spec.add_development_dependency "pry"
34
35
  end
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 'json'
2
+ require 'symbol'
3
+ require 'hash'
3
4
 
4
- class Symbol
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
@@ -1,3 +1,3 @@
1
1
  module HashDot
2
- VERSION = "1.0.0"
2
+ VERSION = "2.0.0"
3
3
  end
data/lib/symbol.rb ADDED
@@ -0,0 +1,9 @@
1
+ class Symbol
2
+ def chop
3
+ to_s.chop.to_sym
4
+ end
5
+
6
+ def last
7
+ slice(-1, 1)
8
+ end
9
+ end
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: 1.0.0
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-06-14 00:00:00.000000000 Z
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.5
128
+ rubygems_version: 2.4.8
113
129
  signing_key:
114
130
  specification_version: 4
115
131
  summary: Use dot syntax with Ruby hashes.