Module: Contraction

Defined in:
lib/contraction.rb,
lib/parser.rb,
lib/contract.rb,
lib/parser/type.rb,
lib/parser/lines.rb,
lib/parser/type_parser.rb

Overview

FIXME: There is an aweful lot of knowledge about which kind of TypedLine is being used scattered around the system. I need to encapsulate that better; the abstractions are leaking!

Defined Under Namespace

Modules: Parser Classes: Contract, DuckType, HashType, ReferenceType, SizedContainer, Type, TypeLexer, TypeList, TypeParser, TypedContainer

Class Method Summary collapse

Class Method Details

.class_methods_for(klass) ⇒ Array<Symbol>

Get all the public class methods for a given class that are unique to the class (ie, not built-ins). NOTE: doing something like the following _doesn’t_ make a class method private: class Foo

private
def self.foobar
end

end

That’s because defining a method on an object, even ‘self`, re-opens the object to define the method on it. It’s the same as saying ‘def some_object.foobar`, only in this case `some_object` is the handy-dandy `self`. To make that really private, you can do: class Foo

private
def self.foobar
end
private_class_method :foobar

end

… or … class Foo

private
class << self
    def self.foobar
    end
end

end

Parameters:

  • klass (Class)

    The class to get the methods from

Returns:

  • (Array<Symbol>)

    The method names



69
70
71
# File 'lib/contraction.rb', line 69

def self.class_methods_for(klass)
  klass.public_methods - Object.public_methods - Module.public_methods
end

.included(mod) ⇒ Object

Called by ruby when Contraction is included in a class.



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

def self.included(mod)
  update_contracts(mod)
end

.instance_methods_for(klass) ⇒ Array<Symbol>

Get all the public instance methods for a given class, that are unique to class (ie, not built-ins)

Parameters:

  • klass (Class)

    The class to get the methods from

Returns:

  • (Array<Symbol>)

    The method names



35
36
37
# File 'lib/contraction.rb', line 35

def self.instance_methods_for(klass)
  klass.public_instance_methods - Object.public_instance_methods - Module.public_instance_methods
end

.methods_for(klass) ⇒ Object



73
74
75
# File 'lib/contraction.rb', line 73

def self.methods_for(klass)
  { class: class_methods_for(klass), instance: instance_methods_for(klass) }
end

.update_contracts(mod) ⇒ Object

Call this method to update contracts for any methods that may have been added after the class/module file was loaded by some third-party code. It’s unlikely that you will need this method, but I thought I would include it just in case.

Parameters:

  • mod (Class)

    The module or class to update contracts for.



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/contraction.rb', line 13

def self.update_contracts(mod)
  instance = mod.allocate

  methods_for(mod).each do |(type, methods)|
    methods.each do |method_name|
      file_contents, line_no = read_file_for_method(instance, method_name, type)

      contract = Contraction::Parser.parse(file_contents[0..line_no-2].reverse, mod, method_name, type)
      define_wrapped_method(mod, method_name, contract, type)
    end
  end
end