smart_properties 1.12.0 → 1.13.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 308f2acd5b26ff6c0d5f008835d1ed86ba18218c
4
- data.tar.gz: d3744f7a5be843235043d5f04d290693016e0877
3
+ metadata.gz: fbf78f2b4f868f64a7e675336305c7f68579ec33
4
+ data.tar.gz: 12eeb1777b492bfb56f4d3fe68e0b2fbea222d88
5
5
  SHA512:
6
- metadata.gz: 7f62e2aee8c528d1d6387e5e51594a998e6ef81c9afdc17935e51389c774f769eaeadc7cd3d55b0b7242c50730be371d2c843f26a4ccfe4e0d34c9f5fb72bb35
7
- data.tar.gz: 34fc69a7afb5635f32201fb8e3c9ca132177fe628ffcd3b7b3f11462dd1901360041234542dc9998b8fd04ab8b62593ed07308b834d5f482152fd17955150435
6
+ metadata.gz: 67139c47fcc6da96c64d3d4489cfc16144f5cdb8772ece8b96e493a3aed6b127de5197003d7695f1d61365e7b2154a5983c2653c9b33e9529ac3d5f4702293b1
7
+ data.tar.gz: 926f1575ffe3723a502ae0f2d427df880d638fafc47ff5870a19df87e81103cf435bf4f2931d583a08e9f7095e1ac41e2d2d7fcab6c54b628b728522f6777b64
@@ -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
- attrs.empty? ? super(*args) : super(*args.push(attrs))
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(
@@ -1,3 +1,3 @@
1
1
  module SmartProperties
2
- VERSION = "1.12.0"
2
+ VERSION = "1.13.0"
3
3
  end
@@ -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.12.0
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 00:00:00.000000000 Z
11
+ date: 2016-10-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec