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?



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

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



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

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

#encode_with(coder) ⇒ Object



65
66
67
# File 'lib/enumerize/set.rb', line 65

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

#include?(value) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#inspectObject



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

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

#textsObject



39
40
41
# File 'lib/enumerize/set.rb', line 39

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

#to_aryObject



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

def to_ary
  @values.to_a
end