Class: Bake::Types::Any

Inherits:
Object
  • Object
show all
Defined in:
lib/bake/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

Instance Method Summary collapse

Constructor Details

#initialize(types) ⇒ Any

Initialize the instance with an array of types.



17
18
19
# File 'lib/bake/types/any.rb', line 17

def initialize(types)
	@types = types
end

Class Method Details

.parse(value) ⇒ Object

As a class type, accepts any value.



44
45
46
# File 'lib/bake/types/any.rb', line 44

def self.parse(value)
	value
end

Instance Method Details

#composite?Boolean

Whether any of the listed types is a composite type.

Returns:



29
30
31
# File 'lib/bake/types/any.rb', line 29

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.



35
36
37
38
39
40
41
# File 'lib/bake/types/any.rb', line 35

def parse(input)
	@types.each do |type|
		return type.parse(input)
	rescue
		# Ignore.
	end
end

#to_sObject

Generate a readable string representation of the listed types.



49
50
51
# File 'lib/bake/types/any.rb', line 49

def to_s
	"any of #{@types.join(', ')}"
end

#|(other) ⇒ Object

Create a copy of the current instance with the other type appended.



23
24
25
# File 'lib/bake/types/any.rb', line 23

def | other
	self.class.new([*@types, other])
end