Module: NotNaughty

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

Defined Under Namespace

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object

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


24
25
26
27
28
29
# File 'lib/not_naughty.rb', line 24

def self.extended(base)
  base.instance_eval do
    include InstanceMethods
    extend ClassMethods
  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?

73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/not_naughty.rb', line 73

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.


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/not_naughty.rb', line 47

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