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.



269
270
271
272
273
274
275
276
# File 'lib/active_graph/shared/type_converters.rb', line 269

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



290
291
292
# File 'lib/active_graph/shared/type_converters.rb', line 290

def convert_type
  Symbol
end

#converted?(value) ⇒ Boolean

Returns:



278
279
280
# File 'lib/active_graph/shared/type_converters.rb', line 278

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

#db_typeObject



286
287
288
# File 'lib/active_graph/shared/type_converters.rb', line 286

def db_type
  Integer
end

#supports_array?Boolean

Returns:



282
283
284
# File 'lib/active_graph/shared/type_converters.rb', line 282

def supports_array?
  true
end

#to_db(value) ⇒ Object



300
301
302
303
304
305
306
307
308
309
310
# File 'lib/active_graph/shared/type_converters.rb', line 300

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



294
295
296
# File 'lib/active_graph/shared/type_converters.rb', line 294

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