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.


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

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


10
11
12
# File 'lib/enumerize/set.rb', line 10

def values
  @values
end

Instance Method Details

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


28
29
30
31
# File 'lib/enumerize/set.rb', line 28

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

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


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

def ==(other)
  return false unless other.respond_to?(:each)
  other.size == size && other.all? { |v| @values.include?(@attr.find_value(v)) }
end

#delete(value) ⇒ Object


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

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

#encode_with(coder) ⇒ Object


67
68
69
# File 'lib/enumerize/set.rb', line 67

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

#include?(value) ⇒ Boolean

Returns:

  • (Boolean)

54
55
56
# File 'lib/enumerize/set.rb', line 54

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

#inspectObject


63
64
65
# File 'lib/enumerize/set.rb', line 63

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

#textsObject


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

def texts
  @values.map(&:text)
end

#to_aryObject


37
38
39
# File 'lib/enumerize/set.rb', line 37

def to_ary
  @values.to_a
end