module ObjectEnum::EnumClassMethods

Public Instance Methods

all() click to toggle source

Get all values. @return [Array] All enum values

# 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_class() click to toggle source

Returns the class that represents the value of the Enum. @return [Class] class object

# File lib/object_enum/enum_class_methods.rb, line 13
def enum_class
  self
end
filter(attributes = {}) click to toggle source

Returns a value that matches Hash. @param [Hash] attributes conditions @return [Array] All values that match the condition @raise [ArgumentError] If not implement each_pair method

# 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 = {}) click to toggle source

Returns a value that matches Hash. If there is no match, nil is returned. @param [Hash] attributes conditions @return Enum value or nil @raise [ArgumentError] If not implement each_pair method

# File lib/object_enum/enum_class_methods.rb, line 22
def find(attributes = {})
  filter(attributes).first
end

Private Instance Methods

match_all_attributes?(enum_value, attributes = {}) click to toggle source
# File lib/object_enum/enum_class_methods.rb, line 40
def match_all_attributes?(enum_value, attributes = {})
  attributes.each_pair do |key, value|
    return false if enum_value.send(key.to_sym) != value
  end

  true
end