Module: Typedocs

Defined in:
lib/typedocs.rb,
lib/typedocs.rb,
lib/typedocs/version.rb,
lib/typedocs/block_spec.rb,
lib/typedocs/method_spec.rb

Defined Under Namespace

Modules: DSL, GrammerPrinter, MethodSpec Classes: ArgumentError, ArgumentsSpec, BlockError, BlockSpec, Context, MultiFunctionalInterface, NoSuchMethod, NoSuchType, Parser, RetValError, TypeSpec

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.context(klass) ⇒ Object



65
66
67
68
# File 'lib/typedocs.rb', line 65

def self.context(klass)
  Typedocs.ensure_klass(klass, Module)
  @@contexts[klass] ||= Context.new(klass)
end

.create_method_spec(klass, name, tdoc_arg) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/typedocs.rb', line 54

def self.create_method_spec(klass, name, tdoc_arg)
  case tdoc_arg
  when String
    Typedocs::Parser.new.parse(klass, tdoc_arg)
  when :inherit
    Typedocs.super_method_spec(klass, name) || (raise NoSuchMethod, "can't find typedoc for super method: #{klass}##{name}")
  else
    raise "Unsupported document: #{tdoc_arg.inspect}"
  end
end

.define_spec(klass, name, method_spec) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/typedocs.rb', line 44

def self.define_spec(klass, name, method_spec)
  klass.instance_eval do
    original_method = instance_method(name)
    define_method name do|*args,&block|
      method_spec.call_with_validate original_method.bind(self), *args, &block
    end
  end
  @@method_specs[[klass, name]] = method_spec
end

.ensure_klass(obj, klass) ⇒ Object

Raises:



27
28
29
# File 'lib/typedocs.rb', line 27

def self.ensure_klass(obj, klass)
  raise ArgumentError, "Expected #{klass.name} but #{obj.inspect}" unless obj.kind_of?(klass)
end

.initialize!Object



21
22
23
24
# File 'lib/typedocs.rb', line 21

def self.initialize!
  @@method_specs = {}
  @@contexts = {}
end

.method_spec(klass, name) ⇒ Object



40
41
42
# File 'lib/typedocs.rb', line 40

def self.method_spec(klass, name)
  @@method_specs[[klass, name]]
end

.super_method_spec(klass, name) ⇒ Object

MethodSpec | nil



32
33
34
35
36
37
38
# File 'lib/typedocs.rb', line 32

def self.super_method_spec(klass, name)
  while klass = klass.superclass
    spec = method_spec(klass, name)
    return spec if spec
  end
  nil
end