smart_properties 1.10.1 → 1.11.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +15 -0
- data/lib/smart_properties/errors.rb +6 -6
- data/lib/smart_properties/property.rb +9 -3
- data/lib/smart_properties/version.rb +1 -1
- data/spec/reader_spec.rb +31 -0
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bc7370198b5c366256110bdf6dc2589491047c26
|
4
|
+
data.tar.gz: 149815152075ee2e125c4f55bb9b8916a1be631d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aa4d68dfabfd0b6cedbb2f2caa6a2105bd3ddd101bde45d3d2fef0fd52527b2ec2a3556c7b58bdf95dcf8075fc6917197e8cfda4d31795c630b6ed6a4a17f6f8
|
7
|
+
data.tar.gz: 895e03d0ad7aa8d96a76ce9c92e2cf72cd1ad558aeb99aaa644dc7c32fcff87f0f7db10631addf71b2193996a63b5ecf8d41a9ee66401cc6c56819fedebc042c
|
data/README.md
CHANGED
@@ -212,6 +212,21 @@ class Person
|
|
212
212
|
end
|
213
213
|
```
|
214
214
|
|
215
|
+
#### Custom readers
|
216
|
+
|
217
|
+
In Ruby, predicate methods by convention end with a `?`.
|
218
|
+
This convention is violated in the example above, but can easily be fixed by supplying a custom `reader` name:
|
219
|
+
|
220
|
+
```ruby
|
221
|
+
class Person
|
222
|
+
property :name, required: lambda { not anonymous }
|
223
|
+
property :anonymous, required: true, default: true, accepts: [true, false], reader: :anonymous?
|
224
|
+
end
|
225
|
+
```
|
226
|
+
|
227
|
+
To ensure backwards compatibility, boolean properties do not automatically change their reader name.
|
228
|
+
It is thus your responsibility to configure the property properly.
|
229
|
+
|
215
230
|
### Constructor argument forwarding
|
216
231
|
|
217
232
|
The `SmartProperties` initializer forwards anything to the super constructor
|
@@ -43,7 +43,7 @@ module SmartProperties
|
|
43
43
|
sender.class.name,
|
44
44
|
value.inspect,
|
45
45
|
property.name,
|
46
|
-
|
46
|
+
accepter_message(sender, property)
|
47
47
|
]
|
48
48
|
)
|
49
49
|
end
|
@@ -54,12 +54,12 @@ module SmartProperties
|
|
54
54
|
|
55
55
|
private
|
56
56
|
|
57
|
-
def
|
58
|
-
|
59
|
-
if
|
60
|
-
return "Values passing lambda defined in #{
|
57
|
+
def accepter_message(sender, property)
|
58
|
+
accepter = property.accepter
|
59
|
+
if accepter.is_a?(Proc)
|
60
|
+
return "Values passing lambda defined in #{accepter.source_location.join(' at line ')}"
|
61
61
|
end
|
62
|
-
|
62
|
+
accepter
|
63
63
|
end
|
64
64
|
end
|
65
65
|
|
@@ -7,8 +7,9 @@ module SmartProperties
|
|
7
7
|
module IndexMethods
|
8
8
|
def [](name)
|
9
9
|
return if name.nil?
|
10
|
-
name
|
11
|
-
|
10
|
+
name = name.to_sym
|
11
|
+
reader = self.class.properties[name].reader
|
12
|
+
public_send(reader) if self.class.properties.key?(name)
|
12
13
|
end
|
13
14
|
|
14
15
|
def []=(name, value)
|
@@ -20,6 +21,7 @@ module SmartProperties
|
|
20
21
|
attr_reader :name
|
21
22
|
attr_reader :converter
|
22
23
|
attr_reader :accepter
|
24
|
+
attr_reader :reader
|
23
25
|
attr_reader :instance_variable_name
|
24
26
|
|
25
27
|
def self.define(scope, name, options = {})
|
@@ -34,6 +36,8 @@ module SmartProperties
|
|
34
36
|
@converter = attrs.delete(:converts)
|
35
37
|
@accepter = attrs.delete(:accepts)
|
36
38
|
@required = attrs.delete(:required)
|
39
|
+
@reader = attrs.delete(:reader)
|
40
|
+
@reader ||= @name
|
37
41
|
|
38
42
|
@instance_variable_name = :"@#{name}"
|
39
43
|
|
@@ -101,7 +105,9 @@ module SmartProperties
|
|
101
105
|
m
|
102
106
|
end
|
103
107
|
|
104
|
-
scope.send(:
|
108
|
+
scope.send(:define_method, reader) do
|
109
|
+
property.get(self)
|
110
|
+
end
|
105
111
|
scope.send(:define_method, :"#{name}=") do |value|
|
106
112
|
property.set(self, value)
|
107
113
|
end
|
data/spec/reader_spec.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe SmartProperties, 'reader' do
|
4
|
+
context "when defining a class with a property with a custom reader" do
|
5
|
+
subject(:klass) do
|
6
|
+
DummyClass.new do
|
7
|
+
property :new, default: false, accepts: [true, false], reader: :new?
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
context "an instance of this class" do
|
12
|
+
subject(:instance) { klass.new }
|
13
|
+
|
14
|
+
it "should read the property using a custom reader" do
|
15
|
+
instance = klass.new new: true
|
16
|
+
expect(instance.new?).to eq(true)
|
17
|
+
|
18
|
+
instance.new = false
|
19
|
+
expect(instance.new?).to eq(false)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should still read the property with the property name when using the #[] syntax" do
|
23
|
+
instance = klass.new new: true
|
24
|
+
expect(instance[:new]).to eq(true)
|
25
|
+
|
26
|
+
instance = klass.new new: false
|
27
|
+
expect(instance[:new]).to eq(false)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
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.11.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-09-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -81,6 +81,7 @@ files:
|
|
81
81
|
- spec/default_values_spec.rb
|
82
82
|
- spec/inheritance_spec.rb
|
83
83
|
- spec/property_collection_caching_spec.rb
|
84
|
+
- spec/reader_spec.rb
|
84
85
|
- spec/required_values_spec.rb
|
85
86
|
- spec/spec_helper.rb
|
86
87
|
- spec/support/dummy_class.rb
|
@@ -116,8 +117,8 @@ test_files:
|
|
116
117
|
- spec/default_values_spec.rb
|
117
118
|
- spec/inheritance_spec.rb
|
118
119
|
- spec/property_collection_caching_spec.rb
|
120
|
+
- spec/reader_spec.rb
|
119
121
|
- spec/required_values_spec.rb
|
120
122
|
- spec/spec_helper.rb
|
121
123
|
- spec/support/dummy_class.rb
|
122
124
|
- spec/support/smart_property_matcher.rb
|
123
|
-
has_rdoc:
|