Class: Tabledata::ColumnDefinition::BooleanProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/tabledata/column_definition.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ BooleanProcessor


148
149
150
151
152
153
154
155
# File 'lib/tabledata/column_definition.rb', line 148

def initialize(options)
  true_values  = options[:true_value].is_a?(Array) ? options.delete(:true_value) : [options.delete(:true_value)]
  false_values = options[:false_value].is_a?(Array) ? options.delete(:false_value) : [options.delete(:false_value)]
  @convert     = {}
  true_values.each do |true_value| @convert[true_value] = true end
  false_values.each do |false_value| @convert[false_value] = false end
  @convert[nil] ||= nil
end

Instance Method Details

#call(value, errors) ⇒ Object


156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/tabledata/column_definition.rb', line 156

def call(value, errors)
  case value
    when TrueClass, FalseClass
      processed = value
    else
      processed = @convert.fetch(value) {
        errors << [:invalid_input, {value: value, acceptable: @convert.values.join(', ')}]
        nil
      }
  end

  processed
end