Module: Fried::Typings::Type

Included in:
ArrayOf, EnumeratorOf, HashOf, Is, IsStrictly, OneOf, StrictlyOneOf, TupleOf
Defined in:
lib/fried/typings/type.rb

Overview

Protocol to validate object type

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



7
8
9
10
11
# File 'lib/fried/typings/type.rb', line 7

def self.included(klass)
  klass.instance_eval do
    include Comparable
  end
end

Instance Method Details

#<=>(other) ⇒ Object

Compares own class with other object (which should be a class)

Parameters:

  • other (Class, Module)


15
16
17
# File 'lib/fried/typings/type.rb', line 15

def <=>(other)
  self.class <=> other
end

#call(obj) ⇒ Object

Returns the passed object.

Parameters:

  • obj

    any object

Returns:

  • the passed object

Raises:

  • (TypeError)

    should be raised if #valid? is false



22
23
24
25
# File 'lib/fried/typings/type.rb', line 22

def call(obj)
  invalid_type!(obj) unless valid?(obj)
  obj
end

#valid?(obj) ⇒ Boolean

Returns true if object is of valid type.

Parameters:

  • obj

    any object

Returns:

  • (Boolean)

    true if object is of valid type

Raises:

  • (NotImplementedError)


29
30
31
# File 'lib/fried/typings/type.rb', line 29

def valid?(obj)
  raise NotImplementedError
end