Class: Sequel::Plugins::NotNaughty

Inherits:
NotNaughty::Validator
  • Object
show all
Defined in:
lib/sequel_not_naughty.rb

Overview

Adapter for Sequel …

… is a Sequel::Plugin.


To make it overall available:

class Sequel::Model
  is :not_naughty
end

Options:

To turn off before_validate and after_validate hooks:

class User < Sequel::Model
  is :not_naughty, :without => :hooks
end

To turn off raised Exceptions if validation before save fails:

class User < Sequel::Model
  # save on invalid users will return false
  is :not_naughty, :without => :exceptions
end

To combine those:

class User < Sequel::Model
  is :not_naughty, :without => [:exceptions, :hooks]
end

API compatibility:

The validates_length_of method does only one check per attribute. If you want to check minimum and maximum length with different error messages you need to write 2 validations:

validates_length_of :name, :minimum => 4
validates_length_of :name, :maximum => 12

Except the example above the Plugin should be 100% API compatible.

Defined Under Namespace

Modules: ClassMethods, Hooks

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.apply(receiver, *args) ⇒ Object

Applies plugin to a Sequel::Model.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/sequel_not_naughty.rb', line 56

def self.apply(receiver, *args)
  without = [args.extract_options![:without]].flatten
  
  receiver.extend ::NotNaughty
  receiver.validator self, :create, :update
  
  ::NotNaughty::Validation.load(
    :acceptance, :confirmation, :format,
    :length, :numericality, :presence
  )
  
  receiver.extend ClassMethods
  
  receiver.instance_eval { alias_method :save, :save! }
  receiver.validated_before :save
  
  without.include? :exception or
  receiver.validator.error_handler.handle(::NotNaughty::Violation) {false}
  
  without.include? :hooks or
  receiver.send! :include, Hooks
end

Instance Method Details

#get_state(instance) ⇒ Object

Returns state for given instance.



80
81
82
# File 'lib/sequel_not_naughty.rb', line 80

def get_state(instance)
  if instance.new? then @states[:create] else @states[:update] end
end