Class: Axiom::Types::Type

Inherits:
Object
  • Object
show all
Extended by:
Options, DescendantsTracker
Defined in:
lib/axiom/types/type.rb

Overview

Abstract base class for every type

Direct Known Subclasses

Object

Class Method Summary collapse

Methods included from Options

accept_options

Class Method Details

.anonymous?Boolean

Test if the type is anonymous

Returns:



157
158
159
# File 'lib/axiom/types/type.rb', line 157

def self.anonymous?
  name.to_s.empty?
end

.baseClass<Axiom::Types::Type>

The base type for the type



139
140
141
# File 'lib/axiom/types/type.rb', line 139

def self.base
  base? ? self : superclass.base
end

.base?Boolean

Test if the type is a base type

Returns:



148
149
150
# File 'lib/axiom/types/type.rb', line 148

def self.base?
  !anonymous?
end

.constraint(constraint = Undefined) {|object| ... } ⇒ Class<Axiom::Types::Type>

Add a constraint to the type

Examples:

with an argument

type.constraint(->(object) { object == 42 }

with a block

type.constraint { |object| object == 42 }

with no arguments

type.constraint  # => constraint

Parameters:

  • constraint (#call) (defaults to: Undefined)

    optional constraint

Yields:

  • (object)

Yield Parameters:

  • object (Object)

    test if the object matches the type constraint

Yield Returns:

  • (Boolean)

    true if the object matches the type constraint

Returns:



113
114
115
116
117
118
# File 'lib/axiom/types/type.rb', line 113

def self.constraint(constraint = Undefined, &block)
  constraint = block if constraint.equal?(Undefined)
  return @constraint if constraint.nil?
  add_constraint(constraint)
  self
end

.finalizeClass<Axiom::Types::Type>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Finalize by deep freezing



58
59
60
61
# File 'lib/axiom/types/type.rb', line 58

def self.finalize
  IceNine.deep_freeze(constraint)
  freeze
end

.include?(object) ⇒ Boolean

Test if the object matches the type constraint

Examples:

type = Axiom::Types::Integer.new do
  minimum 1
  maximum 100
end

type.include?(1)    # => true
type.include?(100)  # => true
type.include?(0)    # => false
type.include?(101)  # => false

Parameters:

Returns:



81
82
83
# File 'lib/axiom/types/type.rb', line 81

def self.include?(object)
  constraint.call(object)
end

.includes(*members) ⇒ undefined

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

TODO:

move into a module

Add a constraint that the object must be included in a set

Parameters:

Returns:

  • (undefined)


129
130
131
132
# File 'lib/axiom/types/type.rb', line 129

def self.includes(*members)
  set = IceNine.deep_freeze(members.to_set)
  constraint(&set.method(:include?))
end

.infer(object) ⇒ Class<Axiom::Types::Type>

Infer the type of the object

Examples:

type = Axiom::Types::Type.infer(Axiom::Types::Integer)
# => Axiom::Types::Integer

Parameters:

Returns:



24
25
26
# File 'lib/axiom/types/type.rb', line 24

def self.infer(object)
  self if equal?(object)
end

.new(*args) {|object| ... } ⇒ Class<Axiom::Types::Type>

Instantiate a new Axiom::Types::Type subclass

Examples:

type = Axiom::Types::Type.new  # => Axiom::Types::Type

Parameters:

  • args (Array(#call))

    optional constraint for the new type

Yields:

  • (object)

Yield Parameters:

  • object (Object)

    test if the object matches the type constraint

Yield Returns:

  • (Boolean)

    true if the object matches the type constraint

Returns:



47
48
49
50
51
# File 'lib/axiom/types/type.rb', line 47

def self.new(*args, &block)
  type = ::Class.new(self, &block)
  type.constraint(*args)
  type.finalize
end