lib/neo4j/shared/enum.rb in neo4j-8.3.4 vs lib/neo4j/shared/enum.rb in neo4j-9.0.0

- old
+ new

@@ -1,10 +1,11 @@ module Neo4j::Shared module Enum extend ActiveSupport::Concern class ConflictingEnumMethodError < Neo4j::Error; end + class InvalidEnumValueError < Neo4j::InvalidParameterError; end module ClassMethods attr_reader :neo4j_enum_data # Similar to ActiveRecord enum, maps an integer value on the database to @@ -41,32 +42,42 @@ # # @see http://edgeapi.rubyonrails.org/classes/ActiveRecord/Enum.html def enum(parameters = {}) options, parameters = *split_options_and_parameters(parameters) parameters.each do |property_name, enum_keys| - enum_keys = normalize_key_list enum_keys + enum_keys = normalize_key_list enum_keys, options @neo4j_enum_data ||= {} @neo4j_enum_data[property_name] = enum_keys define_property(property_name, enum_keys, options) define_enum_methods(property_name, enum_keys, options) end end protected - def normalize_key_list(enum_keys) + def normalize_key_list(enum_keys, options) + case_sensitive = options.fetch(:_case_sensitive, Neo4j::Config.enums_case_sensitive) + case enum_keys when Hash enum_keys when Array - Hash[enum_keys.each_with_index.to_a] + enum_keys = Hash[enum_keys.each_with_index.to_a] else fail ArgumentError, 'Invalid parameter for enum. Please provide an Array or an Hash.' end + + unless case_sensitive + enum_keys.keys.each do |key| + fail ArgumentError, 'Enum keys must be lowercase unless _case_sensitive = true' unless key.downcase == key + end + end + + enum_keys end - VALID_OPTIONS_FOR_ENUMS = [:_index, :_prefix, :_suffix, :_default] + VALID_OPTIONS_FOR_ENUMS = [:_index, :_prefix, :_suffix, :_default, :_case_sensitive] def split_options_and_parameters(parameters) options = {} new_parameters = {} parameters.each do |k, v| @@ -78,18 +89,27 @@ end [options, new_parameters] end def define_property(property_name, enum_keys, options) - property_options = build_property_options(enum_keys, options) - property property_name, property_options - serialize property_name, Neo4j::Shared::TypeConverters::EnumConverter.new(enum_keys, property_options) + property property_name, build_property_options(enum_keys, options) + serialize property_name, Neo4j::Shared::TypeConverters::EnumConverter.new(enum_keys, build_enum_options(enum_keys, options)) end def build_property_options(_enum_keys, options = {}) { default: options[:_default] } + end + + def build_enum_options(enum_keys, options = {}) + if options[:_default] && not(enum_keys.include?(options[:_default])) + fail ArgumentError, 'Enum default value must match an enum key' + end + + build_property_options(enum_keys, options).tap do |enum_options| + enum_options[:case_sensitive] = options[:_case_sensitive] + end end def define_enum_methods(property_name, enum_keys, options) define_enum_methods_?(property_name, enum_keys, options) define_enum_methods_!(property_name, enum_keys, options)