Class: Mixture::Types::Type

Inherits:
Object
  • Object
show all
Defined in:
lib/mixture/types/type.rb

Overview

A single type. A type is never instantized; it is used as a class to represent a type of value.

Direct Known Subclasses

Object

Class Method Summary collapse

Class Method Details

.anonymous?Boolean

If this class is anonymous. This is counting on the fact that the name of an anonymous class is nil; however, assigning a class to a constant on initialization of a class will make that class non-anonymous.

Returns:



92
93
94
# File 'lib/mixture/types/type.rb', line 92

def self.anonymous?
  name.nil?
end

.as(*names) ⇒ void

This method returns an undefined value.

Sets some names that this type can go under. This is used for mappings and for inference.

Parameters:

See Also:



43
44
45
# File 'lib/mixture/types/type.rb', line 43

def self.as(*names)
  mappings.concat(names)
end

.self.constraint(value) ⇒ void .self.constraint {|value| ... } ⇒ void

Note:

Constraints are not meant for validation. Constraints are purely meant for identification, and should be used as such.

This is used to determine if a specific object is this type. There can be many constraints, and they're all used to check the given object.

Overloads:

  • .self.constraint(value) ⇒ void

    This method returns an undefined value.

    Adds the value as a constraint. Ideally, this should respond to #call.

    Examples:

    Subclass constraint.

    constraint(->(value) { value.is_a?(String) })

    Parameters:

    • value (#call)

      The constraint to add.

  • .self.constraint {|value| ... } ⇒ void

    This method returns an undefined value.

    Adds the block as a constraint.

    Examples:

    Subclass constraint.

    constraint { |value| value.is_a?(String) }

    Yields:

    • (value)

    Yield Parameters:

    • value (Object)

      The value to check.

    Yield Returns:

    • (Boolean)

      If the constraint was passed.



140
141
142
143
144
145
146
147
148
149
# File 'lib/mixture/types/type.rb', line 140

def self.constraint(value = Undefined, &block)
  if block_given?
    constraints << block
  elsif value != Undefined
    constraints << value
  else
    fail ArgumentError, "Expected an argument or a block, " \
      "got neither"
  end
end

.constraintsArray<Proc{(Object) => Boolean}>

Constraints on the type. A value will not match the type if any of these constraints fail. This is inherited by subtypes.

Returns:



23
24
25
# File 'lib/mixture/types/type.rb', line 23

def self.constraints
  @constraints ||= ThreadSafe::Array.new
end

.inheritableArray<Class>

A list of types that this type can inherit coercion behavior from. For example, a collection can be coerced into an array or a set.

Returns:



82
83
84
# File 'lib/mixture/types/type.rb', line 82

def self.inheritable
  ancestors - Type.ancestors
end

.inherited(sub) ⇒ void

This method returns an undefined value.

Called by ruby when a class inherits this class. This just propogates the options and constraints to the new subclass.

Parameters:

  • sub (Class)

    The new subclass.



52
53
54
55
# File 'lib/mixture/types/type.rb', line 52

def self.inherited(sub)
  sub.options.merge!(options)
  sub.constraints.concat(constraints)
end

.inspectString

Inspects the class. If the class is anonymous, it uses the :name value in the options if it exists. Otherwise, it passes it up the chain.

Returns:



101
102
103
# File 'lib/mixture/types/type.rb', line 101

def self.inspect
  to_s
end

.mappingsArray<Symbol, Object>

Returns all of the names that this type can go under. This is used for Mixture::Types.mappings and for inference.

Returns:

See Also:



32
33
34
# File 'lib/mixture/types/type.rb', line 32

def self.mappings
  @mappings ||= ThreadSafe::Array.new
end

.matches?(value) ⇒ Boolean

Checks if the given value passes all of the constraints defined on this type. Each constraint is executed within the context of the class, to provide access to options.

Parameters:

  • value (Object)

    The object to check.

Returns:



71
72
73
74
75
# File 'lib/mixture/types/type.rb', line 71

def self.matches?(value)
  constraints.all? do |constraint|
    class_exec(value, &constraint)
  end
end

.optionsHash{Symbol => Object}

The options for the type. This is inherited by subtypes.

Returns:



15
16
17
# File 'lib/mixture/types/type.rb', line 15

def self.options
  @options ||= ThreadSafe::Hash.new
end

.registervoid

This method returns an undefined value.

Registers the type. This shouldn't be called on anonymous classes.



61
62
63
# File 'lib/mixture/types/type.rb', line 61

def self.register
  Types.types << self
end

.to_sString

Inspects the class. If the class is anonymous, it uses the :name value in the options if it exists. Otherwise, it passes it up the chain.

Returns:



106
107
108
109
110
111
112
# File 'lib/mixture/types/type.rb', line 106

def self.to_s
  if anonymous? && options.key?(:name)
    options[:name]
  else
    super
  end
end