class Defi::Challenge

This class contains a challenge to apply against an object.

Public Class Methods

new(method, *args, **opts, &block) click to toggle source

Initialize the challenge class.

@param method [#to_sym] The method to send to an object. @param args [Array] Any arguments of the method. @param opts [Hash] Any keyword arguments of the method. @param block [Proc] Any block argument of the method.

# File lib/defi/challenge.rb, line 14
def initialize(method, *args, **opts, &block)
  @method = method.to_sym
  @args   = args
  @opts   = opts
  @block  = block
end

Public Instance Methods

inspect() click to toggle source

A string containing a human-readable representation of the challenge.

@return [String] The human-readable representation of the challenge.

# File lib/defi/challenge.rb, line 83
def inspect
  inspected_method  = @method.inspect
  inspected_args    = @args.inspect
  inspected_opts    = @opts.inspect
  inspected_block   = @block.nil? ? "nil" : "<Proc>"

  "Defi(" \
    "method: #{inspected_method}, " \
    "args: #{inspected_args}, " \
    "opts: #{inspected_opts}, " \
    "block: #{inspected_block}" \
    ")"
end
to(object) click to toggle source

@param object [#object_id] The object to challenge.

@return [Defi::Value] The actual value, to raise or to return.

# File lib/defi/challenge.rb, line 24
def to(object)
  Value.new { object.public_send(@method, *@args, **@opts, &@block) }
end
to!(object) click to toggle source

@param object [#object_id] The object to challenge in code isolation.

@return [Defi::Value] The actual value, to raise or to return.

@see to

# File lib/defi/challenge.rb, line 33
def to!(object)
  ::Aw.fork! { to(object) }
end
to_h() click to toggle source

Properties of the challenge.

@return [Hash] The properties of the challenge.

# File lib/defi/challenge.rb, line 40
def to_h
  {
    method: @method,
    args:   @args,
    opts:   @opts,
    block:  @block
  }
end
to_s() click to toggle source

String of the challenge.

@return [String] The string representation of the challenge.

# File lib/defi/challenge.rb, line 56
def to_s
  string = ".#{@method}"

  return string if @args.empty? && @opts.empty? && @block.nil?

  stringified_args  = @args.inspect[1..-2]
  stringified_opts  = @opts.inspect[1..-2]
  stringified_block = "<Proc>" unless @block.nil?

  string += "("

  stringified_items = []

  stringified_items << stringified_args   unless @args.empty?
  stringified_items << stringified_opts   unless @opts.empty?
  stringified_items << stringified_block  unless @block.nil?

  "#{string}#{stringified_items.join(", ")})"
end