Module: Cube
- Defined in:
- lib/cube/interfaces.rb,
lib/cube/traits.rb,
lib/cube/toplevel.rb
Overview
Top level module for RubyCube
Defined Under Namespace
Modules: CubeMethods, Interface, Trait
Class Method Summary collapse
- .[](mod) ⇒ Object (also: from)
- .check_type(*_) ⇒ Object
- .check_type_spec(t, v, &blk) ⇒ Object
- .interface(&block) ⇒ Object
- .mark_interface!(cls, iface) ⇒ Object
- .trait(&blk) ⇒ Object
Class Method Details
.[](mod) ⇒ Object Also known as: from
8 9 10 11 12 13 14 |
# File 'lib/cube/toplevel.rb', line 8 def self.[](mod) return mod if mod.is_a?(CubeMethods) unless mod.is_a?(Class) raise ArgumentError, "Only classes can be be converted to Cube classes" end Class.new(mod).extend(CubeMethods) end |
.check_type(*_) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/cube/toplevel.rb', line 61 def self.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 #{t.to_a}" 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 |
.check_type_spec(t, v, &blk) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/cube/toplevel.rb', line 35 def self.check_type_spec(t, v, &blk) if t.is_a?(Set) if v.is_a?(Set) if v != t raise Cube::Interface::TypeMismatchError, "#{t.to_a} is not eql to #{v.to_a}" end return true end unless t.any? { |tp| check_type(tp, v, &blk) rescue false } raise Cube::Interface::TypeMismatchError, "#{v.inspect} is not any of #{t.to_a}" 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, &blk) check_type(t.first, v.last, &blk) return end raise Cube::Interface::TypeMismatchError, "#{v.inspect} is not type #{t}" unless blk.call(t, v) true end |
.interface(&block) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/cube/toplevel.rb', line 20 def self.interface(&block) mod = Module.new mod.extend(Cube::Interface) mod.instance_variable_set('@ids', {}) mod.instance_eval(&block) mod end |
.mark_interface!(cls, iface) ⇒ Object
2 3 4 5 6 |
# File 'lib/cube/toplevel.rb', line 2 def self.mark_interface!(cls, iface) Cube[cls].as_interface(iface, runtime_checks: false) cl_iface = iface.impotent cls.include(cl_iface) end |
.trait(&blk) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/cube/toplevel.rb', line 28 def self.trait(&blk) m = Module.new m.extend(Cube::Trait) m.module_exec(&blk) if block_given? m end |