smart_properties 1.0.2 → 1.0.3
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.
- data/lib/smart_properties.rb +4 -4
- data/spec/smart_properties_spec.rb +18 -8
- metadata +3 -3
data/lib/smart_properties.rb
CHANGED
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
#
|
|
23
23
|
module SmartProperties
|
|
24
24
|
|
|
25
|
-
VERSION = "1.0.
|
|
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) ?
|
|
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 :
|
|
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 :
|
|
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
|
-
|
|
393
|
-
klass.new
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
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.
|
|
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:
|
|
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:
|
|
138
|
+
hash: 3382560522935455660
|
|
139
139
|
requirements: []
|
|
140
140
|
rubyforge_project:
|
|
141
141
|
rubygems_version: 1.8.24
|