Class: Defi::Method
- Inherits:
- BasicObject
- Defined in:
- lib/defi/method.rb
Overview
Represents a method to be applied against an object. Encapsulates method name, arguments, keyword arguments and blocks for dynamic method invocation with proper error handling.
Instance Method Summary collapse
-
#initialize(name, *args, **opts, &block) ⇒ Method
constructor
Initialize a new Method object.
-
#inspect ⇒ String
Human-readable representation.
-
#to(object) ⇒ Defi::Value
Applies the method to the given object.
-
#to_h ⇒ Hash
Method properties.
-
#to_s ⇒ String
String representation of the method call.
Constructor Details
#initialize(name, *args, **opts, &block) ⇒ Method
Initialize a new Method object.
16 17 18 19 20 21 22 23 |
# File 'lib/defi/method.rb', line 16 def initialize(name, *args, **opts, &block) raise ::ArgumentError, "Method name must be a Symbol, got: #{name.class}" unless name.is_a?(::Symbol) @name = name @args = args @opts = opts @block = block end |
Instance Method Details
#inspect ⇒ String
Returns Human-readable representation.
58 59 60 61 62 63 64 |
# File 'lib/defi/method.rb', line 58 def inspect "Defi(" \ "name: #{@name.inspect}, " \ "args: #{@args.inspect}, " \ "opts: #{@opts.inspect}, " \ "block: #{@block.nil? ? "nil" : "<Proc>"})" end |
#to(object) ⇒ Defi::Value
Applies the method to the given object.
39 40 41 |
# File 'lib/defi/method.rb', line 39 def to(object) Value.new { object.public_send(@name, *@args, **@opts, &@block) } end |
#to_h ⇒ Hash
Returns Method properties.
44 45 46 47 48 49 50 51 |
# File 'lib/defi/method.rb', line 44 def to_h { name: @name, args: @args, opts: @opts, block: @block } end |
#to_s ⇒ String
Returns String representation of the method call.
71 72 73 74 75 |
# File 'lib/defi/method.rb', line 71 def to_s return ".#{@name}" if no_arguments? ".#{@name}(#{stringified_arguments})" end |