smart_properties 1.11.0 → 1.12.0
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/README.md +8 -0
- data/lib/smart_properties.rb +6 -0
- data/lib/smart_properties/version.rb +1 -1
- data/spec/required_values_spec.rb +38 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 308f2acd5b26ff6c0d5f008835d1ed86ba18218c
|
4
|
+
data.tar.gz: d3744f7a5be843235043d5f04d290693016e0877
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7f62e2aee8c528d1d6387e5e51594a998e6ef81c9afdc17935e51389c774f769eaeadc7cd3d55b0b7242c50730be371d2c843f26a4ccfe4e0d34c9f5fb72bb35
|
7
|
+
data.tar.gz: 34fc69a7afb5635f32201fb8e3c9ca132177fe628ffcd3b7b3f11462dd1901360041234542dc9998b8fd04ab8b62593ed07308b834d5f482152fd17955150435
|
data/README.md
CHANGED
@@ -200,6 +200,14 @@ class Article
|
|
200
200
|
end
|
201
201
|
```
|
202
202
|
|
203
|
+
Alternatively you can also use the `property!` method.
|
204
|
+
|
205
|
+
```ruby
|
206
|
+
class Article
|
207
|
+
property! :title
|
208
|
+
end
|
209
|
+
```
|
210
|
+
|
203
211
|
The decision whether or not a property is required can also be delayed and
|
204
212
|
evaluated at runtime by providing a block instead of a boolean value. The
|
205
213
|
example below shows how to implement a class that has two properties, `name`
|
data/lib/smart_properties.rb
CHANGED
@@ -83,6 +83,12 @@ module SmartProperties
|
|
83
83
|
properties[name] = Property.define(self, name, options)
|
84
84
|
end
|
85
85
|
protected :property
|
86
|
+
|
87
|
+
def property!(name, options = {})
|
88
|
+
options[:required] = true
|
89
|
+
property(name, options)
|
90
|
+
end
|
91
|
+
protected :property!
|
86
92
|
end
|
87
93
|
|
88
94
|
class << self
|
@@ -39,6 +39,44 @@ RSpec.describe SmartProperties do
|
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
|
+
context "when building a class that has a property! which is required and has no default" do
|
43
|
+
subject(:klass) { DummyClass.new { property! :title } }
|
44
|
+
|
45
|
+
context 'an instance of this class' do
|
46
|
+
it 'should have the correct title when provided with a title during initialization' do
|
47
|
+
instance = klass.new title: 'Lorem Ipsum'
|
48
|
+
expect(instance.title).to eq('Lorem Ipsum')
|
49
|
+
expect(instance[:title]).to eq('Lorem Ipsum')
|
50
|
+
|
51
|
+
instance = klass.new { |i| i.title = 'Lorem Ipsum' }
|
52
|
+
expect(instance.title).to eq('Lorem Ipsum')
|
53
|
+
expect(instance[:title]).to eq('Lorem Ipsum')
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should raise an error stating that required properties are missing when initialized without a title" do
|
57
|
+
exception = SmartProperties::InitializationError
|
58
|
+
message = "Dummy requires the following properties to be set: title"
|
59
|
+
further_expectations = lambda { |error| expect(error.to_hash[:title]).to eq('must be set') }
|
60
|
+
|
61
|
+
expect { klass.new }.to raise_error(exception, message, &further_expectations)
|
62
|
+
expect { klass.new {} }.to raise_error(exception, message, &further_expectations)
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should not allow to set nil as the property's value" do
|
66
|
+
instance = klass.new title: 'Lorem Ipsum'
|
67
|
+
|
68
|
+
exception = SmartProperties::MissingValueError
|
69
|
+
message = "Dummy requires the property title to be set"
|
70
|
+
further_expectations = lambda do |error|
|
71
|
+
expect(error.to_hash[:title]).to eq('must be set')
|
72
|
+
end
|
73
|
+
|
74
|
+
expect { instance.title = nil }.to raise_error(exception, message, &further_expectations)
|
75
|
+
expect { instance[:title] = nil }.to raise_error(exception, message, &further_expectations)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
42
80
|
context "when building a class that has a property name which is only required if the property anonymous is set to false" do
|
43
81
|
subject(:klass) do
|
44
82
|
DummyClass.new do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: smart_properties
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.12.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Konstantin Tennhard
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-10-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|