Class: Beanstalker::Mapper

Inherits:
Object
  • Object
show all
Defined in:
lib/beanstalker/mapper.rb

Defined Under Namespace

Classes: Error, KindUnknown, NotAcceptable, TaskUnknown, WithBlockNotPresent

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Mapper

Returns a new instance of Mapper.



9
10
11
12
13
14
15
# File 'lib/beanstalker/mapper.rb', line 9

def initialize(*args)
  @mapping = {}
  @current_kind = nil
  args.each do |filename|
    include_from_file(filename)
  end
end

Instance Method Details

#can_handle_kind?(kind) ⇒ Boolean

Returns:

  • (Boolean)


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

def can_handle_kind?(kind)
  @mapping && !! @mapping[kind.to_sym]
end

#include_from_file(filename) ⇒ Object



17
18
19
# File 'lib/beanstalker/mapper.rb', line 17

def include_from_file(filename)
  instance_eval(File.read(filename), filename)
end

#method_for(kind, name) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/beanstalker/mapper.rb', line 39

def method_for(kind, name)
  kind_mapping = @mapping[kind.to_sym]
  if kind_mapping.nil?
    raise KindUnknown, "No handler for kind = #{kind.to_sym.inspect}"
  end
  task_mapping = kind_mapping[name.to_sym]
  if task_mapping.nil?
    raise TaskUnknown, "No handler for task = #{name.to_sym.inspect}"
  end
  task_mapping
end

#on(name, &block) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/beanstalker/mapper.rb', line 21

def on(name, &block)
  if @current_kind.nil?
    raise WithBlockNotPresent, "Wrap #on calls with #with block to setup :kind of task in beanstalker_mapper.rb"
  end
  @mapping[@current_kind] ||= {}
  @mapping[@current_kind][name.to_sym] = block
end

#with(kind, &block) ⇒ Object



29
30
31
32
33
# File 'lib/beanstalker/mapper.rb', line 29

def with(kind, &block)
  @current_kind = kind.to_sym
  block.call
  @current_kind = nil
end