smart_properties 1.12.0 → 1.13.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/lib/smart_properties.rb +11 -2
- data/lib/smart_properties/errors.rb +29 -0
- data/lib/smart_properties/version.rb +1 -1
- data/spec/base_spec.rb +15 -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: fbf78f2b4f868f64a7e675336305c7f68579ec33
|
4
|
+
data.tar.gz: 12eeb1777b492bfb56f4d3fe68e0b2fbea222d88
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 67139c47fcc6da96c64d3d4489cfc16144f5cdb8772ece8b96e493a3aed6b127de5197003d7695f1d61365e7b2154a5983c2653c9b33e9529ac3d5f4702293b1
|
7
|
+
data.tar.gz: 926f1575ffe3723a502ae0f2d427df880d638fafc47ff5870a19df87e81103cf435bf4f2931d583a08e9f7095e1ac41e2d2d7fcab6c54b628b728522f6777b64
|
data/lib/smart_properties.rb
CHANGED
@@ -107,10 +107,15 @@ module SmartProperties
|
|
107
107
|
|
108
108
|
##
|
109
109
|
# Implements a key-value enabled constructor that acts as default
|
110
|
-
# constructor for all {SmartProperties}-enabled classes.
|
110
|
+
# constructor for all {SmartProperties}-enabled classes. Positional arguments
|
111
|
+
# or keyword arguments that do not correspond to a property are forwarded to
|
112
|
+
# the super class constructor.
|
111
113
|
#
|
112
114
|
# @param [Hash] attrs the set of attributes that is used for initialization
|
113
115
|
#
|
116
|
+
# @raise [SmartProperties::ConstructorArgumentForwardingError] when unknown arguments were supplied that could not be processed by the super class initializer either.
|
117
|
+
# @raise [SmartProperties::InitializationError] when incorrect values were supplied or required values weren't been supplied.
|
118
|
+
#
|
114
119
|
def initialize(*args, &block)
|
115
120
|
attrs = args.last.is_a?(Hash) ? args.pop.dup : {}
|
116
121
|
properties = self.class.properties
|
@@ -130,7 +135,11 @@ module SmartProperties
|
|
130
135
|
end
|
131
136
|
|
132
137
|
# Call the super constructor and forward unprocessed arguments
|
133
|
-
|
138
|
+
begin
|
139
|
+
attrs.empty? ? super(*args) : super(*args.dup.push(attrs))
|
140
|
+
rescue ArgumentError
|
141
|
+
raise SmartProperties::ConstructorArgumentForwardingError.new(args, attrs)
|
142
|
+
end
|
134
143
|
|
135
144
|
# Execute configuration block
|
136
145
|
block.call(self) if block
|
@@ -13,6 +13,35 @@ module SmartProperties
|
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
+
class ConstructorArgumentForwardingError < Error
|
17
|
+
def initialize(positional_arguments, keyword_arguments)
|
18
|
+
argument_description = [
|
19
|
+
generate_description("positional", positional_arguments.count),
|
20
|
+
generate_description("keyword", keyword_arguments.count)
|
21
|
+
].compact
|
22
|
+
|
23
|
+
arguments = positional_arguments + keyword_arguments.map { |name, value| "#{name}: #{value}" }
|
24
|
+
|
25
|
+
super "Forwarding the following %s failed: %s" % [
|
26
|
+
argument_description.join(" and "),
|
27
|
+
arguments.join(", ")
|
28
|
+
]
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def generate_description(argument_type, argument_number)
|
34
|
+
case argument_number
|
35
|
+
when 0
|
36
|
+
nil
|
37
|
+
when 1
|
38
|
+
"#{argument_type} argument"
|
39
|
+
else
|
40
|
+
"#{argument_number} #{argument_type} arguments"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
16
45
|
class MissingValueError < AssignmentError
|
17
46
|
def initialize(sender, property)
|
18
47
|
super(
|
data/spec/base_spec.rb
CHANGED
@@ -19,6 +19,21 @@ RSpec.describe SmartProperties do
|
|
19
19
|
expect { instance = klass.new(title: BasicObject) }.to_not raise_error
|
20
20
|
end
|
21
21
|
end
|
22
|
+
|
23
|
+
context "the initializer" do
|
24
|
+
it "should raise an ConstructorArgumentForwardingError if provided with an additional positional argument" do
|
25
|
+
unexpected_argument = double("unexpected argument")
|
26
|
+
expect { klass.new(unexpected_argument) }
|
27
|
+
.to raise_error(SmartProperties::ConstructorArgumentForwardingError, /Forwarding the following positional argument failed:.*unexpected argument.*/)
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should raise an ConstructorArgumentForwardingError if provided with unknown keyword arguments" do
|
31
|
+
unexpected_argument1 = double("unexpected argument")
|
32
|
+
unexpected_argument2 = double("unexpected argument")
|
33
|
+
expect { klass.new(first_arg: unexpected_argument1, second_arg: unexpected_argument2) }
|
34
|
+
.to raise_error(SmartProperties::ConstructorArgumentForwardingError, /Forwarding the following 2 keyword arguments failed: first_arg: .*unexpected argument.*, second_arg: .*unexpected argument.*/)
|
35
|
+
end
|
36
|
+
end
|
22
37
|
end
|
23
38
|
|
24
39
|
context "when used to build a class that has a property called title that utilizes the full feature set of SmartProperties" 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.13.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-10-
|
11
|
+
date: 2016-10-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|