scrivener-contrib 0.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4b0e0e8bb3edbf786a2685077c97975d6678df8c
4
+ data.tar.gz: 39770c59910b79b739c68f4dd10e8f73e10ccf93
5
+ SHA512:
6
+ metadata.gz: da15d0649cdb1cbc2b2ee774f58340b9d745974bad89bfe63603b51be6be0f2fd071add0cdda26ba738ae5879297e743d8af652807bf51d225ebd09b80cd38ff
7
+ data.tar.gz: 63de9e003fcaa2240068522e53a274221b601709d3760ede9358805cff8bcb29d73379a77d6eb8621649c0ae8e412fbd5046bab8b97db5504a537ff720a698bb
data/.gems ADDED
@@ -0,0 +1,3 @@
1
+ cutest -v 1.2.1
2
+ ohm -v 2.0.0.rc1
3
+ scrivener -v 0.2.0
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ /pkg
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ scrivener-contrib
2
+ =================
3
+
4
+ Extra validations for Scrivener
data/UNLICENSE ADDED
@@ -0,0 +1,24 @@
1
+ This is free and unencumbered software released into the public domain.
2
+
3
+ Anyone is free to copy, modify, publish, use, compile, sell, or
4
+ distribute this software, either in source code form or as a compiled
5
+ binary, for any purpose, commercial or non-commercial, and by any
6
+ means.
7
+
8
+ In jurisdictions that recognize copyright laws, the author or authors
9
+ of this software dedicate any and all copyright interest in the
10
+ software to the public domain. We make this dedication for the benefit
11
+ of the public at large and to the detriment of our heirs and
12
+ successors. We intend this dedication to be an overt act of
13
+ relinquishment in perpetuity of all present and future rights to this
14
+ software under copyright law.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ For more information, please refer to <http://unlicense.org/>
@@ -0,0 +1,25 @@
1
+ require_relative "ohm" if defined?(Ohm)
2
+
3
+ class Scrivener
4
+ module Validations
5
+ def assert_confirmation(att, error = [att, :not_confirmed])
6
+ confirmation = :"#{att}_confirmation"
7
+
8
+ if assert_present(confirmation, error)
9
+ assert(send(confirmation) == send(att), error)
10
+ end
11
+ end
12
+
13
+ def assert_minimum_length(att, val, error = [att, :too_short])
14
+ assert(send(att).to_s.length >= val, error)
15
+ end
16
+
17
+ def assert_maximum_length(att, val, error = [att, :too_long])
18
+ assert(send(att).to_s.length <= val, error)
19
+ end
20
+
21
+ def assert_exact_length(att, val, error = [att, :wrong_length])
22
+ assert(send(att).to_s.length == val, error)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,20 @@
1
+ class Scrivener
2
+ module Validations
3
+ def assert_unique(att, model, error = [att, :not_unique])
4
+ model = Utils.const(self.class, model)
5
+ record = model.with(att, send(att))
6
+
7
+ assert(record.nil?, error)
8
+ end
9
+
10
+ module Utils # :nodoc:
11
+ def self.const(context, name)
12
+ case name
13
+ when Symbol, String
14
+ context.const_get(name)
15
+ else name
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
data/makefile ADDED
@@ -0,0 +1,4 @@
1
+ .PHONY: test
2
+
3
+ test:
4
+ cutest test/*.rb
@@ -0,0 +1,14 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "scrivener-contrib"
3
+ s.version = "0.0.1"
4
+ s.summary = "Extra validation helpers for Scrivener."
5
+ s.description = s.summary
6
+ s.authors = ["Francesco Rodríguez"]
7
+ s.email = ["lrodriguezsanc@gmail.com"]
8
+ s.homepage = "https://github.com/frodsan/scrivener-contrib"
9
+ s.license = "Unlicense"
10
+
11
+ s.files = `git ls-files`.split("\n")
12
+
13
+ s.add_development_dependency "cutest"
14
+ end
@@ -0,0 +1,31 @@
1
+ require_relative "helper"
2
+
3
+ class Signup < Scrivener
4
+ attr_accessor :password
5
+ attr_accessor :password_confirmation
6
+
7
+ def validate
8
+ assert_confirmation :password
9
+ end
10
+ end
11
+
12
+ test "invalid if confirmation is nil" do
13
+ signup = Signup.new({})
14
+
15
+ assert !(signup.valid?)
16
+ assert_equal [:not_confirmed], signup.errors[:password]
17
+ end
18
+
19
+ test "invalid if confirmation and attribute are not equal" do
20
+ signup = Signup.new(password: "!", password_confirmation: "?")
21
+
22
+ assert !(signup.valid?)
23
+ assert_equal [:not_confirmed], signup.errors[:password]
24
+ end
25
+
26
+ test "valid if confirmation and attribute are equal" do
27
+ signup = Signup.new(password: "!", password_confirmation: "!")
28
+
29
+ assert signup.valid?
30
+ assert signup.errors.empty?
31
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,3 @@
1
+ require "cutest"
2
+ require "scrivener"
3
+ require_relative "../lib/scrivener/contrib"
data/test/length.rb ADDED
@@ -0,0 +1,47 @@
1
+ require_relative "helper"
2
+
3
+ MINIMUM = "*" * 10
4
+ MAXIMUM = "*" * 20
5
+ EXACT = "*" * 8
6
+
7
+ class Signup < Scrivener
8
+ attr_accessor :fname
9
+ attr_accessor :lname
10
+ attr_accessor :passport
11
+
12
+ def validate
13
+ assert_minimum_length :fname, MINIMUM.length
14
+ assert_maximum_length :lname, MAXIMUM.length
15
+ assert_exact_length :passport, EXACT.length
16
+ end
17
+ end
18
+
19
+ setup do
20
+ Signup.new(fname: MINIMUM, lname: MAXIMUM, passport: EXACT)
21
+ end
22
+
23
+ test "valid" do |signup|
24
+ assert signup.valid?
25
+ assert signup.errors.empty?
26
+ end
27
+
28
+ test "invalid minimum length" do |signup|
29
+ signup.fname = ""
30
+
31
+ assert !(signup.valid?)
32
+ assert_equal [:too_short], signup.errors[:fname]
33
+ end
34
+
35
+ test "invalid maximum length" do |signup|
36
+ signup.lname = MAXIMUM + "*"
37
+
38
+ assert !(signup.valid?)
39
+ assert_equal [:too_long], signup.errors[:lname]
40
+ end
41
+
42
+ test "invalid exact length" do |signup|
43
+ signup.passport = ""
44
+
45
+ assert !(signup.valid?)
46
+ assert_equal [:wrong_length], signup.errors[:passport]
47
+ end
data/test/ohm.rb ADDED
@@ -0,0 +1,36 @@
1
+ require "ohm"
2
+ require_relative "helper"
3
+
4
+ Ohm.redis = Redic.new("redis://localhost:6379/30")
5
+
6
+ class User < Ohm::Model
7
+ attribute :email
8
+ unique :email
9
+ end
10
+
11
+ class Signup < Scrivener
12
+ attr_accessor :email
13
+
14
+ def validate
15
+ assert_unique :email, :User
16
+ end
17
+ end
18
+
19
+ prepare do
20
+ Ohm.flush
21
+ end
22
+
23
+ test "invalid if attribute is already taken" do
24
+ user = User.create(email: "aquiles@me.com")
25
+ signup = Signup.new(email: user.email)
26
+
27
+ assert !(signup.valid?)
28
+ assert_equal [:not_unique], signup.errors[:email]
29
+ end
30
+
31
+ test "valid if attribute is not taken" do
32
+ signup = Signup.new(email: "aquiles@me.com")
33
+
34
+ assert signup.valid?
35
+ assert signup.errors.empty?
36
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: scrivener-contrib
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Francesco Rodríguez
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-13 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: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Extra validation helpers for Scrivener.
28
+ email:
29
+ - lrodriguezsanc@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gems"
35
+ - ".gitignore"
36
+ - README.md
37
+ - UNLICENSE
38
+ - lib/scrivener/contrib.rb
39
+ - lib/scrivener/ohm.rb
40
+ - makefile
41
+ - scrivener-contrib.gemspec
42
+ - test/confirmation.rb
43
+ - test/helper.rb
44
+ - test/length.rb
45
+ - test/ohm.rb
46
+ homepage: https://github.com/frodsan/scrivener-contrib
47
+ licenses:
48
+ - Unlicense
49
+ metadata: {}
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project:
66
+ rubygems_version: 2.2.0
67
+ signing_key:
68
+ specification_version: 4
69
+ summary: Extra validation helpers for Scrivener.
70
+ test_files: []