Module: YardTypes

Extended by:
YardTypes
Included in:
YardTypes
Defined in:
lib/yard_types.rb,
lib/yard_types/types.rb,
lib/yard_types/parser.rb,
lib/yard_types/version.rb

Overview

YardTypes provides a parser for YARD type descriptions, and testing whether objects are of the specified types.

Defined Under Namespace

Modules: OrList Classes: CollectionType, DuckType, Failure, HashType, KindType, LiteralType, Parser, Result, Success, TupleType, Type, TypeConstraint

Constant Summary collapse

VERSION =
"0.2.0"

Instance Method Summary collapse

Instance Method Details

#check(type, obj) ⇒ Result

Parses a type identifier with #parse, then validates that the given obj satisfies the type constraint.

Parameters:

  • type (String, Array<String>)

    A YARD type description; see #parse.

  • obj (Object)

    Any object.

Returns:

  • (Result)

    success or failure.


60
61
62
63
64
65
66
67
# File 'lib/yard_types.rb', line 60

def check(type, obj)
  constraint = parse(type)
  if constraint.check(obj)
    Success.new
  else
    Failure.new
  end
end

#parse(type) ⇒ TypeConstraint

Parse a type string using the Parser, and return a TypeConstraint instance representing the described type.

Examples:

type = YardTypes.parse('MyClass, #quacks_like_my_class')
type.check(some_object)

Parameters:

  • type (String, Array<String>)

    The YARD type description

Returns:

Raises:

  • (SyntaxError)

    if the string could not be parsed


49
50
51
52
# File 'lib/yard_types.rb', line 49

def parse(type)
  type = type.join(', ') if type.respond_to?(:join)
  Parser.parse(type)
end