Class: MultiType::Group

Inherits:
Object
  • Object
show all
Defined in:
lib/multi_type/group.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klasses) ⇒ Group

Returns a new instance of Group.



6
7
8
# File 'lib/multi_type/group.rb', line 6

def initialize(klasses)
  @klasses = klasses.uniq
end

Instance Attribute Details

#klassesObject (readonly) Also known as: to_a

Returns the value of attribute klasses.



3
4
5
# File 'lib/multi_type/group.rb', line 3

def klasses
  @klasses
end

Instance Method Details

#===(other) ⇒ Object



10
11
12
# File 'lib/multi_type/group.rb', line 10

def ===(other)
  klasses.any? { |k| k === other }
end

#inspectObject



14
15
16
# File 'lib/multi_type/group.rb', line 14

def inspect
  ?< + klasses.map(&:inspect) * ", " + ?>
end

#to_moduleObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/multi_type/group.rb', line 18

def to_module
  group = self

  Module.new do
    extend self

    %i[inspect to_a ===].each do |m|
      define_method m do |*args|
        group.public_send m, *args
      end
    end

    def add(*other)
      MultiType[*self, *other]
    end
  end
end