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.

Parameters:

  • types (Array)

    the array of types.



32
33
34
# File 'lib/bake/types/any.rb', line 32

def initialize(types)
	@types = types
end

Class Method Details

.parse(value) ⇒ Object

As a class type, accepts any value.



59
60
61
# File 'lib/bake/types/any.rb', line 59

def self.parse(value)
	value
end

Instance Method Details

#composite?Boolean

Whether any of the listed types is a composite type.

Returns:

  • (Boolean)

    true if any of the listed types is ‘composite?`.



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

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.

Parameters:

  • input (String)

    the input to parse, e.g. ‘“5”`.



50
51
52
53
54
55
56
# File 'lib/bake/types/any.rb', line 50

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.



64
65
66
# File 'lib/bake/types/any.rb', line 64

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

#|(other) ⇒ Object

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

Parameters:

  • other (Type)

    the type instance to append.



38
39
40
# File 'lib/bake/types/any.rb', line 38

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