Sha256: 2cfa2cf66b49d2f95f0e49ef24bec5862cfd03d41d34e83d459e12e051b32dbc

Contents?: true

Size: 929 Bytes

Versions: 1

Compression:

Stored size: 929 Bytes

Contents

module Daemonizer
  class Option
    class OptionError < StandardError; end

    def initialize(option, value, auto_eval = false)
      @option = option
      @value = value
      @auto_eval = auto_eval
      if @auto_eval && !@value.is_a?(Proc)
        raise OptionError, "auto_apply can be used only with callable option"
      end
    end

    def value(handler = nil)
      if @auto_eval && @value.is_a?(Proc)
        if handler && handler.worker_id && handler.workers_count
          if @value.arity == 0 || @value.arity == -1
            return @value.call
          elsif @value.arity == 2
            return @value.call(handler.worker_id, handler.workers_count)
          else
            raise OptionError, "option lambda should accept 0 or 2 parameters"
          end
        else
          raise OptionError, "value called before handler initialized"
        end
      else
        @value
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
daemonizer-0.5.0.beta.1 lib/daemonizer/option.rb