angry_hash 0.0.5 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.5
1
+ 0.1.0
data/angry_hash.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{angry_hash}
8
- s.version = "0.0.5"
8
+ s.version = "0.1.0"
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-06-02}
12
+ s.date = %q{2010-08-07}
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
15
  s.files = [
@@ -19,10 +19,13 @@ Gem::Specification.new do |s|
19
19
  "angry_hash.gemspec",
20
20
  "examples/accessors_eg.rb",
21
21
  "examples/creation_eg.rb",
22
+ "examples/dsl.eg.rb",
22
23
  "examples/dup_eg.rb",
23
24
  "examples/eg_helper.rb",
24
25
  "examples/merge_eg.rb",
25
26
  "lib/angry_hash.rb",
27
+ "lib/angry_hash/dsl.rb",
28
+ "lib/angry_hash/extension_tracking.rb",
26
29
  "lib/angry_hash/merge_string.rb"
27
30
  ]
28
31
  s.homepage = %q{http://github.com/plus2/angry_hash}
@@ -33,3 +33,14 @@ eg 'accessor= Hash' do
33
33
  @a.d = d
34
34
  Assert( ! same_obj( d, @a.d ) )
35
35
  end
36
+
37
+ eg 'accessor?' do
38
+ Assert( @a.b? )
39
+ Assert( ! @a.c? )
40
+ Assert( @a.b.c? )
41
+ Assert( ! @a.b.x? )
42
+
43
+ @a.b.f = false
44
+
45
+ Assert( ! @a.b.f? )
46
+ end
@@ -38,5 +38,6 @@ eg 'creation duplicates' do
38
38
  Assert(a1.c[7].__id__ != a2.c[7].__id__)
39
39
  end
40
40
 
41
+
41
42
  #eg 'cycle detection' do
42
43
  #end
@@ -0,0 +1,18 @@
1
+ require 'eg_helper'
2
+
3
+ eg 'eval a block as dsl' do
4
+ original = AngryHash[ :a => 'x', :b => {:c => 1}, :d => 'z' ]
5
+
6
+ original.__eval_as_dsl do
7
+ foo 'bar'
8
+ a 'y'
9
+ b :e => 2
10
+ end
11
+
12
+ Assert(original.foo == 'bar')
13
+ Assert(original.a == 'y')
14
+ Assert(original.d == 'z')
15
+
16
+ Assert(original.b.c == 1)
17
+ Assert(original.b.e == 2)
18
+ end
data/examples/dup_eg.rb CHANGED
@@ -1,61 +1,15 @@
1
1
  require 'eg_helper'
2
2
 
3
+ require 'angry_hash/extension_tracking'
4
+ AngryHash.send :include, AngryHash::ExtensionTracking
5
+
3
6
  eg.setup do
4
7
  @original = { 'database' => {
5
- "micropower_micropower_sandbox"=>
6
- {"app_owner"=>"micropower_micropower_sandbox", "server"=>:db},
7
- "amc_accred_staging"=>
8
- {"app_owner"=>"amc_accred", "server"=>:plus2staging_local_mysql},
9
- "post_froggy"=>
10
- {"app_owner"=>"froggy_owner",
11
- "backup"=>{"to_s3"=>true},
12
- "server"=>:postfrog},
13
- "isg_url_shortener"=>
14
- {"app_owner"=>"isg_sandbox_v5", "server"=>:isg_forums_mysql},
15
- "isg_forums"=>{"app_owner"=>"isg_forums", "server"=>:isg_forums_mysql},
16
- "eggs_eggs"=>
17
- {"app_owner"=>"eggs_eggs",
18
- "backup"=>{"to_s3"=>true},
19
- "server"=>:db,
20
- "admin_owner"=>"eggs_admin"},
21
- "myfrogdb"=>{"app_owner"=>:myfroggy, "server"=>:myfrog},
22
- "amc_exam_staging"=>
23
- {"app_owner"=>"amc_exam", "server"=>:plus2staging_local_mysql},
24
- "micropower_golfo"=>{"app_owner"=>:golfo, "server"=>:linode_golfo_mysql},
25
- "linode_golfo"=>{"app_owner"=>:golfo, "server"=>:linode_golfo_mysql},
26
- "halal_staging"=>
27
- {"app_owner"=>"halal", "server"=>:plus2staging_local_postgres},
28
- "eggs_enag"=>
29
- {"app_owner"=>"eggs_enag", "backup"=>{"to_s3"=>true}, "server"=>:db},
30
- "amc_store"=>
31
- {"app_owner"=>"amc_store", "backup"=>{"to_s3"=>true}, "server"=>:amc},
32
- "weatherzone_wengine"=>
33
- {"app_owner"=>"weatherzone_wengine", "server"=>:linode_postgres},
34
- "weatherzone_wzfb_staging"=>
35
- {"app_owner"=>"weatherzone_wzfb_staging", "server"=>:db},
36
- "amc_ncmr"=>
37
- {"app_owner"=>"amc_ncmr", "backup"=>{"to_s3"=>true}, "server"=>:amc},
38
- "westpac_thinkbank_staging"=>
39
- {"app_owner"=>"westpac_thinkbank_staging", "server"=>:db},
40
- "micropower_micropower_staging"=>
41
- {"app_owner"=>"micropower", "server"=>:staging_mysql},
42
- "weatherzone_wx_staging"=>
43
- {"app_owner"=>"weatherzone_wx_staging", "server"=>:db},
44
- "isg_sandbox_v4"=>
45
- {"app_owner"=>"isg_sandbox_v5", "server"=>:isg_forums_mysql},
46
- "eggs_aecl"=>
47
- {"app_owner"=>"eggs_aecl", "backup"=>{"to_s3"=>true}, "server"=>:db},
48
- "eggs_hwag"=>
49
- {"app_owner"=>"eggs_hwag", "backup"=>{"to_s3"=>true}, "server"=>:db},
50
- "plus2_gemcutter"=>{"app_owner"=>"plus2_gemcutter", "server"=>:db},
51
- "isg_sandbox_v5"=>
52
- {"app_owner"=>"isg_sandbox_v5", "server"=>:isg_forums_mysql},
53
- "amc_exam"=>
54
- {"app_owner"=>"amc_exam", "backup"=>{"to_s3"=>true}, "server"=>:amc},
55
- "weatherzone_wzfb"=>
56
- {"app_owner"=>"weatherzone_wzfb", "backup"=>{"to_s3"=>true}, "server"=>:db},
57
- "amc_store_staging"=>
58
- {"app_owner"=>"amc_store", "server"=>:plus2staging_local_mysql}
8
+ "micropower_micropower_sandbox" => {"app_owner" => "micropower_micropower_sandbox", "server" => :db},
9
+ "post_froggy" => {"app_owner" => "froggy_owner", "backup" => {"to_s3" => true}, "server" => :postfrog},
10
+ "myfrogdb" => {"app_owner" => :myfroggy, "server" => :myfrog},
11
+ "eggs_hwag" => {"app_owner" => "eggs_hwag", "backup" => {"to_s3" => true}, "server" => :db},
12
+ "isg_sandbox_v5" => {"app_owner" => "isg_sandbox_v5", "server" => :isg_forums_mysql},
59
13
  }}
60
14
  end
61
15
 
@@ -67,11 +21,34 @@ eg 'duping copies symbols' do
67
21
  Assert( ah2.database.isg_sandbox_v5.server == @original['database']['isg_sandbox_v5']['server'] )
68
22
  end
69
23
 
24
+ def same_obj(a,b)
25
+ a.__id__ == b.__id__
26
+ end
27
+
28
+ def diff_obj(a,b)
29
+ ! same_obj(a,b)
30
+ end
31
+
32
+ eg 'dup is deep' do
33
+ ah = AngryHash[ @original ]
34
+ ah2 = ah.dup
35
+
36
+ Assert( diff_obj ah , ah2 )
37
+ Assert( diff_obj ah.database , ah2.database )
38
+ Assert( diff_obj ah.database.post_froggy , ah2.database.post_froggy )
39
+ Assert( diff_obj ah.database.post_froggy.backup, ah2.database.post_froggy.backup )
40
+
41
+ end
42
+
70
43
  module Extendo
71
44
  def as_dag
72
45
  dag = dup
73
46
  dag
74
47
  end
48
+
49
+ def is_extended?
50
+ true
51
+ end
75
52
  end
76
53
 
77
54
  eg 'duping from ext' do
@@ -80,9 +57,30 @@ eg 'duping from ext' do
80
57
 
81
58
  ah2 = ah.as_dag
82
59
 
83
- Show( AngryHash.shooper )
84
60
  Show( ah.__id__ )
85
61
  Show( ah2.__id__ )
86
62
 
87
63
  Show( ah2 )
88
64
  end
65
+
66
+ module ExtendoDb
67
+ def is_db_extended?
68
+ true
69
+ end
70
+ end
71
+
72
+ eg 'extension preservation' do
73
+ ah = AngryHash[ @original ]
74
+ Assert( ! ah.is_extended? )
75
+ Assert( ! ah.database.post_froggy.is_db_extended? )
76
+
77
+ ah.extend(Extendo)
78
+ ah.database.post_froggy.extend(ExtendoDb)
79
+
80
+ Assert( ah.is_extended? )
81
+ Assert( ah.database.post_froggy.is_db_extended? )
82
+
83
+ ah2 = ah.dup
84
+ Show( ah2.is_extended? )
85
+ Show( ah2.database.post_froggy.is_db_extended? )
86
+ end
@@ -14,3 +14,11 @@ end
14
14
 
15
15
  require 'rubygems'
16
16
  require 'exemplor'
17
+
18
+ class Object
19
+ def tapp(tag=nil)
20
+ print "#{tag}=" if tag
21
+ pp self
22
+ self
23
+ end
24
+ end
data/examples/merge_eg.rb CHANGED
@@ -130,5 +130,6 @@ eg 'merge with symbol key' do
130
130
 
131
131
  merged = orig.merge(:key=>"db")
132
132
 
133
+ Show( merged )
133
134
  Assert( merged.keys[1].is_a?(String) )
134
135
  end
@@ -0,0 +1,44 @@
1
+ class AngryHash
2
+ module DSL
3
+ # from ActiveSupport 3
4
+ if defined? ::BasicObject
5
+ # A class with no predefined methods that behaves similarly to Builder's
6
+ # BlankSlate. Used for proxy classes.
7
+ class BasicObject < ::BasicObject
8
+ undef_method :==
9
+ undef_method :equal?
10
+
11
+ # Let BasicObject at least raise exceptions.
12
+ def raise(*args)
13
+ ::Object.send(:raise, *args)
14
+ end
15
+ end
16
+ else
17
+ class BasicObject #:nodoc:
18
+ instance_methods.each do |m|
19
+ undef_method(m) if m.to_s !~ /(?:^__|^nil\?$|^send$|^object_id$|^instance_eval$)/
20
+ end
21
+ end
22
+ end
23
+
24
+ class Env < BasicObject
25
+ def __store
26
+ @store ||= {}
27
+ end
28
+ def method_missing(method,*args,&blk)
29
+ method_s = method.to_s
30
+ if method_s[/[A-Za-z0-9]$/] && args.size == 1
31
+ __store[method.to_s] = args.first
32
+ else
33
+ super
34
+ end
35
+ end
36
+ end
37
+
38
+ def __eval_as_dsl(&blk)
39
+ env = Env.new
40
+ env.instance_eval(&blk)
41
+ deep_update(env.__store)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,12 @@
1
+ class AngryHash
2
+ module ExtensionTracking
3
+ def self.included(base)
4
+ # base.extend ClassMethods
5
+ end
6
+ end
7
+
8
+ def extend(mod)
9
+ puts "extending AH #{__id__} with #{mod}"
10
+ super
11
+ end
12
+ end
data/lib/angry_hash.rb CHANGED
@@ -1,7 +1,7 @@
1
+
1
2
  class AngryHash < Hash
2
- def self.shooper
3
- "mooper"
4
- end
3
+ require 'angry_hash/dsl'
4
+ include AngryHash::DSL
5
5
 
6
6
  def self.[](other)
7
7
  super(__convert(other))
@@ -19,6 +19,10 @@ class AngryHash < Hash
19
19
  regular_reader(__convert_key(key))
20
20
  end
21
21
 
22
+ def id
23
+ regular_reader('id')
24
+ end
25
+
22
26
  def dup_and_store(key,value)
23
27
  regular_writer(__convert_key(key), self.class.__convert_value(value))
24
28
  end
@@ -39,7 +43,8 @@ class AngryHash < Hash
39
43
  end
40
44
 
41
45
  def deep_merge(other_hash)
42
- self.regular_merge(other_hash) do |key, oldval, newval|
46
+ # XXX this should convert other to AHash!
47
+ self.regular_merge( other_hash ) do |key, oldval, newval|
43
48
  oldval = AngryHash.__convert_value(oldval)
44
49
  newval = AngryHash.__convert_value(newval)
45
50
 
@@ -119,17 +124,20 @@ class AngryHash < Hash
119
124
 
120
125
  case method_s[-1]
121
126
  when ?=
122
- #regular_writer(key,args.first)
127
+ return super unless args.size == 1 && !block_given?
123
128
  self[ key ] = args.first
124
129
 
125
130
  when ??
131
+ return super unless args.empty? && !block_given?
126
132
  !! self[key]
127
133
 
128
134
  when ?!
135
+ return super unless args.empty?
129
136
  self[key] = AngryHash.new unless self.key?(key)
130
137
  self[key]
131
138
 
132
139
  else
140
+ return super unless args.empty? && !block_given?
133
141
  self[method_s]
134
142
  end
135
143
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
+ - 1
7
8
  - 0
8
- - 5
9
- version: 0.0.5
9
+ version: 0.1.0
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-06-02 00:00:00 +10:00
17
+ date: 2010-08-07 00:00:00 +10:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -33,10 +33,13 @@ files:
33
33
  - angry_hash.gemspec
34
34
  - examples/accessors_eg.rb
35
35
  - examples/creation_eg.rb
36
+ - examples/dsl.eg.rb
36
37
  - examples/dup_eg.rb
37
38
  - examples/eg_helper.rb
38
39
  - examples/merge_eg.rb
39
40
  - lib/angry_hash.rb
41
+ - lib/angry_hash/dsl.rb
42
+ - lib/angry_hash/extension_tracking.rb
40
43
  - lib/angry_hash/merge_string.rb
41
44
  has_rdoc: true
42
45
  homepage: http://github.com/plus2/angry_hash