Class: ActiveGraph::Shared::TypeConverters::EnumConverter

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

Instance Method Summary collapse

Constructor Details

#initialize(enum_keys, options) ⇒ EnumConverter

Returns a new instance of EnumConverter.



311
312
313
314
315
316
317
318
# File 'lib/active_graph/shared/type_converters.rb', line 311

def initialize(enum_keys, options)
  @enum_keys = enum_keys
  @options = options

  return unless @options[:case_sensitive].nil?

  @options[:case_sensitive] = ActiveGraph::Config.enums_case_sensitive
end

Instance Method Details

#convert_typeObject



332
333
334
# File 'lib/active_graph/shared/type_converters.rb', line 332

def convert_type
  Symbol
end

#converted?(value) ⇒ Boolean

Returns:



320
321
322
# File 'lib/active_graph/shared/type_converters.rb', line 320

def converted?(value)
  value.is_a?(db_type)
end

#db_typeObject



328
329
330
# File 'lib/active_graph/shared/type_converters.rb', line 328

def db_type
  Integer
end

#supports_array?Boolean

Returns:



324
325
326
# File 'lib/active_graph/shared/type_converters.rb', line 324

def supports_array?
  true
end

#to_db(value) ⇒ Object



342
343
344
345
346
347
348
349
350
351
352
# File 'lib/active_graph/shared/type_converters.rb', line 342

def to_db(value)
  if value.is_a?(Array)
    value.map(&method(:to_db))
  elsif @options[:case_sensitive]
    @enum_keys[value.to_s.to_sym] ||
      fail(ActiveGraph::Shared::Enum::InvalidEnumValueError, 'Value passed to an enum property must match one of the enum keys')
  else
    @enum_keys[value.to_s.downcase.to_sym] ||
      fail(ActiveGraph::Shared::Enum::InvalidEnumValueError, 'Case-insensitive (downcased) value passed to an enum property must match one of the enum keys')
  end
end

#to_ruby(value) ⇒ Object Also known as: call



336
337
338
# File 'lib/active_graph/shared/type_converters.rb', line 336

def to_ruby(value)
  @enum_keys.key(value) unless value.nil?
end