Sha256: 69cf96bd7e0f8a038a8406c52a4f680094adb226657096ada219274ea5f209b7

Contents?: true

Size: 1.38 KB

Versions: 2

Compression:

Stored size: 1.38 KB

Contents

module Beanstalker
  class Mapper
    class Error < StandardError; end
    class WithBlockNotPresent < Error; end
    class NotAcceptable < Error; end
    class KindUnknown < NotAcceptable; end
    class TaskUnknown < NotAcceptable; end

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

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

    def on(name, &block)
      $logger.info "We are processing #{@current_kind}/#{name} now"
      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

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

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

    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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
beanstalker-0.5.1 lib/beanstalker/mapper.rb
beanstalker-0.5.0 lib/beanstalker/mapper.rb