Class: EnumX::ValueList

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/enum_x/value_list.rb

Overview

A list of multiple enum values.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(enum, values) ⇒ ValueList

Initialization



9
10
11
12
13
# File 'lib/enum_x/value_list.rb', line 9

def initialize(enum, values)
  @enum = enum
  values = [ values ] unless values.is_a?(Enumerable)
  @values = values.map { |value| @enum[value] || value }
end

Instance Attribute Details

#enumObject (readonly)

Attributes



18
19
20
# File 'lib/enum_x/value_list.rb', line 18

def enum
  @enum
end

#valuesObject (readonly) Also known as: to_ary, to_a

Returns the value of attribute values.



20
21
22
# File 'lib/enum_x/value_list.rb', line 20

def values
  @values
end

Instance Method Details

#==(other) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/enum_x/value_list.rb', line 57

def ==(other)
  case other
  when Array then values == other
  when EnumX::ValueList then values == other.values
  when EnumX::Value then values == [ other ]
  else false
  end
end

#[](value) ⇒ Object



49
50
51
# File 'lib/enum_x/value_list.rb', line 49

def [](value)
  values.find { |val| val.to_s == value.to_s }
end

#include?(value) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/enum_x/value_list.rb', line 53

def include?(value)
  values.any? { |val| val.to_s == value.to_s }
end

#to_sObject

Creates a string representation of the values.



34
35
36
# File 'lib/enum_x/value_list.rb', line 34

def to_s
  values.map(&:to_s).join(', ')
end