Class: ActiveGraph::Shared::TypeConverters::BooleanConverter

Inherits:
BaseConverter
  • Object
show all
Defined in:
lib/active_graph/shared/type_converters.rb

Constant Summary collapse

FALSE_VALUES =
%w(n N no No NO false False FALSE off Off OFF f F).to_set

Class Method Summary collapse

Methods inherited from BaseConverter

#supports_array?

Class Method Details

.converted?(value) ⇒ Boolean

Returns:


198
199
200
# File 'lib/active_graph/shared/type_converters.rb', line 198

def converted?(value)
  converted_values.include?(value)
end

.converted_valuesObject


202
203
204
# File 'lib/active_graph/shared/type_converters.rb', line 202

def converted_values
  [true, false]
end

.db_typeObject Also known as: convert_type


206
207
208
# File 'lib/active_graph/shared/type_converters.rb', line 206

def db_type
  ActiveGraph::Shared::Boolean
end

.to_db(value) ⇒ Object Also known as: to_ruby


212
213
214
215
216
217
218
219
220
221
222
# File 'lib/active_graph/shared/type_converters.rb', line 212

def to_db(value)
  return false if FALSE_VALUES.include?(value)
  case value
  when TrueClass, FalseClass
    value
  when Numeric, /^\-?[0-9]/
    !value.to_f.zero?
  else
    value.present?
  end
end