hanswurst 0.5.3 → 0.5.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/VERSION +1 -1
  2. data/lib/hanswurst.rb +29 -42
  3. data/test/test_hanswurst.rb +24 -1
  4. metadata +22 -22
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.3
1
+ 0.5.4
data/lib/hanswurst.rb CHANGED
@@ -145,9 +145,13 @@ class Hanswurst
145
145
 
146
146
  # helper for easy access to role attributes
147
147
  class As
148
- def initialize(doc, obj)
148
+ def initialize(doc, role)
149
149
  @doc = doc
150
- @obj = obj
150
+ @role = role
151
+ end
152
+
153
+ def _role
154
+ @role
151
155
  end
152
156
 
153
157
  def _id
@@ -160,9 +164,13 @@ class Hanswurst
160
164
 
161
165
  # we suppose, we have a subrole
162
166
  def method_missing(meth, *args, &code)
163
- obj = @obj.send(meth, *args)
167
+ # a hack around http://api.rubyonrails.org/classes/ActiveModel/Dirty.html
168
+ # since ActiveModel::Dirty does not recognize when property is a hash and hashentries do change
169
+ @doc.update_roles! if meth =~ /=$/
170
+
171
+ obj = @role.send(meth, *args)
164
172
  if obj.is_a? CouchPotato::Persistence
165
- obj = self.class.new(@doc, @obj.send(meth, *args))
173
+ obj = self.class.new(@doc, @role.send(meth, *args))
166
174
  end
167
175
  obj.instance_eval(&code) if code
168
176
  obj
@@ -225,18 +233,15 @@ class Hanswurst
225
233
  include CouchPotato::Persistence
226
234
 
227
235
  module MethodMissing
228
- # dispatches self.role___prop calls and self.role= setters
236
+ # dispatches self.role calls and self.role= setters
229
237
  def method_missing(meth, *args, &code)
230
238
  value = args.first
231
239
  case meth
232
- when /^([^=]+)___([^=]+)(\=)?$/ # obj.role__prop # => we get / set a property
233
- raise "use of ___ methods is deprecated, please use Hanswurst#as"
234
- # role, property, setter = $1, $2, $3
235
- # return setter ? set_property(role, property, value) : read(role, property, *args)
236
240
  when /^([^=]+)\=$/ # obj.role = ... # => we set a role
237
241
  role=$1
238
242
  add_role(role, value.class) unless role_exists?(role)
239
- return set_role(role, value) if role_exists?(role)
243
+ set_role(role, value) if role_exists?(role)
244
+ return as(meth)
240
245
  else
241
246
  return as(meth) if role_exists?(meth.to_s)
242
247
  end
@@ -277,7 +282,6 @@ class Hanswurst
277
282
  hanswust_data = attributes.delete :hanswurst_data
278
283
  obj = self.class.new attributes
279
284
  hanswust_data.each do |role,val|
280
- #obj.set role, val
281
285
  obj.send(:"#{role}=", val.dup)
282
286
  end
283
287
  obj
@@ -287,6 +291,12 @@ class Hanswurst
287
291
  add_roles(hsh)
288
292
  end
289
293
 
294
+ # a hack around http://api.rubyonrails.org/classes/ActiveModel/Dirty.html
295
+ # since ActiveModel::Dirty does not recognize when property is a hash and hashentries do change
296
+ def update_roles!
297
+ self.hanswurst_data_will_change!
298
+ end
299
+
290
300
  private
291
301
 
292
302
  # add a role
@@ -314,8 +324,8 @@ class Hanswurst
314
324
  raise "class expected: #{klass.inspect}" unless klass.is_a? Class
315
325
  handle_shared(role, klass)
316
326
  self.hanswurst_roles.update role.to_s => klass.name
317
-
318
327
  end
328
+ update_roles!
319
329
  self.hanswurst_roles
320
330
  end
321
331
 
@@ -329,13 +339,6 @@ class Hanswurst
329
339
  self.hanswurst_data and self.hanswurst_data[role.to_s]
330
340
  end
331
341
 
332
- # set a property of a role
333
- def set_property(role, property, value)
334
- return unless role_exists? role
335
- create(role) unless hanswurst_data_exists? role
336
- update role, property, value
337
- end
338
-
339
342
  # set a role
340
343
  def set_role(role, value)
341
344
  value ? set(role, value) : delete_role(role)
@@ -351,6 +354,7 @@ class Hanswurst
351
354
  def set(role, val)
352
355
  self.hanswurst_data ||= {}
353
356
  self.hanswurst_data.update role.to_s => val
357
+ update_roles!
354
358
  end
355
359
 
356
360
  # create a new instance of a role
@@ -358,26 +362,20 @@ class Hanswurst
358
362
  raise "unknown role #{role.inspect}" unless role_exists? role.to_s
359
363
  self.hanswurst_data ||= {}
360
364
  self.hanswurst_data[role.to_s] = self.class.getClass(self.hanswurst_roles[role.to_s]).new
365
+ update_roles!
361
366
  end
362
367
 
363
368
  # delete a role
364
369
  def delete(role)
365
370
  self.hanswurst_data ||= {}
366
- hsh = self.hanswurst_data
367
- hsh.delete(role.to_s)
368
- self.hanswurst_data = {} # we have to do this, otherwise it won't save?!
369
- self.hanswurst_data = hsh
370
- end
371
-
372
- # read a property of a role
373
- def read(role, property, *args)
374
- if hanswurst_data_exists?(role.to_s)
375
- return self.hanswurst_data[role.to_s].send(property.to_sym, *args)
376
- end
377
- nil
371
+ self.hanswurst_data.delete(role.to_s)
372
+ update_roles!
373
+ #self.hanswurst_data = {} # we have to do this, otherwise it won't save?!
374
+ #self.hanswurst_data = hsh
378
375
  end
379
376
 
380
377
  def as(role, &code)
378
+ return nil unless role_exists?(role)
381
379
  unless hanswurst_data[role.to_s]
382
380
  create(role.to_s)
383
381
  end
@@ -388,16 +386,5 @@ class Hanswurst
388
386
  end
389
387
  nil
390
388
  end
391
-
392
- # update a property of a role
393
- # we have to use this method in order to make sure that everything is saved
394
- def update(role, property, value)
395
- if hanswurst_data_exists? role
396
- self.hanswurst_data[role.to_s].send(:"#{property}=", value)
397
- hsh = self.hanswurst_data.merge(role.to_s => self.hanswurst_data[role.to_s])
398
- self.hanswurst_data = {} # we have to do this, otherwise it won't save?!
399
- self.hanswurst_data = hsh
400
- end
401
- end
402
389
  end
403
390
 
@@ -166,6 +166,29 @@ class TestCouch < Test::Unit::TestCase
166
166
  assert_equal 'Donald', o.person.firstname
167
167
  end
168
168
 
169
+ should "update role properties" do
170
+ o = Hanswurst.new
171
+ o.product = Prod.new( :article_number => 'xyz', :name => 'prod1' )
172
+ save o
173
+ o = load(o._id)
174
+ o.product.article_number = '123'
175
+ save o
176
+ o = load(o._id)
177
+ assert_equal 'prod1', o.product.name
178
+ assert_equal '123', o.product.article_number
179
+
180
+
181
+ h = Hanswurst.new( :f => F.new(:d => D.new(:my_prop => 'D')) )
182
+ save h
183
+ h = load(h._id)
184
+ assert_equal 'D', h.f.d.my_prop
185
+
186
+ h.f.d.my_prop = 'd'
187
+ save h
188
+ h = load(h._id)
189
+ assert_equal 'd', h.f.d.my_prop
190
+ end
191
+
169
192
  should "save hanswurst_roles" do
170
193
  o = Hanswurst.new
171
194
  o.person = Pers.new
@@ -191,7 +214,7 @@ class TestCouch < Test::Unit::TestCase
191
214
  o = load(o._id)
192
215
  assert_equal 'prod1', o.product.name
193
216
  assert_equal 'Bugs', o.person.firstname
194
- assert_equal 'abc', o.product.article_number
217
+ assert_equal 'cde', o.product.article_number
195
218
 
196
219
  o.product = nil
197
220
  save o
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hanswurst
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.3
4
+ version: 0.5.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-01-19 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: couch_potato
16
- requirement: &18119560 !ruby/object:Gem::Requirement
16
+ requirement: &19722520 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *18119560
24
+ version_requirements: *19722520
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: yard
27
- requirement: &18118720 !ruby/object:Gem::Requirement
27
+ requirement: &19721800 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 0.6.0
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *18118720
35
+ version_requirements: *19721800
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: bundler
38
- requirement: &18117960 !ruby/object:Gem::Requirement
38
+ requirement: &19720980 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 1.0.0
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *18117960
46
+ version_requirements: *19720980
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: jeweler
49
- requirement: &18117280 !ruby/object:Gem::Requirement
49
+ requirement: &19720300 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 1.5.2
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *18117280
57
+ version_requirements: *19720300
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rcov
60
- requirement: &18116680 !ruby/object:Gem::Requirement
60
+ requirement: &19719700 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *18116680
68
+ version_requirements: *19719700
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: mocha
71
- requirement: &18115920 !ruby/object:Gem::Requirement
71
+ requirement: &19719020 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *18115920
79
+ version_requirements: *19719020
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: shoulda
82
- requirement: &18115200 !ruby/object:Gem::Requirement
82
+ requirement: &19686960 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *18115200
90
+ version_requirements: *19686960
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: linecache19
93
- requirement: &18091980 !ruby/object:Gem::Requirement
93
+ requirement: &19686300 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: '0'
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *18091980
101
+ version_requirements: *19686300
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: ruby-debug19
104
- requirement: &18091400 !ruby/object:Gem::Requirement
104
+ requirement: &19685720 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - ! '>='
@@ -109,10 +109,10 @@ dependencies:
109
109
  version: '0'
110
110
  type: :development
111
111
  prerelease: false
112
- version_requirements: *18091400
112
+ version_requirements: *19685720
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: couch_potato
115
- requirement: &18090780 !ruby/object:Gem::Requirement
115
+ requirement: &19685160 !ruby/object:Gem::Requirement
116
116
  none: false
117
117
  requirements:
118
118
  - - ! '>='
@@ -120,7 +120,7 @@ dependencies:
120
120
  version: '0'
121
121
  type: :runtime
122
122
  prerelease: false
123
- version_requirements: *18090780
123
+ version_requirements: *19685160
124
124
  description: flexible enhancement of couch potato
125
125
  email: ! 'Base64.decode64(''bGludXhAbWFyY3JlbmVhcm5zLmRl
126
126
 
@@ -155,7 +155,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
155
155
  version: '0'
156
156
  segments:
157
157
  - 0
158
- hash: 103633383308200188
158
+ hash: -3336574218003098224
159
159
  required_rubygems_version: !ruby/object:Gem::Requirement
160
160
  none: false
161
161
  requirements: