module NRSER::Types
Stuff to help you define, test, check and match types in Ruby.
Read the documentation {file:lib/nrser/types/README.md here}.
Constants
- AND
- ASSOC
- COMPLEMENT
- COMPLEXES
- GEQ
- INTEGERS
- LEQ
- L_PAREN
Constants
¶ ↑
- NOT
- RATIONALS
- REALS
- RESPONDS_WITH
- R_PAREN
- TILDE_PATH_RE
Regular expression to match “tilde” user-home-relative paths.
@return [Regexp]
- UNION
Public Class Methods
Old bang-less name for {.check!}. We like out bangs around here.
@deprecated
@param (see .check!) @return (see .check!) @raise (see .check!)
# File lib/nrser/types.rb, line 129 def self.check value, type logger.deprecated \ method: __method__, alternative: "NRSER::Types.check!" check! value, type end
Create a {NRSER::Types::Type} from `type` with {.make} and check that `value` satisfies it, raising if it doesn't.
@param [*] value
Value to type check.
@param [*] type
Type to check value against.
@return
The `value` parameter.
@raise [NRSER::Types::CheckError]
If the value fails the type check.
# File lib/nrser/types.rb, line 116 def self.check! value, type make( type ).check! value end
make a type instance from a object representation that can come from a YAML or JSON declaration.
# File lib/nrser/types.rb, line 239 def self.from_repr repr match repr, { str => ->(string) { NRSER::Types.method(string.downcase).call }, Hash => ->(hash) { raise NotImplementedError, "Haven't gotten to it yet!" }, } end
Make a {NRSER::Types::Type} from a value.
If the `value` argument is…
-
a {NRSER::Types::Type}, it is returned.
-
a {Class}, a new {NRSER::Types::IsA} matching that class is returned.
This allows things like
NRSER::Types.check 's', String NRSER::Types.match 's', String, ->(s) { ... }
-
anything else, a new {NRSER::Types::Is} matching that value is returned.
@param [Object] value
@return [NRSER::Types::Type]
# File lib/nrser/types.rb, line 81 def self.make value if value.nil? self.Nil elsif value.is_a? NRSER::Types::Type value else self.When value end end
The {.make} method reference; for easy map and such.
@return [Method]
# File lib/nrser/types.rb, line 96 def self.maker method :make end
My own shitty version of pattern matching!
@todo
Doc this crap.
# File lib/nrser/types.rb, line 179 def self.match value, *clauses if clauses.empty? raise ArgumentError.new NRSER.dedent <<-END Must supply either a single {type => expression} hash argument or a even amount of arguments representing (type, expression) pairs after `value`. #{ NRSER::Version.doc_url 'NRSER/Types#match-class_method' } END end enum = if clauses.length == 1 && clauses.first.respond_to?(:each_pair) clauses.first.each_pair else unless clauses.length % 2 == 0 raise TypeError.new NRSER.dedent <<-END When passing a list of clauses, it must be an even length representing (type, expression) pairs. Found an argument list with length #{ clauses.length }: #{ clauses } END end clauses.each_slice(2) end enum.each { |type, expression| if test? value, type # OK, we matched! Is the corresponding expression callable? if expression.respond_to? :call # It is; invoke and return result. if expression.arity == 0 return expression.call else return expression.call value end else # It's not; assume it's a value and return it. return expression end end } raise TypeError, <<-END.dedent Could not match value #{ value.inspect } to any of types #{ enum.map {|type, expression| "\n #{ type.inspect }"}.join '' } END end
Parse a string into a number.
@return [Integer]
If the string represents a whole integer.
@return [Float]
If the string represents a decimal number.
# File lib/nrser/types/numbers.rb, line 26 def self.parse_number string float = Float string int = float.to_i if float == int then int else float end end
Old question-less name for {.test?}. We like our marks around here.
@param (see .test?) @return (see .test?) @raise (see .test?)
# File lib/nrser/types.rb, line 162 def self.test value, type logger.deprecated \ method: __method__, alternative: "NRSER::Types.test?" test? value, type end
Create a {NRSER::Types::Type} from `type` with {.make} and test if `value` satisfies it.
@param [Object] value
Value to test for membership.
@param [TYPE] type
Type to see if value satisfies. Passed through {.make} to make sure it's a {Type} first.
@return [Boolean]
`true` if `value` satisfies `type`.
# File lib/nrser/types.rb, line 151 def self.test? value, type make(type).test value end
Public Instance Methods
# File lib/nrser/refinements/types.rb, line 4 def t NRSER::Types end
# File lib/nrser/refinements/types.rb, line 8 def to_type NRSER::Types.make self end