Module: ObjectEnum::EnumClassMethods

Defined in:
lib/object_enum/enum_class_methods.rb

Instance Method Summary collapse

Instance Method Details

#allArray

Get all values.

Returns:

  • (Array)

    All enum values



5
6
7
8
9
# File 'lib/object_enum/enum_class_methods.rb', line 5

def all
  self.constants(false)
    .map { |c| const_get(c) }
    .filter { |obj| obj.class.ancestors.include? enum_class }
end

#enum_classClass

Returns the class that represents the value of the Enum.

Returns:

  • (Class)

    class object



13
14
15
# File 'lib/object_enum/enum_class_methods.rb', line 13

def enum_class
  self
end

#filter(attributes = {}) ⇒ Array

Returns a value that matches Hash.

Parameters:

  • attributes (Hash) (defaults to: {})

    conditions

Returns:

  • (Array)

    All values that match the condition

Raises:

  • (ArgumentError)

    If not implement each_pair method



30
31
32
33
34
35
36
# File 'lib/object_enum/enum_class_methods.rb', line 30

def filter(attributes = {})
  unless attributes.respond_to? :each_pair
    raise ArgumentError, "You must pass a hash. But #{attributes.class} passed."
  end

  all.filter { |enum_value| match_all_attributes?(enum_value, attributes) }
end

#find(attributes = {}) ⇒ Object

Returns a value that matches Hash. If there is no match, nil is returned.

Parameters:

  • attributes (Hash) (defaults to: {})

    conditions

Returns:

  • Enum value or nil

Raises:

  • (ArgumentError)

    If not implement each_pair method



22
23
24
# File 'lib/object_enum/enum_class_methods.rb', line 22

def find(attributes = {})
  filter(attributes).first
end