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.



275
276
277
278
279
280
281
282
# File 'lib/active_graph/shared/type_converters.rb', line 275

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



296
297
298
# File 'lib/active_graph/shared/type_converters.rb', line 296

def convert_type
  Symbol
end

#converted?(value) ⇒ Boolean

Returns:



284
285
286
# File 'lib/active_graph/shared/type_converters.rb', line 284

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

#db_typeObject



292
293
294
# File 'lib/active_graph/shared/type_converters.rb', line 292

def db_type
  Integer
end

#supports_array?Boolean

Returns:



288
289
290
# File 'lib/active_graph/shared/type_converters.rb', line 288

def supports_array?
  true
end

#to_db(value) ⇒ Object



306
307
308
309
310
311
312
313
314
315
316
# File 'lib/active_graph/shared/type_converters.rb', line 306

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



300
301
302
# File 'lib/active_graph/shared/type_converters.rb', line 300

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