Class: Object

Inherits:
BasicObject
Defined in:
lib/cube/interfaces.rb

Instance Method Summary collapse

Instance Method Details

#check_type(*_) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/cube/interfaces.rb', line 219

def check_type(t, v)
  if t.is_a?(Set)
    unless t.any? { |tp| check_type(tp, v) rescue false }
      raise Cube::Interface::TypeMismatchError,
        "#{v.inspect} is not any of #{tp.to_a}" unless v.is_a?(tp)
    end
    return
  end
  if t.is_a? Array
    raise Cube::Interface::TypeMismatchError,
      "#{v} is not an Array" unless v.is_a? Array
    check_type(t.first, v.first)
    check_type(t.first, v.last)
    return
  end
  raise Cube::Interface::TypeMismatchError, "#{v.inspect} is not type #{t}" unless v.is_a? t
  true
end

#interface(&block) ⇒ Object



209
210
211
212
213
214
215
216
# File 'lib/cube/interfaces.rb', line 209

def interface(&block)
  mod = Module.new
  mod.extend(Cube::Interface)
  mod.instance_variable_set('@ids', {})
  mod.instance_variable_set('@private_ids', {})
  mod.instance_eval(&block)
  mod
end