Module: Scrivener::Validations
- Defined in:
- lib/scrivener/ohm.rb,
lib/scrivener/utils.rb,
lib/scrivener/sequel.rb,
lib/scrivener/contrib.rb
Defined Under Namespace
Modules: Utils
Instance Method Summary collapse
-
#assert_confirmation(att, error = [att, :not_confirmed]) ⇒ Object
Encapsulates the pattern of wanting to validate a password or email address field with a confirmation.
-
#assert_exact_length(att, val, error = [att, :wrong_length]) ⇒ Object
Validates that the specified attribute matches exactly the length restriction supplied.
-
#assert_maximum_length(att, val, error = [att, :too_long]) ⇒ Object
Validates that maximum size of the specified attribute.
-
#assert_minimum_length(att, val, error = [att, :too_short]) ⇒ Object
Validates that minimum size of the specified attribute.
-
#assert_unique(att, model, error = [att, :not_unique]) ⇒ Object
Validates that an attribute is unique.
Instance Method Details
#assert_confirmation(att, error = [att, :not_confirmed]) ⇒ Object
Encapsulates the pattern of wanting to validate a password or email address field with a confirmation.
class SignUp < Scrivener
attr_accessor :password, :password_confirmation
def validate
assert_present(:password)
assert_confirmation(:password)
end
end
signup = SignUp.new
signup.password = "123456"
signup.password_confirmation = "nonsense"
signup.valid? # => false
signup.errors[:password] # => [:not_confirmed]
76 77 78 |
# File 'lib/scrivener/contrib.rb', line 76 def assert_confirmation(att, error = [att, :not_confirmed]) assert(send(:"#{att}_confirmation") == send(att), error) end |
#assert_exact_length(att, val, error = [att, :wrong_length]) ⇒ Object
Validates that the specified attribute matches exactly the length restriction supplied.
class SignUp < Scrivener
attr_accessor :status
def validate
assert_exact_length(:status, 1)
end
end
signup = Signup.new(status: "10")
signup.valid? # => false
signup.errors[:status] # => [:wrong_length]
54 55 56 |
# File 'lib/scrivener/contrib.rb', line 54 def assert_exact_length(att, val, error = [att, :wrong_length]) assert(send(att).to_s.length == val, error) end |
#assert_maximum_length(att, val, error = [att, :too_long]) ⇒ Object
Validates that maximum size of the specified attribute.
class SignUp < Scrivener
attr_accessor :password
def validate
assert_minimum_length(:password, 72)
end
end
signup = Signup.new(password: _very_long_password)
signup.valid? # => false
signup.errors[:password] # => [:too_long]
35 36 37 |
# File 'lib/scrivener/contrib.rb', line 35 def assert_maximum_length(att, val, error = [att, :too_long]) assert(send(att).to_s.length <= val, error) end |
#assert_minimum_length(att, val, error = [att, :too_short]) ⇒ Object
Validates that minimum size of the specified attribute.
class SignUp < Scrivener
attr_accessor :password
def validate
assert_minimum_length(:password, 8)
end
end
signup = Signup.new(password: "short")
signup.valid? # => false
signup.errors[:password] # => [:too_short]
17 18 19 |
# File 'lib/scrivener/contrib.rb', line 17 def assert_minimum_length(att, val, error = [att, :too_short]) assert(send(att).to_s.length >= val, error) end |
#assert_unique(att, model, error = [att, :not_unique]) ⇒ Object
Validates that an attribute is unique.
class User < Sequel::Model
end
class SignUp < Scrivener
attr_accessor :email
def validate
assert_unique(:email, :User)
end
end
user = User.create(email: "[email protected]")
signup = SignUp.new(email: user.email)
signup.valid? # => false
signup.errors[:email] # => [:not_unique]
26 27 28 29 30 31 |
# File 'lib/scrivener/ohm.rb', line 26 def assert_unique(att, model, error = [att, :not_unique]) model = Utils.const(self.class, model) record = model.with(att, send(att)) assert(record.nil?, error) end |