scrivener_errors 0.0.3

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cc214a28a79f0457445e3d49ccc42d79a3cba275
4
+ data.tar.gz: aca56643715833b6c0a29615df33c7d8caac0d7d
5
+ SHA512:
6
+ metadata.gz: 43443a3c67fe493225a1bdc93ee9a3671621d1a3eb337a973a703c3403e656374b56fc5721936e8ff23cb467eedc71acf0cfa7c3a8e341ab982cbf46ecebd82d
7
+ data.tar.gz: a2d94fe0bec84d0ec589a50fbbccb24685ae84754509610b73d2c7e01e2dde263f518c3c9a1253e7899ca3490ad7f64b524174b574dbc04a6d1e03defd682c40
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2014 Brendon Murphy
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,56 @@
1
+ Scrivener Errors
2
+ ================
3
+
4
+ Basic error messages for [Scrivener](https://github.com/soveran/scrivener)
5
+ filters.
6
+
7
+ Description
8
+ -----------
9
+
10
+ Scrivener errors are returned has a hash with a format like:
11
+
12
+ ```ruby
13
+ {:email => [:not_email], :password => [:not_present]}
14
+ ```
15
+
16
+ This can be represented instead as a string like:
17
+
18
+ ```
19
+ Email is not an email, password can't be blank
20
+ ```
21
+
22
+ ScrivenerErrors will present these error hashes as a string
23
+ or list of errors message for use in failure notices or
24
+ form errors.
25
+
26
+ This also comes with a small plugin for [Cuba](http://cuba.is/)
27
+ to get the error message easily, `ScrivenerErrors::Helpers`.
28
+
29
+ Installation
30
+ ------------
31
+
32
+ $ gem install scrivener_errors
33
+
34
+ Usage
35
+ -----
36
+
37
+ ```ruby
38
+ # Setup the plugin for Cuba
39
+ Cuba.plugin ScrivenerErrors::Helpers
40
+
41
+ # Inside a Cuba action
42
+ filter = Signup.new(req.params)
43
+ if !filter.valid?
44
+ session[:error] = filter_errors(filter)
45
+ res.redirect "/some_form"
46
+ end
47
+ ```
48
+
49
+ Notes
50
+ -----
51
+
52
+ Currently there's no I18n in use. The error messages are
53
+ from a small English mapped hash at the moment.
54
+
55
+ An error type that is not found will be represented like
56
+ "Password is invalid" by default.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ task :test do
2
+ require "cutest"
3
+ Cutest.run(Dir["test/*.rb"])
4
+ end
5
+
6
+ task :default => :test
@@ -0,0 +1,50 @@
1
+ class ScrivenerErrors
2
+ attr_reader :scrivener
3
+
4
+ MESSAGES = {
5
+ :not_email => "is not an email",
6
+ :not_decimal => "is not a decimal",
7
+ :not_in_range => "has invalid length",
8
+ :not_numeric => "is not a number",
9
+ :not_present => "can't be blank",
10
+ :not_url => "is not a url",
11
+ :too_short => "is too short"
12
+ }.freeze
13
+
14
+ def initialize(scrivener)
15
+ @scrivener = scrivener
16
+ ensure_validated
17
+ end
18
+
19
+ def ensure_validated
20
+ if scrivener.errors == {}
21
+ scrivener.valid?
22
+ end
23
+ end
24
+
25
+ def to_s
26
+ messages.join(', ').capitalize
27
+ end
28
+ alias :message :to_s
29
+
30
+ def messages
31
+ scrivener.errors.inject([]) do |memo, error|
32
+ att = error[0]
33
+ memo.concat error[1].map {|e| error_string(att, e) }
34
+ memo
35
+ end
36
+ end
37
+
38
+ def error_string(att, error)
39
+ att = att.to_s.tr('_', ' ')
40
+ failure = MESSAGES.fetch(error, 'is invalid')
41
+
42
+ [att, failure].join(' ')
43
+ end
44
+
45
+ module Helpers
46
+ def filter_errors(scrivener)
47
+ ScrivenerErrors.new(scrivener).to_s
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,22 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "scrivener_errors"
3
+ s.version = "0.0.3"
4
+ s.summary = "Basic error messages for Scrivener filters"
5
+ s.description = s.summary
6
+ s.authors = ["Brendon Murphy"]
7
+ s.email = ["xternal1+github@gmail.com"]
8
+ s.homepage = "http://github.com/bemurphy/scrivener_errors"
9
+ s.license = "MIT"
10
+
11
+ s.files = Dir[
12
+ "LICENSE",
13
+ "README.md",
14
+ "Rakefile",
15
+ "lib/**/*.rb",
16
+ "*.gemspec",
17
+ "test/**/*.rb"
18
+ ]
19
+
20
+ s.add_development_dependency "cutest", "~> 1.2"
21
+ s.add_development_dependency "scrivener", "~> 0.2"
22
+ end
@@ -0,0 +1,74 @@
1
+ require_relative "../lib/scrivener_errors"
2
+ require "scrivener"
3
+
4
+ class SignUp < Scrivener
5
+ attr_accessor :email, :password, :password_confirmation
6
+
7
+ def validate
8
+ assert_email :email
9
+
10
+ if assert_present :password
11
+ assert_length :password, (8..99)
12
+ assert(password == password_confirmation,
13
+ [:password_confirmation, :not_confirmed])
14
+ end
15
+ end
16
+ end
17
+
18
+ scope do
19
+ test "error messages" do
20
+ filter = SignUp.new(email: 'john',
21
+ password: 'password',
22
+ password_confirmation: 'p4ssw3rd')
23
+
24
+ messages = ScrivenerErrors.new(filter).messages
25
+ assert_equal "email is not an email", messages[0]
26
+ assert_equal "password confirmation is invalid", messages[1]
27
+ end
28
+
29
+ test "multiple errors on a single attribute" do
30
+ filter = SignUp.new(email: 'john@example.com',
31
+ password: 'x',
32
+ password_confirmation: 'y')
33
+
34
+ messages = ScrivenerErrors.new(filter).messages
35
+ assert_equal "password has invalid length", messages[0]
36
+ assert_equal "password confirmation is invalid", messages[1]
37
+ end
38
+
39
+ test "no errors" do
40
+ filter = SignUp.new(email: 'john@example.com',
41
+ password: 'password',
42
+ password_confirmation: 'password')
43
+
44
+ messages = ScrivenerErrors.new(filter).messages
45
+ assert_equal [], messages
46
+ end
47
+
48
+ test "converting to a string" do
49
+ filter = SignUp.new(email: 'john',
50
+ password: 'password',
51
+ password_confirmation: 'y')
52
+
53
+ assert_equal(
54
+ "Email is not an email, password confirmation is invalid",
55
+ ScrivenerErrors.new(filter).message
56
+ )
57
+ end
58
+ end
59
+
60
+ scope do
61
+ test "filter errors to a string" do
62
+ object = Object.new
63
+ object.extend ScrivenerErrors::Helpers
64
+
65
+ filter = SignUp.new(email: 'john',
66
+ password: 'password',
67
+ password_confirmation: 'y')
68
+
69
+ assert_equal(
70
+ "Email is not an email, password confirmation is invalid",
71
+ object.filter_errors(filter)
72
+ )
73
+ end
74
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: scrivener_errors
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Brendon Murphy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: cutest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.2'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: scrivener
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.2'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.2'
41
+ description: Basic error messages for Scrivener filters
42
+ email:
43
+ - xternal1+github@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - LICENSE
49
+ - README.md
50
+ - Rakefile
51
+ - lib/scrivener_errors.rb
52
+ - scrivener_errors.gemspec
53
+ - test/scrivener_errors_test.rb
54
+ homepage: http://github.com/bemurphy/scrivener_errors
55
+ licenses:
56
+ - MIT
57
+ metadata: {}
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 2.2.2
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: Basic error messages for Scrivener filters
78
+ test_files: []