Class: Neo4j::Shared::TypeConverters::EnumConverter

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

Instance Method Summary collapse

Constructor Details

#initialize(enum_keys, options) ⇒ EnumConverter

Returns a new instance of EnumConverter.



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

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

  return unless @options[:case_sensitive].nil?

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

Instance Method Details

#convert_typeObject



295
296
297
# File 'lib/neo4j/shared/type_converters.rb', line 295

def convert_type
  Symbol
end

#converted?(value) ⇒ Boolean

Returns:



283
284
285
# File 'lib/neo4j/shared/type_converters.rb', line 283

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

#db_typeObject



291
292
293
# File 'lib/neo4j/shared/type_converters.rb', line 291

def db_type
  Integer
end

#supports_array?Boolean

Returns:



287
288
289
# File 'lib/neo4j/shared/type_converters.rb', line 287

def supports_array?
  true
end

#to_db(value) ⇒ Object



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

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(Neo4j::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(Neo4j::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



299
300
301
# File 'lib/neo4j/shared/type_converters.rb', line 299

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