shoulda-lotus 0.0.3 → 0.0.4

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: d8a42596ac1614c3d15f72c7ba3dc69f4d129f7f
4
- data.tar.gz: 5ee02bf7f61a5b4cda9a6528d675b71433fd32cb
3
+ metadata.gz: 54b064ad8875c42790c76901ea41644227071bc4
4
+ data.tar.gz: 85413d801d4d8056555300361ac7da0a124e8a8d
5
5
  SHA512:
6
- metadata.gz: d1761144e81387b172e85c8b72b2e8598defe3ac1b61794bc3b431eb6d21d7852ef63b3bb4389beb4aeab90d54d4513b5fbaabef3191edfc50f93711971565b2
7
- data.tar.gz: 6b1e164632a8bcf75d4b78f624ce292dbc44aa6b3ab5145b6b69025846f90601d9c182f98277c87148649406035ca5c2ded8dfebb6b04224f05fca4469a2f90c
6
+ metadata.gz: 26cdd15d98c2cd5bd8ace8ac7f05f281cb18417618e1ddcc2056e1d0f2206e84b20e177cfd6a4c28c68d795177f5e8cab5e6cf38deb9ad5e4c7b661216d1eaad
7
+ data.tar.gz: 702ddcd98f91cee3c9fbc3e27c1d6a7439d59ec9f6cd6605c80848bfbd02376920a452bf4696ff5a69a10407373ab796af1e2a1d29518073eec8fab6889f5116
data/README.md CHANGED
@@ -27,9 +27,11 @@ Or install it yourself as:
27
27
  class Person
28
28
  include Lotus::Validations
29
29
 
30
- attribute :email, presence: true, format: /@/
31
- attribute :name, size: 5..50
32
- attribute :password, size: 10
30
+ attribute :email, type: String, presence: true, format: /@/
31
+ attribute :name, type: String, size: 5..50
32
+ attribute :password, type: String, size: 10
33
+ attribute :birthday, type: Date
34
+ attribute :created_at, type: DateTime
33
35
  end
34
36
  ```
35
37
 
@@ -45,6 +47,13 @@ it { is_expected.to validate_presence_of(:email) }
45
47
  # size
46
48
  it { is_expected.to validate_length(:name).is_at_least(5).is_at_most(50) }
47
49
  it { is_expected.to validate_length(:password).is_equal_to(10) }
50
+
51
+ # coerces
52
+ it { is_expected.to coerce_attribute(:email).to(String) }
53
+ it { is_expected.to coerce_attribute(:name).to(String) }
54
+ it { is_expected.to coerce_attribute(:password).to(String) }
55
+ it { is_expected.to coerce_attribute(:birthday).to(Date) }
56
+ it { is_expected.to coerce_attribute(:created_at).to(DateTime) }
48
57
  ```
49
58
 
50
59
  ## Contributing
@@ -11,11 +11,8 @@ module Shoulda
11
11
  end
12
12
 
13
13
  def matches?(target)
14
- @target = target
15
- @target.send("#{@attribute}=", @value)
16
- @target.valid?
17
-
18
- !(@target.errors.for(@attribute).select { |error| error.attribute == @attribute.to_s && error.validation == :format }.size > 0)
14
+ target.send("#{@attribute}=", @value)
15
+ !Matcher.new(target, @attribute, :format).matches?
19
16
  end
20
17
 
21
18
  def description
@@ -0,0 +1,53 @@
1
+ module Shoulda
2
+ module Lotus
3
+ module Matchers
4
+ def coerce_attribute(attribute)
5
+ CoerceAttributeMatcher.new(attribute)
6
+ end
7
+
8
+ class CoerceAttributeMatcher
9
+ TYPES = {
10
+ 'Array' => [1, [1]],
11
+ 'BigDecimal' => ['x', nil],
12
+ 'Boolean' => ['0303', false],
13
+ 'Date' => [false, nil],
14
+ 'DateTime' => ['x', nil],
15
+ 'Float' => ['x', nil],
16
+ 'Hash' => ['ç', nil],
17
+ 'Integer' => ['x', nil],
18
+ 'Pathname' => [true, nil],
19
+ 'Set' => [nil, Set.new],
20
+ 'String' => [1, '1'],
21
+ 'Symbol' => [Hash.new, nil],
22
+ 'Time' => ['uy', nil],
23
+ }
24
+
25
+ def initialize(attribute)
26
+ @attribute = attribute
27
+ end
28
+
29
+ def matches?(target)
30
+ target.send("#{@attribute}=", TYPES[@type.to_s].first)
31
+ target.send(@attribute) == TYPES[@type.to_s].last
32
+ end
33
+
34
+ def description
35
+ "coerce '#{@attribute}' to '#{@type}'"
36
+ end
37
+
38
+ def failure_message
39
+ "does coerce '#{@attribute}' to '#{@type}'"
40
+ end
41
+
42
+ def failure_message_when_negated
43
+ "does not coerce '#{@attribute}' to '#{@type}'"
44
+ end
45
+
46
+ def to(type)
47
+ @type = type
48
+ self
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -13,15 +13,11 @@ module Shoulda
13
13
  def matches?(target)
14
14
  errors = []
15
15
 
16
- @target = target
16
+ target.send("#{@attribute}=", '*' * (minimum - 1))
17
+ errors << Matcher.new(target, @attribute, :size).matches?
17
18
 
18
- @target.send("#{@attribute}=", '*' * (minimum - 1))
19
- @target.valid?
20
- errors << (@target.errors.for(@attribute).select { |error| error.attribute == @attribute.to_s && error.validation == :size }.size > 0)
21
-
22
- @target.send("#{@attribute}=", '*' * (maximum + 1))
23
- @target.valid?
24
- errors << (@target.errors.for(@attribute).select { |error| error.attribute == @attribute.to_s && error.validation == :size }.size > 0)
19
+ target.send("#{@attribute}=", '*' * (maximum + 1))
20
+ errors << Matcher.new(target, @attribute, :size).matches?
25
21
 
26
22
  errors.all? { |error| error }
27
23
  end
@@ -11,10 +11,7 @@ module Shoulda
11
11
  end
12
12
 
13
13
  def matches?(target)
14
- @target = target
15
- @target.valid?
16
-
17
- @target.errors.for(@attribute).select { |error| error.attribute == @attribute.to_s && error.validation == :presence }.size > 0
14
+ Matcher.new(target, @attribute, :presence).matches?
18
15
  end
19
16
 
20
17
  def description
@@ -1,3 +1,4 @@
1
+ require 'shoulda/lotus/matchers/coerce_attribute_matcher'
1
2
  require 'shoulda/lotus/matchers/allow_value_matcher'
2
3
  require 'shoulda/lotus/matchers/validate_length_of_matcher'
3
4
  require 'shoulda/lotus/matchers/validate_presence_of_matcher'
@@ -5,6 +6,18 @@ require 'shoulda/lotus/matchers/validate_presence_of_matcher'
5
6
  module Shoulda
6
7
  module Lotus
7
8
  module Matchers
9
+ class Matcher
10
+ def initialize(target, attribute, validation)
11
+ @target = target
12
+ @attribute = attribute
13
+ @validation = validation
14
+ end
15
+
16
+ def matches?
17
+ @target.valid?
18
+ @target.errors.for(@attribute).select { |error| error.attribute == @attribute.to_s && error.validation == @validation }.size > 0
19
+ end
20
+ end
8
21
  end
9
22
  end
10
23
  end
@@ -1,5 +1,5 @@
1
1
  module Shoulda
2
2
  module Lotus
3
- VERSION = '0.0.3'.freeze
3
+ VERSION = '0.0.4'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shoulda-lotus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Leonardo Saraiva
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-06-30 00:00:00.000000000 Z
11
+ date: 2015-07-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -97,6 +97,7 @@ files:
97
97
  - lib/shoulda/lotus.rb
98
98
  - lib/shoulda/lotus/matchers.rb
99
99
  - lib/shoulda/lotus/matchers/allow_value_matcher.rb
100
+ - lib/shoulda/lotus/matchers/coerce_attribute_matcher.rb
100
101
  - lib/shoulda/lotus/matchers/validate_length_of_matcher.rb
101
102
  - lib/shoulda/lotus/matchers/validate_presence_of_matcher.rb
102
103
  - lib/shoulda/lotus/version.rb
@@ -126,4 +127,3 @@ signing_key:
126
127
  specification_version: 4
127
128
  summary: Making tests easy on the fingers and eyes, but on lotus.
128
129
  test_files: []
129
- has_rdoc: