smart_properties 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -22,7 +22,7 @@
22
22
  #
23
23
  module SmartProperties
24
24
 
25
- VERSION = "1.0.2"
25
+ VERSION = "1.0.3"
26
26
 
27
27
  class Property
28
28
 
@@ -60,8 +60,8 @@ module SmartProperties
60
60
  end
61
61
  end
62
62
 
63
- def default
64
- @default.kind_of?(Proc) ? @default.call : @default
63
+ def default(scope)
64
+ @default.kind_of?(Proc) ? scope.instance_exec(&@default) : @default
65
65
  end
66
66
 
67
67
  def accepts?(value, scope)
@@ -210,7 +210,7 @@ module SmartProperties
210
210
  attrs ||= {}
211
211
 
212
212
  self.class.properties.each do |_, property|
213
- value = attrs.key?(property.name) ? attrs.delete(property.name) : property.default
213
+ value = attrs.key?(property.name) ? attrs.delete(property.name) : property.default(self)
214
214
  send(:"#{property.name}=", value)
215
215
  end
216
216
  end
@@ -373,14 +373,25 @@ describe SmartProperties do
373
373
 
374
374
  end
375
375
 
376
- context 'when used to build a class that has a property called :text whose default value is a lambda statement' do
376
+ context 'when used to build a class that has a property called :id whose default value is a lambda statement' do
377
377
 
378
378
  subject do
379
+ counter = Class.new.tap do |c|
380
+
381
+ c.class_eval do
382
+ def next
383
+ @counter ||= 0
384
+ @counter += 1
385
+ end
386
+ end
387
+
388
+ end.new
389
+
379
390
  Class.new.tap do |c|
380
391
  c.send(:include, described_class)
381
392
 
382
393
  c.instance_eval do
383
- property :text, :default => lambda { "-" * 3 }
394
+ property :id, :default => lambda { counter.next }
384
395
  end
385
396
  end
386
397
  end
@@ -389,12 +400,11 @@ describe SmartProperties do
389
400
 
390
401
  klass = subject.call
391
402
 
392
- subject do
393
- klass.new
394
- end
395
-
396
- it "should return the result of the lambda statement as default value for :text" do
397
- subject.text.should be == '---'
403
+ it "should have auto-incrementing ids" do
404
+ first_instance = klass.new
405
+ second_instance = klass.new
406
+
407
+ (second_instance.id - first_instance.id).should be == 1
398
408
  end
399
409
 
400
410
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smart_properties
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -126,7 +126,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
126
126
  version: '0'
127
127
  segments:
128
128
  - 0
129
- hash: -4417476715899075786
129
+ hash: 3382560522935455660
130
130
  required_rubygems_version: !ruby/object:Gem::Requirement
131
131
  none: false
132
132
  requirements:
@@ -135,7 +135,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
135
135
  version: '0'
136
136
  segments:
137
137
  - 0
138
- hash: -4417476715899075786
138
+ hash: 3382560522935455660
139
139
  requirements: []
140
140
  rubyforge_project:
141
141
  rubygems_version: 1.8.24