Module: T

Defined in:
lib/emery/type.rb

Defined Under Namespace

Classes: AnyType, ArrayType, HashType, Nilable, StringFormatted, UnionType, UntypedType

Class Method Summary collapse

Class Method Details

.any(*typdefs) ⇒ Object



164
165
166
# File 'lib/emery/type.rb', line 164

def T.any(*typdefs)
  AnyType.new(*typdefs)
end

.array(item_type) ⇒ Object



156
157
158
# File 'lib/emery/type.rb', line 156

def T.array(item_type)
  ArrayType.new(item_type)
end

.check(type, value) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/emery/type.rb', line 129

def T.check(type, value)
  if type.methods.include? :check
    type.check(value)
  else
    if type != NilClass
      T.check_not_nil(type, value)
    end
    if !value.is_a? type
      raise TypeError.new("Value '#{value.inspect.to_s}' type is #{value.class} - #{type} is required")
    end
  end
  return value
end

.check_not_nil(type, value) ⇒ Object



2
3
4
5
6
# File 'lib/emery/type.rb', line 2

def T.check_not_nil(type, value)
  if value == nil
    raise TypeError.new("Type #{type.to_s} does not allow nil value")
  end
end

.check_var(var_name, type, value) ⇒ Object



172
173
174
175
176
177
178
179
# File 'lib/emery/type.rb', line 172

def T.check_var(var_name, type, value)
  begin
    check(type, value)
    return value
  rescue TypeError => e
    raise TypeError.new("Variable #{var_name} type check failed, expected type: #{type.to_s}, value: #{value}")
  end
end

.hash(key_type, value_type) ⇒ Object



160
161
162
# File 'lib/emery/type.rb', line 160

def T.hash(key_type, value_type)
  HashType.new(key_type, value_type)
end

.instance_of?(type, value) ⇒ Boolean

Returns:



143
144
145
146
147
148
149
150
# File 'lib/emery/type.rb', line 143

def T.instance_of?(type, value)
  begin
    T.check(type, value)
    true
  rescue TypeError
    false
  end
end

.nilable(value_type) ⇒ Object



152
153
154
# File 'lib/emery/type.rb', line 152

def T.nilable(value_type)
  Nilable.new(value_type)
end

.union(*cases) ⇒ Object



168
169
170
# File 'lib/emery/type.rb', line 168

def T.union(*cases)
  UnionType.new(*cases)
end