lib/scrivener/contrib.rb in scrivener-contrib-1.0.0 vs lib/scrivener/contrib.rb in scrivener-contrib-1.1.0

- old
+ new

@@ -1,21 +1,80 @@ -require_relative "ohm" if defined?(Ohm) - class Scrivener module Validations - def assert_confirmation(att, error = [att, :not_confirmed]) - assert(send(:"#{att}_confirmation") == send(att), error) - end - + # 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] + # def assert_minimum_length(att, val, error = [att, :too_short]) assert(send(att).to_s.length >= val, error) end + # 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] + # def assert_maximum_length(att, val, error = [att, :too_long]) assert(send(att).to_s.length <= val, error) end + # 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] + # def assert_exact_length(att, val, error = [att, :wrong_length]) assert(send(att).to_s.length == val, error) + end + + # 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] + # + def assert_confirmation(att, error = [att, :not_confirmed]) + assert(send(:"#{att}_confirmation") == send(att), error) end end end