Class: Defi::Challenge

Inherits:
BasicObject
Defined in:
lib/defi/challenge.rb

Overview

This class contains a challenge to apply against an object.

Instance Method Summary collapse

Constructor Details

#initialize(method, *args, **opts, &block) ⇒ Challenge

Initialize the challenge class.

Parameters:

  • method (#to_sym)

    The method to send to an object.

  • args (Array)

    Any arguments of the method.

  • opts (Hash)

    Any keyword arguments of the method.

  • block (Proc)

    Any block argument of the method.



14
15
16
17
18
19
# File 'lib/defi/challenge.rb', line 14

def initialize(method, *args, **opts, &block)
  @method = method.to_sym
  @args   = args
  @opts   = opts
  @block  = block
end

Instance Method Details

#inspectString

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

Returns:

  • (String)

    The human-readable representation of the challenge.



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/defi/challenge.rb', line 75

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) ⇒ Defi::Value

Returns The actual value, to raise or to return.

Parameters:

  • object (#object_id)

    The object to challenge.

Returns:

  • (Defi::Value)

    The actual value, to raise or to return.



24
25
26
# File 'lib/defi/challenge.rb', line 24

def to(object)
  Value.new { object.public_send(@method, *@args, **@opts, &@block) }
end

#to!(object) ⇒ Defi::Value

Returns The actual value, to raise or to return.

Parameters:

  • object (#object_id)

    The object to challenge in code isolation.

Returns:

  • (Defi::Value)

    The actual value, to raise or to return.

See Also:



33
34
35
# File 'lib/defi/challenge.rb', line 33

def to!(object)
  ::Aw.fork! { to(object) }
end

#to_hHash

Properties of the challenge.

Returns:

  • (Hash)

    The properties of the challenge.



40
41
42
43
44
45
46
47
# File 'lib/defi/challenge.rb', line 40

def to_h
  {
    method: @method,
    args:   @args,
    opts:   @opts,
    block:  @block
  }
end

#to_sString

String of the challenge.

Returns:

  • (String)

    The string representation of the challenge.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/defi/challenge.rb', line 52

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