louche 0.1 → 0.1.1

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: 4fb1ff2a90ab738a9cc157e1941ad7f81a8766db
4
- data.tar.gz: 5cfc8efcf777ef57f988c788715ad197549ba682
3
+ metadata.gz: 839b489c21dd4febb6445eac53431b1311f963c8
4
+ data.tar.gz: cd5ff1e1590caae21eccbc39827ef606a827fd9e
5
5
  SHA512:
6
- metadata.gz: b2485bba9066234d23c656ea1b50676737eaeda547f60e0c7b08d2e842e36baf05a8eb3efdc8a14c66e14ae94a8a1ad6447224e129c0f5991d02296e95ebd630
7
- data.tar.gz: 1442e35ad3da1b2fa5029fcc13e3fffe7d2726f4bae934d0fb2b3083b18a338f5531bd2b3aabd53d737906c80254ff796ccb25b2de9ef49f2ee7a11503b988fe
6
+ metadata.gz: 031b9b212bb204a480757619a23cc2461d7cade08399be80805db6842d1e545d29676d02640dff102d2eaa78902e5f06e46741556ee4739641f7a1343bbc008b
7
+ data.tar.gz: a7376d5cc340e05ec58296b91e33ffc842d0a48eaa7a29bb1dfdec7a0fed541c7c26acfe09a7f9647e3c8794f728667dafc8c4c2243f1c42e53ab9526a9434f1
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  <p align="center">
2
- <a href="https://github.com/mirego/microscope">
3
- <img src="http://i.imgur.com/FhNZIjg.png" alt="Louche" />
2
+ <a href="https://github.com/mirego/louche">
3
+ <img src="http://i.imgur.com/qCu7dpr.png" alt="Louche" />
4
4
  </a>
5
5
  <br />
6
6
  Louche adds common validators for ActiveModel/ActiveRecord classes.
@@ -25,6 +25,8 @@ Louche provides a few validators to use in your ActiveModel/ActiveRecord classes
25
25
 
26
26
  ### `EmailValidator`
27
27
 
28
+ #### Example
29
+
28
30
  ```ruby
29
31
  class User < ActiveRecord::Base
30
32
  validates :email, email: true
@@ -34,8 +36,17 @@ User.new(email: 'foo@example.com').valid? # => true
34
36
  User.new(email: 'foo@example').valid? # => false
35
37
  ```
36
38
 
39
+ #### Options
40
+
41
+ | Option | Description
42
+ |------------|-----------------------------------------------------
43
+ | `:regex` | The regex used to validate the email (default: `/\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i`)
44
+ | `:message` | The ActiveRecord message added to the record errors (default: `:invalid_email`)
45
+
37
46
  ### `URLValidator`
38
47
 
48
+ #### Example
49
+
39
50
  ```ruby
40
51
  class User < ActiveRecord::Base
41
52
  validates :website, url: true
@@ -45,8 +56,17 @@ User.new(website: 'http://example.com').valid? # => true
45
56
  User.new(website: 'example.$$$').valid? # => false
46
57
  ```
47
58
 
59
+ #### Options
60
+
61
+ | Option | Description
62
+ |------------|-----------------------------------------------------
63
+ | `:schemes` | The URI schemes to allow (default: `%w(http https)`)
64
+ | `:message` | The ActiveRecord message added to the record errors (default: `:invalid_url`)
65
+
48
66
  ### `PhoneNumberValidator`
49
67
 
68
+ #### Example
69
+
50
70
  ```ruby
51
71
  class User < ActiveRecord::Base
52
72
  validates :phone_number, phone_number: true
@@ -61,8 +81,18 @@ user.valid? # => false
61
81
  user.phone_number # '5552525'
62
82
  ```
63
83
 
84
+ #### Options
85
+
86
+ | Option | Description
87
+ |------------------|-----------------------------------------------------
88
+ | `:regex` | The regex used to validate the number (default: `/\A\d{10,}\z/`)
89
+ | `:cleanup_regex` | The regex used to validate clean the input before validating/saving it (default: `/[^\d]/`)
90
+ | `:message` | The ActiveRecord message added to the record errors (default: `:invalid_phone_number`)
91
+
64
92
  ### `PostalCodeValidator`
65
93
 
94
+ #### Example
95
+
66
96
  ```ruby
67
97
  class User < ActiveRecord::Base
68
98
  validates :postal_code, postal_code: true
@@ -77,8 +107,18 @@ user.valid? # => false
77
107
  user.postal_code # => 'L0L'
78
108
  ```
79
109
 
110
+ #### Options
111
+
112
+ | Option | Description
113
+ |------------------|-----------------------------------------------------
114
+ | `:regex` | The regex used to validate the code (default: `/\A[a-z]\d[a-z]\d[a-z]\d\z/i`)
115
+ | `:cleanup_regex` | The regex used to validate clean the input before validating/saving it (default: `/[^a-z0-9]/i`)
116
+ | `:message` | The ActiveRecord message added to the record errors (default: `:invalid_postal_code`)
117
+
80
118
  ### `ArrayValidator`
81
119
 
120
+ #### Example
121
+
82
122
  ```ruby
83
123
  class Tag < Struct.new(:name)
84
124
  def valid?
@@ -98,6 +138,29 @@ User.new(tags: ['food', 'beer', 'code']).valid? # => true
98
138
  User.new(tags: ['food', '', 'code']).valid? # => false
99
139
  ```
100
140
 
141
+ #### Options
142
+
143
+ | Option | Description
144
+ |--------------------|-----------------------------------------------------
145
+ | `:message` | The ActiveRecord message added to the record errors (default: `:invalid_array`)
146
+ | `:validity_method` | The method that will be sent to each array item (default: `:valid?`)
147
+
148
+ ## Localized error messages
149
+
150
+ Louche uses standard ActiveRecord messages. Here’s what your
151
+ `config/locales/activerecord.en.yml` file could look like:
152
+
153
+ ```yaml
154
+ en:
155
+ errors:
156
+ messages:
157
+ invalid_email: is not a valid email address
158
+ invalid_url: is not a valid URL
159
+ invalid_phone_number: is not a valid phone number
160
+ invalid_postal_code: is not a valid postal code
161
+ invalid_array: contains invalid items
162
+ ```
163
+
101
164
  ## License
102
165
 
103
166
  `Louche` is © 2014 [Mirego](http://www.mirego.com) and may be freely distributed under the [New BSD license](http://opensource.org/licenses/BSD-3-Clause). See the [`LICENSE.md`](https://github.com/mirego/louche/blob/master/LICENSE.md) file.
@@ -1,12 +1,14 @@
1
1
  class ArrayValidator < ActiveModel::EachValidator
2
2
  def initialize(options)
3
3
  options.reverse_merge!(message: :invalid_array)
4
+ options.reverse_merge!(validity_method: :valid?)
4
5
  super
5
6
  end
6
7
 
7
8
  def validate_each(record, attribute, value)
8
9
  if value.is_a?(Array)
9
- add_error(record, attribute, value) if value.empty? || !value.map.all?(&:valid?)
10
+ validity_method = options.fetch(:validity_method)
11
+ add_error(record, attribute, value) if value.empty? || !value.map.all?(&validity_method)
10
12
  else
11
13
  add_error(record, attribute, value)
12
14
  end
@@ -1,7 +1,7 @@
1
1
  class PhoneNumberValidator < ActiveModel::EachValidator
2
2
  def initialize(options)
3
3
  options.reverse_merge!(message: :invalid_phone_number)
4
- options.reverse_merge!(regex: /\d{10,}/)
4
+ options.reverse_merge!(regex: /\A\d{10,}\z/)
5
5
  options.reverse_merge!(cleanup_regex: /[^\d]/)
6
6
  super
7
7
  end
@@ -1,7 +1,7 @@
1
1
  class PostalCodeValidator < ActiveModel::EachValidator
2
2
  def initialize(options)
3
3
  options.reverse_merge!(message: :invalid_postal_code)
4
- options.reverse_merge!(regex: /^[a-z]\d[a-z]\d[a-z]\d$/i)
4
+ options.reverse_merge!(regex: /\A[a-z]\d[a-z]\d[a-z]\d\z/i)
5
5
  options.reverse_merge!(cleanup_regex: /[^a-z0-9]/i)
6
6
  super
7
7
  end
@@ -1,3 +1,3 @@
1
1
  module Louche
2
- VERSION = '0.1'
2
+ VERSION = '0.1.1'
3
3
  end
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  describe ArrayValidator do
4
4
  describe :validate_each do
5
5
  let(:validator) { described_class.new(options) }
6
- let(:options) { { attributes: [attribute] } }
6
+ let(:options) { { attributes: [attribute], validity_method: validity_method } }
7
7
  let(:record) { double('ActiveRecord::Base', errors: errors) }
8
8
  let(:errors) { double('ActiveModel::Errors') }
9
9
  let(:attribute) { :items }
@@ -21,29 +21,60 @@ describe ArrayValidator do
21
21
  specify { validator.validate_each(record, attribute, value) }
22
22
  end
23
23
 
24
- let(:valid_item) { double('ArrayItem', valid?: true) }
25
- let(:invalid_item) { double('ArrayItem', valid?: false) }
24
+ context 'with default validity method' do
25
+ let(:validity_method) { :valid? }
26
+ let(:valid_item) { double('ArrayItem', valid?: true) }
27
+ let(:invalid_item) { double('ArrayItem', valid?: false) }
26
28
 
27
- context 'with an invalid value' do
28
- context 'with non-Array valud' do
29
- let(:value) { valid_item }
30
- it_behaves_like 'an erroneous validator'
31
- end
29
+ context 'with an invalid value' do
30
+ context 'with non-Array value' do
31
+ let(:value) { valid_item }
32
+ it_behaves_like 'an erroneous validator'
33
+ end
34
+
35
+ context 'with value containing invalid item' do
36
+ let(:value) { [valid_item, invalid_item] }
37
+ it_behaves_like 'an erroneous validator'
38
+ end
32
39
 
33
- context 'with value containing invalid item' do
34
- let(:value) { [valid_item, invalid_item] }
35
- it_behaves_like 'an erroneous validator'
40
+ context 'with empty value' do
41
+ let(:value) { [] }
42
+ it_behaves_like 'an erroneous validator'
43
+ end
36
44
  end
37
45
 
38
- context 'with empty value' do
39
- let(:value) { [] }
40
- it_behaves_like 'an erroneous validator'
46
+ context 'with valid value' do
47
+ let(:value) { [valid_item, valid_item, valid_item] }
48
+ it_behaves_like 'a successful validator'
41
49
  end
42
50
  end
43
51
 
44
- context 'with valid value' do
45
- let(:value) { [valid_item, valid_item, valid_item] }
46
- it_behaves_like 'a successful validator'
52
+ context 'with custom validity method' do
53
+ let(:validity_method) { :really_valid? }
54
+ let(:valid_item) { double('ArrayItem', really_valid?: true) }
55
+ let(:invalid_item) { double('ArrayItem', really_valid?: false) }
56
+
57
+ context 'with an invalid value' do
58
+ context 'with non-Array value' do
59
+ let(:value) { valid_item }
60
+ it_behaves_like 'an erroneous validator'
61
+ end
62
+
63
+ context 'with value containing invalid item' do
64
+ let(:value) { [valid_item, invalid_item] }
65
+ it_behaves_like 'an erroneous validator'
66
+ end
67
+
68
+ context 'with empty value' do
69
+ let(:value) { [] }
70
+ it_behaves_like 'an erroneous validator'
71
+ end
72
+ end
73
+
74
+ context 'with valid value' do
75
+ let(:value) { [valid_item, valid_item, valid_item] }
76
+ it_behaves_like 'a successful validator'
77
+ end
47
78
  end
48
79
  end
49
80
  end
@@ -51,6 +51,12 @@ describe PhoneNumberValidator do
51
51
 
52
52
  it_behaves_like 'a successful validator'
53
53
  end
54
+
55
+ context 'containing trailing characters' do
56
+ let(:value) { ' 514 555-2525 ' }
57
+ let(:expected_stored_value) { '5145552525' }
58
+ it_behaves_like 'a successful validator'
59
+ end
54
60
  end
55
61
  end
56
62
  end
@@ -53,6 +53,13 @@ describe PostalCodeValidator do
53
53
 
54
54
  it_behaves_like 'a successful validator'
55
55
  end
56
+
57
+ context 'with trailing characters' do
58
+ let(:value) { ' G0R2T0 ' }
59
+ let(:expected_stored_value) { 'G0R2T0' }
60
+
61
+ it_behaves_like 'a successful validator'
62
+ end
56
63
  end
57
64
  end
58
65
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: louche
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rémi Prévost
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-01 00:00:00.000000000 Z
11
+ date: 2014-05-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel