Class: V::Operations

Inherits:
Module
  • Object
show all
Defined in:
lib/v/operation.rb

Constant Summary collapse

TO_SYM_DEFN =
%q"def self.to_sym; :'%s' end"
DEF_SHORTCUT =
<<-RUBY
  def %s(*args, &callback)
    schedule (self.class)::Operations.new(:%s, *args, &callback)
  end
RUBY

Instance Method Summary collapse

Constructor Details

#initialize(load_path) ⇒ Operations

Returns a new instance of Operations.



5
6
7
# File 'lib/v/operation.rb', line 5

def initialize(load_path)
  @load_path, @operations = load_path, {}
end

Instance Method Details

#included(base) ⇒ Object



9
10
11
12
13
14
# File 'lib/v/operation.rb', line 9

def included(base)
  base.const_set :Operations, self

  class_path = File.join @load_path, 'operations', '**', '*'
  Dir[ class_path ].each { |p| require p }
end

#new(op_sym, *args, &callback) ⇒ Object



32
33
34
35
36
37
# File 'lib/v/operation.rb', line 32

def new(op_sym, *args, &callback)
  op_class = @operations[op_sym] or
  V::ENOOP.raise(op_sym)

  op_class.new(*args, &callback)
end

#operation(op_sym, &defn) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/v/operation.rb', line 22

def operation(op_sym, &defn)
  this_module = self
  op = Class.new(Operation) { const_set :Operations, this_module }
  op.class_eval TO_SYM_DEFN % "#{ op_sym }".gsub('_', '-')
  op.class_eval(&defn)

  module_eval DEF_SHORTCUT % [ op_sym, op_sym ]

  @operations[ op_sym ] = op
end