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

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.new
.password = "123456"
.password_confirmation = "nonsense"
.valid? # => false
.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.new(status: "10")
.valid? # => false
.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.new(password: _very_long_password)
.valid? # => false
.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.new(password: "short")
.valid? # => false
.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.new(email: user.email)
.valid? # => false
.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