Sha256: f86543ddc07049935ce76d36bc6e88fcf90208d2e9736c2e61fa7f7a25e10aa1

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

module Docket
  class RoundRobin

    attr_accessor :storage

    def initialize args={}
      @storage = args[:storage] || Docket::Storage.new('/tmp/docket.rb')
    end

    def set identifier, robins, options={}
      save identifier, robins, options
    end

    def perform identifier, action
      robin = next_robin identifier
      action.call(robin)
    end

    def unset identifier
      unset_key identifier
      unset_from_groups identifier
    end
  
    protected

    def unset_key identifier
      storage.remove identifier
    end

    def unset_from_groups identifier
      groups = storage.read("#{identifier}_groups")
      Array(groups).each do |group|
        old_group = storage.read(group)
        storage.save(group, old_group.reject { |item| item == identifier })
      end

      storage.remove "#{identifier}_groups"
    end

    def next_robin identifier
      list = storage.read(identifier) || []
      robin_list = RobinList.new(list)

      next_robin = robin_list.fetch_next
      save identifier, nil, :list => robin_list.list

      next_robin
    end

    def save identifier, robins, options={}
      list = options[:list] || robins

      storage.save(identifier, list)
      storage.append(options[:group], identifier) if options[:group]
      storage.append("#{identifier}_groups", options[:group]) if options[:group]
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
docket-0.1.2 lib/docket/round_robin.rb