Class: Defi::Method

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

Overview

Represents a method to be applied against an object. This class encapsulates the method name, its arguments, keyword arguments, and an optional block, enabling dynamic method invocation.

Instance Method Summary collapse

Constructor Details

#initialize(name, *args, **opts, &block) ⇒ Method

Initialize a new Method object.

Parameters:

  • name (Symbol)

    The name of the method.

  • args (Array)

    Any arguments of the method.

  • opts (Hash)

    Any keyword arguments of the method.

  • block (Proc)

    Any block argument of the method.

Raises:

  • (ArgumentError)

    If the name is not a symbol.



15
16
17
18
19
20
21
22
# File 'lib/defi/method.rb', line 15

def initialize(name, *args, **opts, &block)
  raise ::ArgumentError, name.class.inspect unless name.is_a?(::Symbol)

  @name = name
  @args = args
  @opts = opts
  @block = block
end

Instance Method Details

#inspectString

Returns a human-readable representation of the method.

Examples:

add = Defi::Method.new(:+, 1)
add.inspect # => "Defi(name: :+, args: [1], opts: {}, block: nil)"

Returns:

  • (String)

    The human-readable representation of the method.



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/defi/method.rb', line 84

def inspect
  inspected_name  = @name.inspect
  inspected_args  = @args.inspect
  inspected_opts  = @opts.inspect
  inspected_block = @block.nil? ? "nil" : "<Proc>"

  "Defi(" \
    "name: #{inspected_name}, " \
    "args: #{inspected_args}, " \
    "opts: #{inspected_opts}, " \
    "block: #{inspected_block})"
end

#to(object) ⇒ Defi::Value

Applies the method to the given object.

Examples:

add = Defi::Method.new(:+, 1)
add.to(2).call # => 3

Parameters:

  • object (#object_id)

    The object to method.

Returns:

  • (Defi::Value)

    The actual value, to raise or to return.



32
33
34
# File 'lib/defi/method.rb', line 32

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

#to_hHash

Returns a hash containing the method’s properties.

Returns:

  • (Hash)

    The properties of the method.



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

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

#to_sString

Returns a string representation of the method.

Examples:

add = Defi::Method.new(:+, 1)
add.to_s # => ".+(1)"

Returns:

  • (String)

    The string representation of the method.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/defi/method.rb', line 58

def to_s
  string = ".#{@name}"
  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?

  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