Class: Predicates::Enumerated

Inherits:
Base
  • Object
show all
Defined in:
lib/predicates/enumerated.rb

Overview

A basic pattern where the values are enumerated.

Options

  • :options - a complete collection of values that the attribute may contain

Example

field_is_enumerated :options => ['allowed_value_one', 'allowed_value_two']

Direct Known Subclasses

Aliased, Whitelisted

Instance Attribute Summary collapse

Attributes inherited from Base

#full_message, #or_empty, #validate_if, #validate_on

Instance Method Summary collapse

Methods inherited from Base

#allow_empty?, #error, #error_binds, #normalize, #to_human

Constructor Details

#initialize(attr, options = {}) ⇒ Enumerated

Returns a new instance of Enumerated.



11
12
13
14
# File 'lib/predicates/enumerated.rb', line 11

def initialize(attr, options = {})
  options[:or_empty] ||= false
  super(attr, options)
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



9
10
11
# File 'lib/predicates/enumerated.rb', line 9

def options
  @options
end

Instance Method Details

#error_messageObject



16
17
18
# File 'lib/predicates/enumerated.rb', line 16

def error_message
  @error_message || :inclusion
end

#validate(value, record) ⇒ Object



20
21
22
# File 'lib/predicates/enumerated.rb', line 20

def validate(value, record)
  self.options.include? value
end