Class: Interchange

Inherits:
Module
  • Object
show all
Defined in:
lib/interchange.rb,
lib/interchange/version.rb

Defined Under Namespace

Modules: DefaultImplementationsForInstances, Methods Classes: DownImplementation, ImplementationMissingError, NotAvailableError, NullImplementation, UnregisteredError

Constant Summary collapse

VERSION =
"0.1.0"

Instance Method Summary collapse

Constructor Details

#initialize(*methods) ⇒ Interchange

Returns a new instance of Interchange.



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/interchange.rb', line 99

def initialize(*methods)
  module_eval do
    include Methods
  end

  define_delegate_method :up?

  methods.each do |method|
    define_delegate_method method
  end
end

Instance Method Details

#define_delegate_method(method) ⇒ Object



111
112
113
114
115
116
# File 'lib/interchange.rb', line 111

def define_delegate_method(method)
  define_method method do |*args, &block|
    raise ImplementationMissingError.new(implementation, method) unless implementation.respond_to? method
    implementation.send method, *args, &block
  end
end

#extended(klass) ⇒ Object



93
94
95
96
97
# File 'lib/interchange.rb', line 93

def extended(klass)
  klass.register :null, NullImplementation.new
  klass.register :down, DownImplementation.new
  klass.use :null
end

#included(base) ⇒ Object



89
90
91
# File 'lib/interchange.rb', line 89

def included(base)
  base.send :include, DefaultImplementationsForInstances
end