angry_hash 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2010 PLUS2 Pty. Ltd.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,114 @@
1
+ # AngryHash
2
+
3
+ A stabler mash with different emphases.
4
+
5
+ # Install
6
+
7
+ gem install angry_hash
8
+
9
+ # Usage
10
+ grr = AngryHash[] #=> {}
11
+ grr = AngryHash.new #=> {}
12
+
13
+ grr = AngryHash[ :look => { :a => ['hash'] } ]
14
+
15
+ # reach into the hash using dot notation
16
+ grr.look.a[0] #=> 'hash'
17
+
18
+ # instantiate a sub-hash (idempotently):
19
+ grr.look.another #=> nil
20
+ grr.look.another!.one = "nice"
21
+
22
+ grr.look.another!.one #=> "nice"
23
+ grr.look.another.one #=> "nice"
24
+
25
+ # truth in hashes
26
+ grr.green = true
27
+ grr.yellow = [:goldenrod,:canary]
28
+ grr.red = false
29
+
30
+ grr.green? #=> true
31
+ grr.yellow? #=> true
32
+ grr.red? #=> false
33
+ grr.blue? #=> false
34
+
35
+
36
+ ## Merging Deeply
37
+
38
+ ### Merge
39
+
40
+ Deep merges are non-destructive, as in normal hash merges.
41
+
42
+ Merge favours the other hash's keys. Reverse merge favours the target's keys.
43
+
44
+ grr = AngryHash[ :a => {:b => :c}, :z => :x ]
45
+ arr = AngryHash[ :a => {:d => :e}, :z => :y ]
46
+
47
+ grr.deep_merge( arr ) #=> {"a" => {"b" => :c, "d" => :e}, "z" => :y}
48
+ grr.reverse_deep_merge( arr ) #=> {"a" => {"b" => :c, "d" => :e}, "z" => :x}
49
+
50
+ # grr & arr are unmodified
51
+ grr #=> { "a" => {"b" => :c}, "z" => :x }
52
+ arr #=> { "a" => {"d" => :e}, "z" => :y }
53
+
54
+ ### Update
55
+
56
+ Deep updates replace the contents of the hash with the merged version.
57
+
58
+ They're also known as `deep_merge!` and `reverse_deep_merge!`
59
+
60
+ grr = AngryHash[ :a => {:b => :c}, :z => :x ]
61
+ arr = AngryHash[ :a => {:d => :e}, :z => :y ]
62
+
63
+ grr.deep_update( arr )
64
+
65
+ # grr is updated, arr is unmodified
66
+ grr #=> { "a" => {"b" => :c, "d" => :e}, "z" => :y }
67
+ arr #=> { "a" => {"d" => :e}, "z" => :y }
68
+
69
+ grr.reverse_deep_update( arr )
70
+
71
+ # grr is updated, arr is unmodified
72
+ grr #=> { "a" => {"b" => :c, "d" => :e}, "z" => :x }
73
+ arr #=> { "a" => {"d" => :e}, "z" => :y }
74
+
75
+ # Extensions
76
+
77
+ `TODO` write up this section.
78
+
79
+ # Gotchas
80
+
81
+ ## AngryHashes are Hashes
82
+
83
+ This is good and bad. Its good because you can use all the normal hash methods.
84
+
85
+ AngryHash[ :a => :b, :c => :d ].each {|k,v| ... }
86
+
87
+ Unfortunately this also means that that if your hash keys collide with the names of Hash methods (or those of its ancestors) you can't use dot notation:
88
+
89
+ h = AngryHash[ :hash => 'hooray' ]
90
+
91
+ h.hash #=> 12345...
92
+ h['hash'] #=> 'hooray'
93
+ h[:hash] #=> 'hooray'
94
+
95
+ I'm planning on creating a sanitised `AngryProxy` to wrap `AngryHash`es for the times you really want guaranteed access via dot notation.
96
+
97
+ ## id
98
+
99
+ Since `Object#id` is deprecated in Ruby 1.8 and has been removed in 1.9, I decided it was safe to override `id` in `AngryHash`:
100
+
101
+ h = AngryHash[ :id => 'abc' ]
102
+
103
+ h.id #=> 'abc'
104
+ h.__id__ #=> 12345...
105
+
106
+ # About
107
+
108
+ `AngryHash`'s code is hosted on GitHub and can be found at http://github.com/plus2/angry_hash
109
+
110
+ Please report problems at http://github.com/plus2/angry_hash/issues.
111
+
112
+ `AngryHash` is by Lachie Cox for PLUS2 & YesMaster.
113
+
114
+ You're free to use AngryHash under the MIT license; see License for details.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.2.1
data/angry_hash.gemspec CHANGED
@@ -5,13 +5,17 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{angry_hash}
8
- s.version = "0.2.0"
8
+ s.version = "0.2.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Lachie Cox"]
12
- s.date = %q{2010-08-25}
12
+ s.date = %q{2010-08-28}
13
13
  s.description = %q{A stabler mash with different emphases. Used in plus2 projects AngryMob and Igor.}
14
14
  s.email = %q{lachie@plus2.com.au}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.md"
18
+ ]
15
19
  s.files = [
16
20
  ".gitignore",
17
21
  "Changelog.md",
data/examples/dup_eg.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  require 'eg_helper'
2
2
 
3
- require 'angry_hash/extension_tracking'
4
- AngryHash.send :include, AngryHash::ExtensionTracking
3
+ require 'angry_hash/extension'
5
4
 
6
5
  eg.setup do
7
6
  @original = { 'database' => {
@@ -42,7 +41,7 @@ end
42
41
 
43
42
  module Extendo
44
43
  def as_dag
45
- dag = dup
44
+ dag = dup_with_extension
46
45
  dag
47
46
  end
48
47
 
data/examples/merge_eg.rb CHANGED
@@ -133,3 +133,12 @@ eg 'merge with symbol key' do
133
133
  Show( merged )
134
134
  Assert( merged.keys[1].is_a?(String) )
135
135
  end
136
+
137
+ eg 'merge regression 2' do
138
+ other = { 'subscriptions' => [{'id' => 'fooper', 'kind' => 'topic', 'key' => 'bloop.*'}] }
139
+ this = AngryHash[ :whatever => 'blah blah' ]
140
+
141
+ this.deep_update(other)
142
+
143
+ Assert( AngryHash === this.subscriptions.first )
144
+ end
data/lib/angry_hash.rb CHANGED
@@ -48,7 +48,8 @@ class AngryHash < Hash
48
48
  end
49
49
 
50
50
  def deep_merge(other_hash)
51
- # XXX this should convert other to AHash!
51
+ other_hash = AngryHash[other_hash]
52
+
52
53
  self.regular_merge( other_hash ) do |key, oldval, newval|
53
54
  oldval = AngryHash.__convert_value(oldval)
54
55
  newval = AngryHash.__convert_value(newval)
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 0
9
- version: 0.2.0
8
+ - 1
9
+ version: 0.2.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Lachie Cox
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-08-25 00:00:00 +10:00
17
+ date: 2010-08-28 00:00:00 +10:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -24,8 +24,9 @@ executables: []
24
24
 
25
25
  extensions: []
26
26
 
27
- extra_rdoc_files: []
28
-
27
+ extra_rdoc_files:
28
+ - LICENSE
29
+ - README.md
29
30
  files:
30
31
  - .gitignore
31
32
  - Changelog.md
@@ -38,6 +39,8 @@ files:
38
39
  - lib/angry_hash/dsl.rb
39
40
  - lib/angry_hash/extension.rb
40
41
  - lib/angry_hash/merge_string.rb
42
+ - LICENSE
43
+ - README.md
41
44
  has_rdoc: true
42
45
  homepage: http://github.com/plus2/angry_hash
43
46
  licenses: []