smart_properties 1.7.0 → 1.8.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8e43f13ab772a153c4729a1f20be52f140934555
4
- data.tar.gz: 5dab533362b6132ee4b67c4eaef9269d957b4211
3
+ metadata.gz: 863983d7a1707f254f044997c1e35b193bfb2c36
4
+ data.tar.gz: 4853b0a0f314cc9c8f1062d293a5f550fce92f1c
5
5
  SHA512:
6
- metadata.gz: 3b3c1e442d65b38c7571a2380b9bbdc643dd6f3b3d9931be1edeafcd3f9eedf9432e0637ce585e6a2d383274a3090eae0bde5473c461dcb7a1346ff3671769cb
7
- data.tar.gz: 80ebfc6a118202f954e7b347b8685132e23478bbc9f9ef4e8dbd0d34bb5d98f009f71fab9b4f213f9618938c1d62b6de762989b4e89ef9c70224e22468b5e15d
6
+ metadata.gz: 6cb253427617818e2969273311ffaa622289c8420920b7cb98cf981857693a7c0a2a06c8b789b33c1a258e978b4b3372e60d86c58a0e783a2369069637eb8432
7
+ data.tar.gz: d55fbab7960a235c68fcb28765366926de5fccad1daee313b437e135100ef5bb09fd755bbd9b6cffebaebd6bfd7ae5e9178b300ae400ad585ec88d3d67cefcd1
@@ -21,7 +21,7 @@
21
21
  # :required => true
22
22
  #
23
23
  module SmartProperties
24
- VERSION = "1.7.0"
24
+ VERSION = "1.8.0"
25
25
 
26
26
  class Error < ::ArgumentError; end
27
27
  class ConfigurationError < Error; end
@@ -312,8 +312,10 @@ module SmartProperties
312
312
  #
313
313
  # @param [Hash] attrs the set of attributes that is used for initialization
314
314
  #
315
- def initialize(attrs = {}, &block)
316
- attrs ||= {}
315
+ def initialize(*args, &block)
316
+ attrs = args.last.is_a?(Hash) ? args.pop : {}
317
+ super(*args)
318
+
317
319
  properties = self.class.properties.each.to_a
318
320
  missing_properties = []
319
321
 
@@ -1,6 +1,68 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe SmartProperties do
4
+ described_module = self.described_class
5
+
6
+ context 'when extending a subclass' do
7
+ let(:klass) do
8
+ Class.new do
9
+ attr_reader :a, :b
10
+
11
+ def initialize(a = 13, b = nil)
12
+ @a = a
13
+ @b = b
14
+ end
15
+ end
16
+ end
17
+
18
+ let(:subklass) do
19
+ Class.new(klass) do
20
+ include described_module
21
+ property :title
22
+ end
23
+ end
24
+
25
+ context 'when creating new instances' do
26
+ it 'should call the super-class constructor' do
27
+ instance = subklass.new
28
+ expect(instance.a).to eq(13)
29
+ end
30
+
31
+ it 'should handle keyword arguments' do
32
+ instance = subklass.new(:title => 'Lorem ipsum')
33
+ expect(instance.title).to eq('Lorem ipsum')
34
+ end
35
+ end
36
+
37
+ context 'when creating new instances with additional positional arguments' do
38
+ it 'it should forward those arguments to super-class constructor' do
39
+ instance = subklass.new(1, 2)
40
+ expect(instance.a).to eq(1)
41
+ expect(instance.b).to eq(2)
42
+ end
43
+
44
+ it 'should forward arrays as they are to the super-class constructor' do
45
+ instance = subklass.new([1, 2])
46
+ expect(instance.a).to eq([1, 2])
47
+ expect(instance.b).to eq(nil)
48
+ end
49
+
50
+ it 'should handle keyword arguments' do
51
+ instance = subklass.new(1, :title => 'Lorem ipsum')
52
+ expect(instance.a).to eq(1)
53
+ expect(instance.b).to eq(nil)
54
+ expect(instance.title).to eq('Lorem ipsum')
55
+ end
56
+
57
+ it 'should handle blocks' do
58
+ instance = subklass.new(1) { |i| i.title = 'Lorem ipsum' }
59
+ expect(instance.a).to eq(1)
60
+ expect(instance.b).to eq(nil)
61
+ expect(instance.title).to eq('Lorem ipsum')
62
+ end
63
+ end
64
+ end
65
+
4
66
  context "when extending an other class" do
5
67
  subject(:klass) do
6
68
  Class.new.tap do |c|
@@ -1,45 +1,56 @@
1
1
  module Matchers
2
2
  class SmartPropertyMatcher
3
-
4
3
  attr_reader :property_name
5
- attr_reader :instance
4
+ attr_reader :subject
6
5
 
7
6
  def initialize(property_name)
8
7
  @property_name = property_name
9
8
  end
10
9
 
11
- def matches?(instance)
12
- @instance = instance
13
- smart_property_enabled? && is_smart_property?
10
+ def matches?(subject)
11
+ @subject = subject.is_a?(Class) ? subject : subject.class
12
+ smart_property_enabled? && is_smart_property? && getters_and_setters_are_defined?
13
+ end
14
+
15
+ def does_not_match?(subject)
16
+ @subject = subject.is_a?(Class) ? subject : subject.class
17
+ !smart_property_enabled? || !is_smart_property?
18
+ end
19
+
20
+ def description
21
+ "have smart property #{property_name}"
14
22
  end
15
23
 
16
24
  def failure_message
17
- return "expected #{instance.class.name} to have a property named #{property_name}" if smart_property_enabled?
18
- return "expected #{instance.class.name} to be smart property enabled"
25
+ return "expected #{subject.class.name} to have a property named #{property_name}" if smart_property_enabled?
26
+ return "expected #{subject.class.name} to be smart property enabled"
19
27
  end
20
28
 
21
29
  def failure_message_when_negated
22
- "expected #{instance.class.name} to not have a property named #{property_name}"
30
+ "expected #{subject.class.name} to not have a property named #{property_name}"
23
31
  end
24
32
  alias negative_failure_message failure_message_when_negated
25
33
 
26
34
  private
27
35
 
28
- def smart_property_enabled?
29
- instance.ancestors.include?(::SmartProperties)
30
- end
36
+ def smart_property_enabled?
37
+ subject.ancestors.include?(::SmartProperties)
38
+ end
31
39
 
32
- def is_smart_property?
33
- instance.properties[property_name].kind_of?(::SmartProperties::Property)
34
- end
40
+ def is_smart_property?
41
+ subject.properties[property_name].kind_of?(::SmartProperties::Property)
42
+ end
35
43
 
44
+ def getters_and_setters_are_defined?
45
+ methods = subject.instance_methods
46
+ methods.include?(property_name) && methods.include?(:"#{property_name}=")
47
+ end
36
48
  end
37
49
 
38
- def has_smart_property(*args)
50
+ def have_smart_property(*args)
39
51
  SmartPropertyMatcher.new(*args)
40
52
  end
41
-
42
- alias :have_smart_property :has_smart_property
53
+ alias has_smart_property have_smart_property
43
54
  end
44
55
 
45
56
  RSpec.configure do |spec|
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.7.0
4
+ version: 1.8.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: 2015-09-11 00:00:00.000000000 Z
11
+ date: 2015-09-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec