Class: Monolens::Type::Any

Inherits:
Object
  • Object
show all
Includes:
ErrorHandling
Defined in:
lib/monolens/type/any.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ErrorHandling

#fail!

Constructor Details

#initialize(candidates) ⇒ Any

Returns a new instance of Any.



6
7
8
# File 'lib/monolens/type/any.rb', line 6

def initialize(candidates)
  @candidates = candidates
end

Class Method Details

.===(instance) ⇒ Object



18
19
20
# File 'lib/monolens/type/any.rb', line 18

def self.===(instance)
  true
end

.dress(instance, registry) ⇒ Object



14
15
16
# File 'lib/monolens/type/any.rb', line 14

def self.dress(instance, registry)
  instance
end

.of(*candidates) ⇒ Object



10
11
12
# File 'lib/monolens/type/any.rb', line 10

def self.of(*candidates)
  Any.new(candidates)
end

Instance Method Details

#===(instance) ⇒ Object



22
23
24
# File 'lib/monolens/type/any.rb', line 22

def ===(instance)
  @candidates.any?{|c| c === instance }
end

#dress(instance, registry, &block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/monolens/type/any.rb', line 26

def dress(instance, registry, &block)
  first_error = nil
  @candidates.each do |candidate|
    begin
      return candidate.dress(instance, registry, &block)
    rescue TypeError => ex
      first_error ||= ex
    end
  end
  fail!(first_error.message, &block)
end