Skip to content

Commit

Permalink
Declare dynamic methods
Browse files Browse the repository at this point in the history
Two main cases:

- `attr_*` needs `@dynamic`: soutaro/steep#1036
- three `define_method` because of the `RUBY_VERSION` condition
  • Loading branch information
lloeki committed Jul 3, 2024
1 parent a41383f commit 0f12964
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/graft/callback.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

module Graft
class Callback
# @dynamic name
attr_reader :name

# NOTE: opts is not used in the current implementation
Expand All @@ -12,6 +13,7 @@ def initialize(name = nil, opts = {}, &block)
@enabled = true
end

# @dynamic call
# Using `define_method` instead of `def` as the latter trips up static type checking
if RUBY_VERSION < "3.0"
define_method :call do |*args, &block|
Expand Down
2 changes: 2 additions & 0 deletions lib/graft/hook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def self.ignore
Thread.current[:hook_entered] = false
end

# @dynamic point, stack
attr_reader :point, :stack

# NOTE: Push logic upward ClassMethods
Expand Down Expand Up @@ -117,6 +118,7 @@ def uninstall
end

class << self
# @dynamic wrapper
# Using `define_method` instead of `def` as the latter trips up static type checking
if RUBY_VERSION < "3.0"
define_method :wrapper do |hook|
Expand Down
3 changes: 3 additions & 0 deletions lib/graft/hook_point.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def initialize(key)
@key = key
end

# @dynamic key
attr_reader :key

def inspect
Expand Down Expand Up @@ -62,6 +63,7 @@ def strategy_module(strategy)
end
end

# @dynamic klass_name, method_kind, method_name
attr_reader :klass_name, :method_kind, :method_name

def initialize(hook_point, strategy = DEFAULT_STRATEGY)
Expand Down Expand Up @@ -396,6 +398,7 @@ def unchain(suffix)
end
end

# @dynamic apply
# Using `define_method` instead of `def` as the latter trips up static type checking
if RUBY_VERSION < "3.0"
define_method :apply do |obj, suffix, *args, &block|
Expand Down

0 comments on commit 0f12964

Please sign in to comment.