Class: Types::Any
- Inherits:
-
Object
- Object
- Types::Any
- Defined in:
- lib/types/any.rb
Overview
An ordered list of types. The first type to match the input is used.
“‘ruby type = Bake::Types::Any(Bake::Types::String, Bake::Types::Integer) “`
Class Method Summary collapse
-
.parse(value) ⇒ Object
As a class type, accepts any value.
Instance Method Summary collapse
-
#composite? ⇒ Boolean
Whether any of the listed types is a composite type.
-
#initialize(types) ⇒ Any
constructor
Initialize the instance with an array of types.
-
#parse(input) ⇒ Object
Parse an input string, trying the listed types in order, returning the first one which doesn’t raise an exception.
-
#to_s ⇒ Object
Generate a readable string representation of the listed types.
-
#|(other) ⇒ Object
Create a copy of the current instance with the other type appended.
Constructor Details
#initialize(types) ⇒ Any
Initialize the instance with an array of types.
33 34 35 |
# File 'lib/types/any.rb', line 33 def initialize(types) @types = types end |
Class Method Details
.parse(value) ⇒ Object
As a class type, accepts any value.
66 67 68 |
# File 'lib/types/any.rb', line 66 def self.parse(value) value end |
Instance Method Details
#composite? ⇒ Boolean
Whether any of the listed types is a composite type.
45 46 47 |
# File 'lib/types/any.rb', line 45 def composite? @types.any?{|type| type.composite?} end |
#parse(input) ⇒ Object
Parse an input string, trying the listed types in order, returning the first one which doesn’t raise an exception.
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/types/any.rb', line 51 def parse(input) @types.each do |type| return type.parse(input) rescue => error last_error = error end if last_error raise last_error else raise ArgumentError, "Unable to parse input #{input.inspect}!" end end |
#to_s ⇒ Object
Generate a readable string representation of the listed types.
71 72 73 |
# File 'lib/types/any.rb', line 71 def to_s "#{@types.join(' | ')}" end |
#|(other) ⇒ Object
Create a copy of the current instance with the other type appended.
39 40 41 |
# File 'lib/types/any.rb', line 39 def | other self.class.new([*@types, other]) end |