smart_properties 1.6.0 → 1.6.1
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.
- checksums.yaml +4 -4
- data/lib/smart_properties.rb +15 -3
- data/spec/smart_properties_spec.rb +5 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b925a25ffc3986a157e22c2b1e15d14fffe83ac2
|
4
|
+
data.tar.gz: 257d8a8aa05874ae0dcc43c00a60142c4fd809b7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6ad62801456bd554a4cf3984438490d4dadb9054a29a9dd16d4ffc87206a3a1d1abbeee25fdffc60810a5d1451b390882541f5b5540f2230d32b19f6f3828d24
|
7
|
+
data.tar.gz: d2557a94805341d73a358bacb2c3b0daa9a5c2916bc288789c7db2a0c0d4f28eb0b2890d075ec56cfde6f0dc3be3770a74a916ea673183e66712361d2f728335
|
data/lib/smart_properties.rb
CHANGED
@@ -21,7 +21,7 @@
|
|
21
21
|
# :required => true
|
22
22
|
#
|
23
23
|
module SmartProperties
|
24
|
-
VERSION = "1.6.
|
24
|
+
VERSION = "1.6.1"
|
25
25
|
|
26
26
|
class Error < ::ArgumentError; end
|
27
27
|
class ConfigurationError < Error; end
|
@@ -315,16 +315,28 @@ module SmartProperties
|
|
315
315
|
def initialize(attrs = {}, &block)
|
316
316
|
attrs ||= {}
|
317
317
|
properties = self.class.properties.each.to_a
|
318
|
+
missing_properties = []
|
318
319
|
|
319
320
|
# Assign attributes or default values
|
320
321
|
properties.each do |_, property|
|
321
|
-
|
322
|
-
|
322
|
+
if attrs.key?(property.name)
|
323
|
+
instance_variable_set("@#{property.name}", property.prepare(attrs[property.name], self))
|
324
|
+
else
|
325
|
+
missing_properties.push(property)
|
326
|
+
end
|
323
327
|
end
|
324
328
|
|
325
329
|
# Exectue configuration block
|
326
330
|
block.call(self) if block
|
327
331
|
|
332
|
+
# Set defaults
|
333
|
+
missing_properties.each do |property|
|
334
|
+
variable = "@#{property.name}"
|
335
|
+
if instance_variable_get(variable).nil? && !(default_value = property.default(self)).nil?
|
336
|
+
instance_variable_set(variable, property.prepare(default_value, self))
|
337
|
+
end
|
338
|
+
end
|
339
|
+
|
328
340
|
# Check presence of all required properties
|
329
341
|
faulty_properties =
|
330
342
|
properties.select { |_, property| property.required?(self) && send(property.name).nil? }.map(&:last)
|
@@ -468,9 +468,13 @@ describe SmartProperties do
|
|
468
468
|
expect { klass.new }.to raise_error(RuntimeError, 'Boom!')
|
469
469
|
end
|
470
470
|
|
471
|
-
it "should not evaluate the lambda expression and thus not raise during initialization if a different value for :boom has been provided" do
|
471
|
+
it "should not evaluate the lambda expression and thus not raise during initialization if a different value for :boom has been provided as a parameter" do
|
472
472
|
expect { klass.new(boom: 'Everything is just fine!') }.not_to raise_error
|
473
473
|
end
|
474
|
+
|
475
|
+
it "should not evaluate the lambda expression and thus not raise during initialization if a different value for :boom has been provided in an initilization block" do
|
476
|
+
expect { klass.new { |inst| inst.boom = 'Everything is just fine!' } }.not_to raise_error
|
477
|
+
end
|
474
478
|
end
|
475
479
|
end
|
476
480
|
|