class Uber::Options::Value

Public Class Methods

new(value, options={}) click to toggle source
# File lib/uber/options.rb, line 45
def initialize(value, options={})
  @value, @dynamic = value, options[:dynamic]

  @proc     = proc?
  @callable = callable?
  @method   = method?

  return if options.has_key?(:dynamic)

  @dynamic = @proc || @callable || @method
end

Public Instance Methods

call(context, *args) click to toggle source
# File lib/uber/options.rb, line 57
def call(context, *args)
  return @value unless dynamic?

  evaluate_for(context, *args)
end
Also aliased as: evaluate
callable?() click to toggle source
# File lib/uber/options.rb, line 72
def callable?
  @value.is_a?(Uber::Callable)
end
dynamic?() click to toggle source
# File lib/uber/options.rb, line 64
def dynamic?
  @dynamic
end
evaluate(context, *args)
Alias for: call
method?() click to toggle source
# File lib/uber/options.rb, line 76
def method?
  @value.is_a?(Symbol)
end
proc?() click to toggle source
# File lib/uber/options.rb, line 68
def proc?
  @value.kind_of?(Proc)
end

Private Instance Methods

callable!(context, *args) click to toggle source

Callable object is executed in its original context.

# File lib/uber/options.rb, line 101
def callable!(context, *args)
  @value.call(context, *args)
end
evaluate_for(*args) click to toggle source
# File lib/uber/options.rb, line 81
def evaluate_for(*args)
  return proc!(*args)     if @proc
  return callable!(*args) if @callable
  method!(*args)
   # TODO: change to context.instance_exec and deprecate first argument.
end
method!(context, *args) click to toggle source
# File lib/uber/options.rb, line 88
def method!(context, *args)
  context.send(@value, *args)
end
proc!(context, *args) click to toggle source
# File lib/uber/options.rb, line 92
def proc!(context, *args)
  if context.nil?
    @value.call(*args)
  else
    context.instance_exec(*args, &@value)
  end
end