Module: NotNaughty

Defined in:
lib/not_naughty.rb,
lib/not_naughty/builder.rb,
lib/not_naughty/validator.rb,
lib/not_naughty/violation.rb,
lib/not_naughty/validation.rb,
lib/not_naughty/instance_methods.rb,
lib/not_naughty/validations/format_validation.rb,
lib/not_naughty/validations/length_validation.rb,
lib/not_naughty/validations/presence_validation.rb,
lib/not_naughty/validations/acceptance_validation.rb,
lib/not_naughty/validations/confirmation_validation.rb,
lib/not_naughty/validations/numericality_validation.rb,
lib/not_naughty/validations/sequel/uniqueness_validation.rb,
lib/not_naughty/error_handler.rb

Defined Under Namespace

Modules: Builder, InstanceMethods Classes: AcceptanceValidation, ConfirmationValidation, ErrorHandler, FormatValidation, LengthValidation, NumericalityValidation, PresenceValidation, UniquenessValidation, Validation, Validator, Violation

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object

Extended classes get NotNaughty::Builder and NotNaughty::InstanceMethods.



28
29
30
31
32
33
# File 'lib/not_naughty.rb', line 28

def self.extended(base)
  base.instance_eval do
    include InstanceMethods
    extend Builder
  end
end

Instance Method Details

#validated_before(method) ⇒ Object

Prepends a call for validation before then given method. If, on call, the validation passes the method is called. Otherwise it raises an NotNaughty::ValidationException or returns false.

Example:

validated_before :save # raise ValidationException unless valid?


77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/not_naughty.rb', line 77

def validated_before(method)
  __method = :"#{method}_without_validations"
  alias_method __method, method
  
  define_method method do |*params|
    begin
      if valid? then send! __method else raise errors end
    rescue Exception => error
      self.class.validator.error_handler.raise error
    end
  end
end

#validator(*states) ⇒ Object

call-seq: validator(validator_klass = NotNaughty::Validator, *states = [:default])

Returns instance of Validator. This is either the validator’s clone of superclass, an instance of the the given descendant of or the NotNaughty:Validator himself.

Examples:

validator # => Instance of NotNaughty::Validator with :default state
validator :create, :update # ~ - but with :create and :update states
validator AnotherValidator # Instance of AnotherValidator

validator AnotherValidator, :state_a, :state_b

The above examples work as long validator is not already called. To reset an already assigned validator set @validator to nil.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/not_naughty.rb', line 51

def validator(*states)
  @validator ||= if !states.empty?
    validator_klass =
      if states.first.is_a? Class and states.first <= NotNaughty::Validator
        states.shift
      else
        NotNaughty::Validator
      end
    
    validator_klass.new(*states)
    
  elsif superclass.respond_to? :validator
    superclass.validator.clone
    
  else
    NotNaughty::Validator.new
    
  end
end