dotted_hash 0.9 → 1.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/.gitignore +1 -0
- data/.travis.yml +6 -0
- data/Gemfile +5 -0
- data/MIT-LICENSE +3 -1
- data/README.md +2 -1
- data/Rakefile +11 -1
- data/dotted_hash.gemspec +4 -3
- data/lib/dotted_hash.rb +46 -15
- data/lib/dotted_hash/version.rb +1 -1
- data/test/dotted_hash_spec.rb +7 -10
- metadata +26 -11
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 3dccd5c932fd7197a40555f0ab7d4337a3a9018c
|
|
4
|
+
data.tar.gz: f3cb641cf1f2e2a3b845f781a7051aaab53e3293
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 931abd36ada5a91c9767a20d9a7b2535432364e6a5fdc30a9c3a98a5a5990f67bc7eb0f8889ae4673f5694fa240fce30df86593eaca75e2ec360b2cb855721b6
|
|
7
|
+
data.tar.gz: c2c0f6756869edd015074ffd0ad3aae5a9543e72ac16c72fb16887d9093c8dec4582c2ca6765630842d2895c97cf3f300c5184f697bba609c6635463570ae329
|
data/.gitignore
CHANGED
data/.travis.yml
ADDED
data/Gemfile
CHANGED
data/MIT-LICENSE
CHANGED
data/README.md
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
[](https://travis-ci.org/istana/dotted_hash)
|
|
2
|
+
|
|
1
3
|
# DottedHash
|
|
2
4
|
|
|
3
5
|
Recursive OpenStruct-like or Hash-like object. Uses ActiveModel.
|
|
@@ -40,7 +42,6 @@ Or install it yourself as:
|
|
|
40
42
|
if key has spaces
|
|
41
43
|
|
|
42
44
|
> document.quotes['Duke Nukem Forever']
|
|
43
|
-
> document.quotes['Duke Nukem Forever']
|
|
44
45
|
|
|
45
46
|
nested
|
|
46
47
|
|
data/Rakefile
CHANGED
|
@@ -17,4 +17,14 @@ task :test do
|
|
|
17
17
|
puts unit
|
|
18
18
|
system("rspec #{unit}")
|
|
19
19
|
end
|
|
20
|
-
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
task :default => [:test]
|
|
23
|
+
|
|
24
|
+
require 'rdoc/task'
|
|
25
|
+
Rake::RDocTask.new do |rd|
|
|
26
|
+
rd.main = "README.md"
|
|
27
|
+
rd.rdoc_files.include("README.md", "lib/*.rb")
|
|
28
|
+
rd.options << "--all"
|
|
29
|
+
rd.title = "Enjoy DottedHash!"
|
|
30
|
+
end
|
data/dotted_hash.gemspec
CHANGED
|
@@ -21,8 +21,8 @@ Gem::Specification.new do |spec|
|
|
|
21
21
|
spec.extra_rdoc_files = [ "README.md", "MIT-LICENSE" ]
|
|
22
22
|
spec.rdoc_options = [ "--charset=UTF-8" ]
|
|
23
23
|
|
|
24
|
-
spec.add_dependency "activemodel", "
|
|
25
|
-
spec.add_dependency "activesupport"
|
|
24
|
+
spec.add_dependency "activemodel", "~> 4.0"
|
|
25
|
+
spec.add_dependency "activesupport", "~> 4.0"
|
|
26
26
|
|
|
27
27
|
unless defined?(JRUBY_VERSION)
|
|
28
28
|
spec.add_development_dependency "turn"
|
|
@@ -30,5 +30,6 @@ Gem::Specification.new do |spec|
|
|
|
30
30
|
|
|
31
31
|
spec.add_development_dependency "bundler", "~> 1.3"
|
|
32
32
|
spec.add_development_dependency "rake"
|
|
33
|
-
|
|
33
|
+
spec.add_development_dependency "rspec", "~> 2.13"
|
|
34
|
+
spec.add_development_dependency "rdoc", "~> 4.0"
|
|
34
35
|
end
|
data/lib/dotted_hash.rb
CHANGED
|
@@ -5,6 +5,7 @@ require 'active_model'
|
|
|
5
5
|
require 'active_support/core_ext/string/inflections'
|
|
6
6
|
require 'active_support/json'
|
|
7
7
|
|
|
8
|
+
# See Readme.md in gem/repository directory for usage instructions
|
|
8
9
|
class DottedHash
|
|
9
10
|
extend ActiveModel::Naming
|
|
10
11
|
include ActiveModel::Conversion
|
|
@@ -12,22 +13,22 @@ class DottedHash
|
|
|
12
13
|
|
|
13
14
|
## Basic security
|
|
14
15
|
|
|
15
|
-
# Maximum depth of whole tree, not keys (keys depth+1)
|
|
16
|
-
# Counted from 0
|
|
17
|
-
# Not fully bulletproof, depth may be set to wrong number if careless
|
|
16
|
+
# Maximum depth of whole tree, not keys (keys depth+1).
|
|
17
|
+
# Counted from 0.
|
|
18
|
+
# Not fully bulletproof, depth may be set to wrong number if careless.
|
|
18
19
|
MAX_DEPTH = 10
|
|
19
20
|
|
|
20
|
-
# Maximum count of attributes
|
|
21
|
-
# Use hash like this to specify each level
|
|
21
|
+
# Maximum count of attributes.
|
|
22
|
+
# Use hash like this to specify each level.
|
|
22
23
|
# MAX_ATTRS = {1 => 20, 2 => 5, default: 10}
|
|
23
24
|
MAX_ATTRS = 10
|
|
24
25
|
|
|
25
|
-
# Maximum size of document, counted from JSON result of document
|
|
26
|
-
# It is not bulletproof, but if using simple structures, it is enough
|
|
27
|
-
# Other structures may have much bigger representation in memory than in JSON
|
|
26
|
+
# Maximum size of document, counted from JSON result of document.
|
|
27
|
+
# It is not bulletproof, but if using simple structures, it is enough.
|
|
28
|
+
# Other structures may have much bigger representation in memory than in JSON.
|
|
28
29
|
MAX_SIZE = 16384
|
|
29
30
|
|
|
30
|
-
# Create new instance, recursively converting all Hashes to
|
|
31
|
+
# Create new instance, recursively converting all Hashes to DottedHash
|
|
31
32
|
# and leaving everything else alone.
|
|
32
33
|
#
|
|
33
34
|
def initialize(args={}, level=0)
|
|
@@ -40,6 +41,22 @@ class DottedHash
|
|
|
40
41
|
assign_value(key, value)
|
|
41
42
|
end
|
|
42
43
|
end
|
|
44
|
+
|
|
45
|
+
# Merge with another hash
|
|
46
|
+
def merge!(obj)
|
|
47
|
+
if obj.respond_to? :to_h
|
|
48
|
+
hash = obj.to_h
|
|
49
|
+
elsif obj.respond_to? :to_hash
|
|
50
|
+
hash = obj.to_hash
|
|
51
|
+
else
|
|
52
|
+
raise('Merge works only with hashlike object')
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
hash.each do |key, value|
|
|
56
|
+
assign_value(key, value)
|
|
57
|
+
end
|
|
58
|
+
self
|
|
59
|
+
end
|
|
43
60
|
|
|
44
61
|
def assign_value(key, value)
|
|
45
62
|
max_attrs = if MAX_ATTRS.is_a?(Fixnum)
|
|
@@ -76,8 +93,11 @@ class DottedHash
|
|
|
76
93
|
end
|
|
77
94
|
end
|
|
78
95
|
|
|
96
|
+
# Always respond to write.
|
|
97
|
+
# Respond to attribute or defined method.
|
|
98
|
+
#
|
|
79
99
|
def respond_to?(method_name, include_private = false)
|
|
80
|
-
#
|
|
100
|
+
# answers to any write method
|
|
81
101
|
if method_name.to_s[-1] == '='
|
|
82
102
|
true
|
|
83
103
|
else
|
|
@@ -85,6 +105,9 @@ class DottedHash
|
|
|
85
105
|
end
|
|
86
106
|
end
|
|
87
107
|
|
|
108
|
+
# Recursively assigns value.
|
|
109
|
+
# Also creates sub-DottedHashes if they don't exist).
|
|
110
|
+
#
|
|
88
111
|
def recursive_assign(key, value)
|
|
89
112
|
return nil if key.blank?
|
|
90
113
|
keys = key.split('.')
|
|
@@ -101,30 +124,34 @@ class DottedHash
|
|
|
101
124
|
end
|
|
102
125
|
end
|
|
103
126
|
|
|
127
|
+
# Provides access to attribute.
|
|
128
|
+
# Use when you have spaces and other non +a-z_+ characters in attribute name.
|
|
129
|
+
#
|
|
104
130
|
def [](key)
|
|
105
131
|
@attributes[key.to_sym]
|
|
106
132
|
end
|
|
107
133
|
|
|
134
|
+
# Returns +id+ of document
|
|
135
|
+
#
|
|
108
136
|
def id
|
|
109
|
-
@attributes[:
|
|
110
|
-
end
|
|
111
|
-
|
|
112
|
-
def type
|
|
113
|
-
@attributes[:_type] || @attributes[:type]
|
|
137
|
+
@attributes[:id]
|
|
114
138
|
end
|
|
115
139
|
|
|
116
140
|
def persisted?
|
|
117
141
|
!!id
|
|
118
142
|
end
|
|
119
143
|
|
|
144
|
+
# Standard ActiveModel Errors
|
|
120
145
|
def errors
|
|
121
146
|
ActiveModel::Errors.new(self)
|
|
122
147
|
end
|
|
123
148
|
|
|
149
|
+
# Always +true+
|
|
124
150
|
def valid?
|
|
125
151
|
true
|
|
126
152
|
end
|
|
127
153
|
|
|
154
|
+
# Returns key if key exists
|
|
128
155
|
def to_key
|
|
129
156
|
persisted? ? [id] : nil
|
|
130
157
|
end
|
|
@@ -138,11 +165,15 @@ class DottedHash
|
|
|
138
165
|
|
|
139
166
|
alias_method :to_h, :to_hash
|
|
140
167
|
|
|
168
|
+
# Returns (filtered) Ruby +Hash+ with characters
|
|
169
|
+
# and objects only allowed in JSON.
|
|
170
|
+
#
|
|
141
171
|
def as_json(options=nil)
|
|
142
172
|
hash = to_hash
|
|
143
173
|
hash.respond_to?(:with_indifferent_access) ? hash.with_indifferent_access.as_json(options) : hash.as_json(options)
|
|
144
174
|
end
|
|
145
175
|
|
|
176
|
+
# JSON string of +as_json+ result
|
|
146
177
|
def to_json(options=nil)
|
|
147
178
|
as_json.to_json(options)
|
|
148
179
|
end
|
data/lib/dotted_hash/version.rb
CHANGED
data/test/dotted_hash_spec.rb
CHANGED
|
@@ -25,17 +25,8 @@ describe DottedHash do
|
|
|
25
25
|
end
|
|
26
26
|
|
|
27
27
|
it "should have an 'id' method" do
|
|
28
|
-
a = DottedHash.new(
|
|
29
|
-
b = DottedHash.new(id: 1)
|
|
28
|
+
a = DottedHash.new(id: 1)
|
|
30
29
|
expect(a.id).to eq(1)
|
|
31
|
-
expect(b.id).to eq(1)
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
it "should have a 'type' method" do
|
|
35
|
-
a = DottedHash.new(_type: 'foo')
|
|
36
|
-
b = DottedHash.new(type: 'foo')
|
|
37
|
-
expect(a.type).to eq('foo')
|
|
38
|
-
expect(b.type).to eq('foo')
|
|
39
30
|
end
|
|
40
31
|
|
|
41
32
|
it "should respond to :to_indexed_json" do
|
|
@@ -155,6 +146,12 @@ describe DottedHash do
|
|
|
155
146
|
it "should be inspectable" do
|
|
156
147
|
expect(@document.inspect).to match(/<DottedHash .* title|DottedHash .* author/)
|
|
157
148
|
end
|
|
149
|
+
|
|
150
|
+
it "merge!" do
|
|
151
|
+
doc = @document.merge!(mergetest: 'foo')
|
|
152
|
+
expect(doc.mergetest).to eq('foo')
|
|
153
|
+
expect(doc.title).to eq('Test')
|
|
154
|
+
end
|
|
158
155
|
|
|
159
156
|
context "within Rails" do
|
|
160
157
|
|
metadata
CHANGED
|
@@ -1,43 +1,43 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: dotted_hash
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: '0
|
|
4
|
+
version: '1.0'
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Ivan Stana
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2013-
|
|
11
|
+
date: 2013-12-03 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activemodel
|
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
|
16
16
|
requirements:
|
|
17
|
-
- -
|
|
17
|
+
- - ~>
|
|
18
18
|
- !ruby/object:Gem::Version
|
|
19
|
-
version: '
|
|
19
|
+
version: '4.0'
|
|
20
20
|
type: :runtime
|
|
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: '
|
|
26
|
+
version: '4.0'
|
|
27
27
|
- !ruby/object:Gem::Dependency
|
|
28
28
|
name: activesupport
|
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
|
30
30
|
requirements:
|
|
31
|
-
- -
|
|
31
|
+
- - ~>
|
|
32
32
|
- !ruby/object:Gem::Version
|
|
33
|
-
version: '0'
|
|
33
|
+
version: '4.0'
|
|
34
34
|
type: :runtime
|
|
35
35
|
prerelease: false
|
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
|
37
37
|
requirements:
|
|
38
|
-
- -
|
|
38
|
+
- - ~>
|
|
39
39
|
- !ruby/object:Gem::Version
|
|
40
|
-
version: '0'
|
|
40
|
+
version: '4.0'
|
|
41
41
|
- !ruby/object:Gem::Dependency
|
|
42
42
|
name: turn
|
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -94,6 +94,20 @@ dependencies:
|
|
|
94
94
|
- - ~>
|
|
95
95
|
- !ruby/object:Gem::Version
|
|
96
96
|
version: '2.13'
|
|
97
|
+
- !ruby/object:Gem::Dependency
|
|
98
|
+
name: rdoc
|
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
|
100
|
+
requirements:
|
|
101
|
+
- - ~>
|
|
102
|
+
- !ruby/object:Gem::Version
|
|
103
|
+
version: '4.0'
|
|
104
|
+
type: :development
|
|
105
|
+
prerelease: false
|
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
107
|
+
requirements:
|
|
108
|
+
- - ~>
|
|
109
|
+
- !ruby/object:Gem::Version
|
|
110
|
+
version: '4.0'
|
|
97
111
|
description: Recursive OpenStruct-like or Hash-like object. Based on Tire::Result::Item
|
|
98
112
|
with addition of writing attributes and security limits.
|
|
99
113
|
email:
|
|
@@ -105,6 +119,7 @@ extra_rdoc_files:
|
|
|
105
119
|
- MIT-LICENSE
|
|
106
120
|
files:
|
|
107
121
|
- .gitignore
|
|
122
|
+
- .travis.yml
|
|
108
123
|
- Gemfile
|
|
109
124
|
- LICENSE.txt
|
|
110
125
|
- MIT-LICENSE
|
|
@@ -137,7 +152,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
137
152
|
version: '0'
|
|
138
153
|
requirements: []
|
|
139
154
|
rubyforge_project:
|
|
140
|
-
rubygems_version: 2.0.
|
|
155
|
+
rubygems_version: 2.0.14
|
|
141
156
|
signing_key:
|
|
142
157
|
specification_version: 4
|
|
143
158
|
summary: Recursive OpenStruct-like object.
|