Class: Enumerize::Set

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Predicatable
Defined in:
lib/enumerize/set.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Predicatable

#respond_to_missing?

Constructor Details

#initialize(obj, attr, values) ⇒ Set

Returns a new instance of Set.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/enumerize/set.rb', line 10

def initialize(obj, attr, values)
  @obj    = obj
  @attr   = attr
  @values = []

  if values.respond_to?(:each)
    values.each do |input|
      value = @attr.find_value(input)

      if value && !@values.include?(value)
        @values << value
      end
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Enumerize::Predicatable

Instance Attribute Details

#valuesObject (readonly)

Returns the value of attribute values.



8
9
10
# File 'lib/enumerize/set.rb', line 8

def values
  @values
end

Instance Method Details

#<<(value) ⇒ Object Also known as: push



26
27
28
29
# File 'lib/enumerize/set.rb', line 26

def <<(value)
  @values << value
  mutate!
end

#==(other) ⇒ Object Also known as: eql?



41
42
43
# File 'lib/enumerize/set.rb', line 41

def ==(other)
  other.size == size && other.all? { |v| @values.include?(@attr.find_value(v)) }
end

#delete(value) ⇒ Object



51
52
53
54
# File 'lib/enumerize/set.rb', line 51

def delete(value)
  @values.delete(@attr.find_value(value))
  mutate!
end

#encode_with(coder) ⇒ Object



60
61
62
# File 'lib/enumerize/set.rb', line 60

def encode_with(coder)
  coder.represent_object(Array, @values)
end

#include?(value) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/enumerize/set.rb', line 47

def include?(value)
  @values.include?(@attr.find_value(value))
end

#inspectObject



56
57
58
# File 'lib/enumerize/set.rb', line 56

def inspect
  "#<Enumerize::Set {#{join(', ')}}>"
end

#to_aryObject



35
36
37
# File 'lib/enumerize/set.rb', line 35

def to_ary
  @values.to_a
end