Class: OptionInitializer::ClassMatch

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

Instance Method Summary collapse

Constructor Details

#initialize(*classes) ⇒ ClassMatch

Returns a new instance of ClassMatch.



70
71
72
# File 'lib/option_initializer.rb', line 70

def initialize *classes
  @classes = Set[*classes]
end

Instance Method Details

#match(object) ⇒ Object



81
82
83
# File 'lib/option_initializer.rb', line 81

def match object
  @classes.any? { |k| object.is_a? k }
end

#to_sObject



85
86
87
88
# File 'lib/option_initializer.rb', line 85

def to_s
  a = @classes.map(&:to_s)
  [a[0...-1].join(', '), a.last].reject(&:empty?).join(', or ')
end

#|(other_class) ⇒ Object



74
75
76
77
78
79
# File 'lib/option_initializer.rb', line 74

def | other_class
  unless other_class.is_a?(Class)
    raise TypeError, "wrong argument type (expected: Class)"
  end
  ClassMatch.new(*@classes.union([other_class]))
end