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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 13450c3abde2a7100248385cefbafd3a41260c76
4
- data.tar.gz: 21ccd452b4ca350acecbf2209dcdc8723ccac4ac
3
+ metadata.gz: 3dccd5c932fd7197a40555f0ab7d4337a3a9018c
4
+ data.tar.gz: f3cb641cf1f2e2a3b845f781a7051aaab53e3293
5
5
  SHA512:
6
- metadata.gz: 163655b8c528a56443d47af75813d1e15aca9617154707bcff3ce6262f95ec78d32a6372cc8c5c7a8c9d5a54031360e6ae2126f046c1ab461490d411bb7fcfba
7
- data.tar.gz: 5889f79bf47c38db461b32ef119662ba7695cef4c0d016073d472dfd1fd1dfbc150c4e9d85ced5abf41d084ebd7e54f6a9811efad578b1bf8996f2585de8dc7d
6
+ metadata.gz: 931abd36ada5a91c9767a20d9a7b2535432364e6a5fdc30a9c3a98a5a5990f67bc7eb0f8889ae4673f5694fa240fce30df86593eaca75e2ec360b2cb855721b6
7
+ data.tar.gz: c2c0f6756869edd015074ffd0ad3aae5a9543e72ac16c72fb16887d9093c8dec4582c2ca6765630842d2895c97cf3f300c5184f697bba609c6635463570ae329
data/.gitignore CHANGED
@@ -1,2 +1,3 @@
1
1
  Gemfile.lock
2
2
  pkg/
3
+ html/
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - "1.9.3"
4
+ - "2.0.0"
5
+ - rbx-2.2.1
6
+ - jruby-19mode
data/Gemfile CHANGED
@@ -2,3 +2,8 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in dotted_hash.gemspec
4
4
  gemspec
5
+
6
+ platforms :rbx do
7
+ gem 'psych', '~> 2.0'
8
+ gem 'rubysl', '~> 2.0'
9
+ end
data/MIT-LICENSE CHANGED
@@ -1,4 +1,6 @@
1
- Copyright 2011 Karel Minarik
1
+ Original Copyright 2011 Karel Minarik
2
+
3
+ Copyright 2013 Ivan Stana
2
4
 
3
5
  Permission is hereby granted, free of charge, to any person obtaining
4
6
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -1,3 +1,5 @@
1
+ [![Build Status](https://travis-ci.org/istana/dotted_hash.png)](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", ">= 3.0"
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
- spec.add_development_dependency "rspec", "~> 2.13"
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 Item
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
- # answer to any write method
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[:_id] || @attributes[:id]
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
@@ -1,3 +1,3 @@
1
1
  class DottedHash
2
- VERSION = "0.9"
2
+ VERSION = "1.0"
3
3
  end
@@ -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(_id: 1)
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.9'
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-05-02 00:00:00.000000000 Z
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: '3.0'
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: '3.0'
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.3
155
+ rubygems_version: 2.0.14
141
156
  signing_key:
142
157
  specification_version: 4
143
158
  summary: Recursive OpenStruct-like object.