Class: Interfaces::Interface

Inherits:
Object
  • Object
show all
Defined in:
lib/interfaces/interface.rb

Overview

Interface is meant to be used as a base class for the definition of Interfaces

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Interface

allow interfaces to be instantiated directly by passing in values for each of the abstract methods



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/interfaces/interface.rb', line 60

def initialize(opts = {})
  opts.each_pair do |key, value|

    # only allow initializers for abstract methods
    unless self.class.abstract_methods.include?(key.to_sym) || self.class.optional_methods.include?(key.to_sym)
      raise InterfaceError, "Attempted to assign value to method '#{key}' which is not an abstract or optional method of #{self.class}"
    end

    if value.is_a?(Proc)
      # if the value is a proc then the abstract method
      # override should call that proc
      self.define_singleton_method(key, &value)
    else
      # if the value is not a proc, then the abstract
      # method override should just return the value
      self.define_singleton_method(key) { value }
    end
  end
end

Class Method Details

.abstract(*methods) ⇒ Object

Define methods as asbtract



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/interfaces/interface.rb', line 7

def self.abstract(*methods)
  @abstract_methods ||= Set.new
  methods.each do |method|
    # the default implmentation of an abstract method is to
    # raise the AbstractMethodInvokedError exception
    define_method(method) do
      raise AbstractMethodInvokedError, "Abstract method #{method} called"
    end
    @abstract_methods << method.to_sym
  end
end

.abstract?Boolean

True if a class has abstract methods

Returns:

  • (Boolean)


33
34
35
# File 'lib/interfaces/interface.rb', line 33

def self.abstract?
  !abstract_methods.empty?
end

.abstract_methodsObject

Lists all abstract methods of a class



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/interfaces/interface.rb', line 20

def self.abstract_methods
  if self == Interface
    []
  else
    # determine which methods of superclass are still undefined
    nonoverrided_superclass_abtract_methods = superclass.abstract_methods.reject do |m|
      self.instance_methods.include?(m) && self.instance_method(m).owner == self
    end
    ( (@abstract_methods || Set.new) + nonoverrided_superclass_abtract_methods ).to_a
  end
end

.optional(*methods) ⇒ Object

Define methods as optional



47
48
49
50
51
52
53
54
55
56
# File 'lib/interfaces/interface.rb', line 47

def self.optional(*methods)
  @optional_methods ||= Set.new
  methods.each do |method|
    # the default implmentation of an optional method is to return nil
    define_method(method) do
      nil
    end
    @optional_methods << method.to_sym
  end
end

.optional_methodsObject

List all optional methods of a class



38
39
40
41
42
43
44
# File 'lib/interfaces/interface.rb', line 38

def self.optional_methods
  if self == Interface
    []
  else
    ( (@optional_methods || Set.new) + superclass.optional_methods ).to_a
  end
end