Class: BehaviorValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/behavior_validator.rb,
lib/behavior_validator/engine.rb,
lib/behavior_validator/version.rb

Defined Under Namespace

Classes: Engine

Constant Summary collapse

VERSION =
'0.0.3'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.reserved_optionsObject



10
11
12
# File 'lib/behavior_validator.rb', line 10

def reserved_options
  [:allow_nil, :allow_blank, :message, :on, :if, :unless]
end

.valid?(value, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


3
4
5
6
7
8
# File 'lib/behavior_validator.rb', line 3

def valid?(value, options = {})
  options = options.reject { |option| reserved_options.include?(option) }
  options.each_pair.all? do |method, expected_result|
    value.respond_to?(method) && value.__send__(method) == expected_result
  end
end

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



15
16
17
18
19
# File 'lib/behavior_validator.rb', line 15

def validate_each(record, attribute, value)
  unless self.class.valid?(value, options)
    record.errors.add(attribute, options[:message] || :invalid_behavior)
  end
end