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
-
#initialize(name, *args, **opts, &block) ⇒ Method
constructor
Initialize a new Method object.
-
#inspect ⇒ String
Returns a human-readable representation of the method.
-
#to(object) ⇒ Defi::Value
Applies the method to the given object.
-
#to_h ⇒ Hash
Returns a hash containing the method’s properties.
-
#to_s ⇒ String
Returns a string representation of the method.
Constructor Details
#initialize(name, *args, **opts, &block) ⇒ Method
Initialize a new Method object.
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
#inspect ⇒ String
Returns a 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.
32 33 34 |
# File 'lib/defi/method.rb', line 32 def to(object) Value.new { object.public_send(@name, *@args, **@opts, &@block) } end |
#to_h ⇒ Hash
Returns a hash containing the method’s properties.
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_s ⇒ String
Returns a 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 |