Module: ActiveGraph::Shared::Enum::ClassMethods

Defined in:
lib/active_graph/shared/enum.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#neo4j_enum_dataObject (readonly)

Returns the value of attribute neo4j_enum_data.



9
10
11
# File 'lib/active_graph/shared/enum.rb', line 9

def neo4j_enum_data
  @neo4j_enum_data
end

Instance Method Details

#enum(**parameters) ⇒ Object

Similar to ActiveRecord enum, maps an integer value on the database to a set of enum keys.

Examples:

Base example

class Media
  include ActiveGraph::Node
  enum type: [:image, :video, :unknown]
end
Media.types # => { :images => 0, :video => 1, :unknown => 2 }

media.video!
media.image? # => false
media.type # => :video

Media.videos # => All medias with type = 1 (:video)
Media.where(type: :video) # => All medias with type = 1 (:video)

Prefix-ing an enum

Media.enum type: [:image, :video, :unknown], _prefix: :enum

media.enum_video!
media.enum_video? # => true

Suffix-ing an enum

Media.enum type: [:image, :video, :unknown], _suffix: true

media.video_type!
media.video_type? # => true

Define a custom mapping for keys-numbers

Media.enum type: { image: 1, video: 2, unknown: 3 }

See Also:



44
45
46
47
48
49
50
51
52
53
# File 'lib/active_graph/shared/enum.rb', line 44

def enum(**parameters)
  options, parameters = *split_options_and_parameters(parameters)
  parameters.each do |property_name, 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