scrivener-contrib 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9909e7ad08610ce637d73a6e7042ed1c78f3f520
4
- data.tar.gz: b70b640e41ae7dcf0ba642d827314328242eca79
3
+ metadata.gz: 4a16cd30105eac68800e11f51f739224b01d54a3
4
+ data.tar.gz: d099f993fa188551d24a58a23969022adf4d4a73
5
5
  SHA512:
6
- metadata.gz: 0df8f451f9f037e8be147685fd321bad834ffe953f0f3c705574db80815ef666f7e473c57fc9b0bab699e28fbbb275bca70d6cdd229577a52032698b59d37124
7
- data.tar.gz: ac08a267c75050c979b435369339855b79c3553c774613868aae654cfa4d01910e7ba9f6c08d4bdc05a26815180e231937bb6b75645ca8e0c9d251431711b933
6
+ metadata.gz: e88abacc21018fc224976f7a01788fdf73acfd8bd257e98e5da914d3784abf4f061d1257190f934f736257165acc6ba552e12189abbd738f1a6a98dfb0ca0ee0
7
+ data.tar.gz: e203513b47c9f2673abaf2741e8271c818c19dbdbf1fc03fb0c817930a6a38d80b9df27ab2738acd3ba8e376447b2922aa4ed73412ea648106f0502167076e60
data/.gems CHANGED
@@ -1,3 +1,5 @@
1
- cutest -v 1.2.1
2
- ohm -v 2.0.1
3
- scrivener -v 0.2.0
1
+ cutest -v 1.2.2
2
+ ohm -v 2.2.0
3
+ scrivener -v 0.4.0
4
+ sequel -v 4.20.0
5
+ sqlite3 -v 1.3.10
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2014 Francesco Rodríguez, Mayn Kjær
1
+ Copyright (c) 2014-Present Harmoni LLC
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  scrivener-contrib
2
2
  =================
3
3
 
4
- Extra validations for Scrivener
4
+ Extra validations for Scrivener.
5
5
 
6
6
  Usage
7
7
  -----
@@ -9,10 +9,11 @@ Usage
9
9
  The extra validations are:
10
10
 
11
11
  ```ruby
12
- assert_confirmation(:password)
13
- assert_minimum_length(:password, 6)
14
- assert_maximum_length(:name, 50)
15
- assert_exact_length(:idnum, 8)
12
+ assert_confirmation(:attribute)
13
+ assert_minimum_length(:attribute, integer)
14
+ assert_maximum_length(:attribute, integer)
15
+ assert_exact_length(:attribute, integer)
16
+ assert_unique(:attribute, :Model)
16
17
  ```
17
18
 
18
19
  This is a basic example:
@@ -20,8 +21,9 @@ This is a basic example:
20
21
  ```ruby
21
22
  require "scrivener"
22
23
  require "scrivener/contrib"
24
+ require "scrivener/ohm" # or require "scrivener/sequel"
23
25
 
24
- class Signup < Scrivener
26
+ class SignUp < Scrivener
25
27
  attr_accessor :username
26
28
  attr_accessor :name
27
29
  attr_accessor :idnum
@@ -36,8 +38,33 @@ class Signup < Scrivener
36
38
 
37
39
  assert_maximum_length(:name, 50)
38
40
  assert_exact_length(:idnum, 8)
41
+
42
+ if assert_present(:username)
43
+ assert_unique(:username, :User)
44
+ end
39
45
  end
40
46
  end
47
+
48
+ signup = SignUp.new(
49
+ username: "repeated",
50
+ password: "123",
51
+ name: _very_long_name
52
+ )
53
+
54
+ signup.valid?
55
+ # => false
56
+
57
+ signup.errors[:password]
58
+ # => [:not_confirmed, :too_short]
59
+
60
+ signup.errors[:username]
61
+ # => [:not_unique]
62
+
63
+ signup.errors[:name]
64
+ # => [:too_long]
65
+
66
+ signup.errors[:idnum]
67
+ # => [:wrong_length]
41
68
  ```
42
69
 
43
70
  Installation
@@ -1,21 +1,80 @@
1
- require_relative "ohm" if defined?(Ohm)
2
-
3
1
  class Scrivener
4
2
  module Validations
5
- def assert_confirmation(att, error = [att, :not_confirmed])
6
- assert(send(:"#{att}_confirmation") == send(att), error)
7
- end
8
-
3
+ # Validates that minimum size of the specified attribute.
4
+ #
5
+ # class SignUp < Scrivener
6
+ # attr_accessor :password
7
+ #
8
+ # def validate
9
+ # assert_minimum_length(:password, 8)
10
+ # end
11
+ # end
12
+ #
13
+ # signup = Signup.new(password: "short")
14
+ # signup.valid? # => false
15
+ # signup.errors[:password] # => [:too_short]
16
+ #
9
17
  def assert_minimum_length(att, val, error = [att, :too_short])
10
18
  assert(send(att).to_s.length >= val, error)
11
19
  end
12
20
 
21
+ # Validates that maximum size of the specified attribute.
22
+ #
23
+ # class SignUp < Scrivener
24
+ # attr_accessor :password
25
+ #
26
+ # def validate
27
+ # assert_minimum_length(:password, 72)
28
+ # end
29
+ # end
30
+ #
31
+ # signup = Signup.new(password: _very_long_password)
32
+ # signup.valid? # => false
33
+ # signup.errors[:password] # => [:too_long]
34
+ #
13
35
  def assert_maximum_length(att, val, error = [att, :too_long])
14
36
  assert(send(att).to_s.length <= val, error)
15
37
  end
16
38
 
39
+ # Validates that the specified attribute matches exactly the
40
+ # length restriction supplied.
41
+ #
42
+ # class SignUp < Scrivener
43
+ # attr_accessor :status
44
+ #
45
+ # def validate
46
+ # assert_exact_length(:status, 1)
47
+ # end
48
+ # end
49
+ #
50
+ # signup = Signup.new(status: "10")
51
+ # signup.valid? # => false
52
+ # signup.errors[:status] # => [:wrong_length]
53
+ #
17
54
  def assert_exact_length(att, val, error = [att, :wrong_length])
18
55
  assert(send(att).to_s.length == val, error)
19
56
  end
57
+
58
+ # Encapsulates the pattern of wanting to validate a password or
59
+ # email address field with a confirmation.
60
+ #
61
+ # class SignUp < Scrivener
62
+ # attr_accessor :password, :password_confirmation
63
+ #
64
+ # def validate
65
+ # assert_present(:password)
66
+ # assert_confirmation(:password)
67
+ # end
68
+ # end
69
+ #
70
+ # signup = SignUp.new
71
+ # signup.password = "123456"
72
+ # signup.password_confirmation = "nonsense"
73
+ # signup.valid? # => false
74
+ # signup.errors[:password] # => [:not_confirmed]
75
+ #
76
+ def assert_confirmation(att, error = [att, :not_confirmed])
77
+ assert(send(:"#{att}_confirmation") == send(att), error)
78
+ end
20
79
  end
21
80
  end
@@ -1,20 +1,33 @@
1
+ require_relative "utils"
2
+
1
3
  class Scrivener
2
4
  module Validations
5
+ # Validates that an attribute is unique.
6
+ #
7
+ # class User < Ohm::Model
8
+ # attribute :email
9
+ #
10
+ # unique :email
11
+ # end
12
+ #
13
+ # class SignUp < Scrivener
14
+ # attr_accessor :email
15
+ #
16
+ # def validate
17
+ # assert_unique(:email, :User)
18
+ # end
19
+ # end
20
+ #
21
+ # user = User.create(email: "mayn.kjaer@gmail.com")
22
+ # signup = SignUp.new(email: user.email)
23
+ # signup.valid? # => false
24
+ # signup.errors[:email] # => [:not_unique]
25
+ #
3
26
  def assert_unique(att, model, error = [att, :not_unique])
4
- model = Utils.const(self.class, model)
27
+ model = Utils.const(self.class, model)
5
28
  record = model.with(att, send(att))
6
29
 
7
30
  assert(record.nil?, error)
8
31
  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
32
  end
20
33
  end
@@ -0,0 +1,30 @@
1
+ require_relative "utils"
2
+
3
+ class Scrivener
4
+ module Validations
5
+ # Validates that an attribute is unique.
6
+ #
7
+ # class User < Sequel::Model
8
+ # end
9
+ #
10
+ # class SignUp < Scrivener
11
+ # attr_accessor :email
12
+ #
13
+ # def validate
14
+ # assert_unique(:email, :User)
15
+ # end
16
+ # end
17
+ #
18
+ # user = User.create(email: "frodsan@me.com")
19
+ # signup = SignUp.new(email: user.email)
20
+ # signup.valid? # => false
21
+ # signup.errors[:email] # => [:not_unique]
22
+ #
23
+ def assert_unique(att, model, error = [att, :not_unique])
24
+ model = Utils.const(self.class, model)
25
+ record = model[att => send(att)]
26
+
27
+ assert(record.nil?, error)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,13 @@
1
+ class Scrivener
2
+ module Validations
3
+ module Utils # :nodoc:
4
+ def self.const(context, name)
5
+ case name
6
+ when Symbol, String
7
+ context.const_get(name)
8
+ else name
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
data/makefile CHANGED
@@ -1,4 +1,8 @@
1
+ .DEFAULT_GOAL := test
1
2
  .PHONY: test
2
3
 
4
+ gem:
5
+ gem build *.gemspec
6
+
3
7
  test:
4
8
  cutest test/*.rb
@@ -1,14 +1,19 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "scrivener-contrib"
3
- s.version = "1.0.0"
4
- s.summary = "Extra validation helpers for Scrivener."
3
+ s.version = "1.1.0"
4
+ s.summary = "Extra validations for Scrivener."
5
5
  s.description = s.summary
6
6
  s.authors = ["Francesco Rodríguez", "Mayn Kjær"]
7
7
  s.email = ["frodsan@me.com", "mayn.kjaer@gmail.com"]
8
- s.homepage = "https://github.com/harmoni-io/scrivener-contrib"
8
+ s.homepage = "https://github.com/harmoni/scrivener-contrib"
9
9
  s.license = "MIT"
10
10
 
11
11
  s.files = `git ls-files`.split("\n")
12
12
 
13
+ s.add_dependency "scrivener"
14
+
13
15
  s.add_development_dependency "cutest"
16
+ s.add_development_dependency "ohm"
17
+ s.add_development_dependency "sequel"
18
+ s.add_development_dependency "sqlite3"
14
19
  end
@@ -1,5 +1,6 @@
1
1
  require "ohm"
2
2
  require_relative "helper"
3
+ require_relative "../lib/scrivener/ohm"
3
4
 
4
5
  Ohm.redis = Redic.new("redis://localhost:6379")
5
6
  Ohm.redis.call("SELECT", "30")
@@ -22,7 +23,7 @@ prepare do
22
23
  end
23
24
 
24
25
  test "invalid if attribute is already taken" do
25
- user = User.create(email: "aquiles@me.com")
26
+ user = User.create(email: "aquiles@me.com")
26
27
  signup = Signup.new(email: user.email)
27
28
 
28
29
  assert !(signup.valid?)
@@ -0,0 +1,39 @@
1
+ require "sequel"
2
+ require_relative "helper"
3
+ require_relative "../lib/scrivener/sequel"
4
+
5
+ DB = Sequel.sqlite
6
+
7
+ DB.create_table(:users) do
8
+ String :email
9
+ end
10
+
11
+ class User < Sequel::Model
12
+ end
13
+
14
+ class Signup < Scrivener
15
+ attr_accessor :email
16
+
17
+ def validate
18
+ assert_unique :email, :User
19
+ end
20
+ end
21
+
22
+ prepare do
23
+ DB[:users].truncate
24
+ end
25
+
26
+ test "invalid if attribute is already taken" do
27
+ user = User.create(email: "aquiles@me.com")
28
+ signup = Signup.new(email: user.email)
29
+
30
+ assert !(signup.valid?)
31
+ assert_equal [:not_unique], signup.errors[:email]
32
+ end
33
+
34
+ test "valid if attribute is not taken" do
35
+ signup = Signup.new(email: "aquiles@me.com")
36
+
37
+ assert signup.valid?
38
+ assert signup.errors.empty?
39
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scrivener-contrib
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Francesco Rodríguez
@@ -9,8 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-08-28 00:00:00.000000000 Z
12
+ date: 2015-03-08 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: scrivener
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
14
28
  - !ruby/object:Gem::Dependency
15
29
  name: cutest
16
30
  requirement: !ruby/object:Gem::Requirement
@@ -25,7 +39,49 @@ dependencies:
25
39
  - - ">="
26
40
  - !ruby/object:Gem::Version
27
41
  version: '0'
28
- description: Extra validation helpers for Scrivener.
42
+ - !ruby/object:Gem::Dependency
43
+ name: ohm
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: sequel
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: sqlite3
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ description: Extra validations for Scrivener.
29
85
  email:
30
86
  - frodsan@me.com
31
87
  - mayn.kjaer@gmail.com
@@ -39,13 +95,16 @@ files:
39
95
  - README.md
40
96
  - lib/scrivener/contrib.rb
41
97
  - lib/scrivener/ohm.rb
98
+ - lib/scrivener/sequel.rb
99
+ - lib/scrivener/utils.rb
42
100
  - makefile
43
101
  - scrivener-contrib.gemspec
44
102
  - test/confirmation.rb
45
103
  - test/helper.rb
46
104
  - test/length.rb
47
105
  - test/ohm.rb
48
- homepage: https://github.com/harmoni-io/scrivener-contrib
106
+ - test/sequel.rb
107
+ homepage: https://github.com/harmoni/scrivener-contrib
49
108
  licenses:
50
109
  - MIT
51
110
  metadata: {}
@@ -65,8 +124,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
65
124
  version: '0'
66
125
  requirements: []
67
126
  rubyforge_project:
68
- rubygems_version: 2.2.2
127
+ rubygems_version: 2.4.5
69
128
  signing_key:
70
129
  specification_version: 4
71
- summary: Extra validation helpers for Scrivener.
130
+ summary: Extra validations for Scrivener.
72
131
  test_files: []