Class: Definition
- Inherits:
-
Object
- Object
- Definition
- Defined in:
- lib/definition.rb
Defined Under Namespace
Modules: Const Classes: Enroller
Instance Attribute Summary collapse
-
#arity ⇒ Object
readonly
Returns the value of attribute arity.
Instance Method Summary collapse
- #assert_args(args) ⇒ Object
- #assert_match(name, meth, implement) ⇒ Object
- #assert_ret(ret) ⇒ Object
-
#initialize(ret_type, root) ⇒ Definition
constructor
A new instance of Definition.
- #raise(exclass, message, *args) ⇒ Object
- #set(arg_types) ⇒ Object
- #to_enroll(table) ⇒ Object
Constructor Details
#initialize(ret_type, root) ⇒ Definition
Returns a new instance of Definition.
31 32 33 34 |
# File 'lib/definition.rb', line 31 def initialize(ret_type, root) @ret_type = ret_type @root = root end |
Instance Attribute Details
#arity ⇒ Object (readonly)
Returns the value of attribute arity.
30 31 32 |
# File 'lib/definition.rb', line 30 def arity @arity end |
Instance Method Details
#assert_args(args) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/definition.rb', line 77 def assert_args(args) start_index = 0 length = args.length args.each_with_index do |arg, i| next if i < start_index break if i >= length arg_type = @arg_types[i] if arg_type == Definition::Const::Rest start_index = @arity + args.length + i + 1 elsif !(arg_type === arg) raise ArgumentError, "arg %i: not %p === %p", i, arg_type, arg end end end |
#assert_match(name, meth, implement) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/definition.rb', line 38 def assert_match(name, meth, implement) begin if meth.respond_to?(:parameters) if meth.parameters.length != @arg_types.length = "%s: arity mismatch (%i for %i)" raise DefinitionError, , name, meth.parameters.length, @arg_types.length end meth.parameters.each_with_index do |param, i| type, var = param arg_type = @arg_types[i] case type when :req, :opt next if arg_type != Const::Rest && arg_type != Const::Block when :rest next if arg_type == Const::Rest when :block next if arg_type == Const::Block end raise DefinitionError, "%s: arg %i: definition mismatch", name, i end elsif meth.arity != @arity = "%s: arity mismatch (%i for %i)" raise DefinitionError, , name, meth.arity, @arity end rescue DefinitionError implement.module_eval { remove_method(name) } Kernel.raise end end |
#assert_ret(ret) ⇒ Object
91 92 93 94 95 96 |
# File 'lib/definition.rb', line 91 def assert_ret(ret) ret_type = @ret_type unless ret_type === ret raise TypeError, "return value: not %p === %p", ret_type, ret end end |
#raise(exclass, message, *args) ⇒ Object
97 98 99 100 101 102 103 104 105 |
# File 'lib/definition.rb', line 97 def raise(exclass, , *args) = sprintf(, *args) if @root.ancestors.include? Interface << " (interface #{@root})" else << " (implementation #{@root})" end super exclass, end |
#set(arg_types) ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/definition.rb', line 68 def set(arg_types) @arg_types = arg_types @arity = arg_types.length if arg_types.last == Const::Block @arity -= 1 @block = true end @arity = -@arity if arg_types.include?(Const::Rest) end |
#to_enroll(table) ⇒ Object
35 36 37 |
# File 'lib/definition.rb', line 35 def to_enroll(table) Enroller.new(self, table) end |