này thực hiện của tôi để phát triển cách để chạy mã trước khi tất cả các phương pháp trong mô hình của bạngọi trước khi phương pháp trong mô hình trên ruby
Cuộc gọi "before_hook: months_used" phương pháp cần phải được trên dưới cùng của lớp học để các ExecutionHooks có thể nhận được instance_method được tải trong module. Tôi muốn tải các phương pháp dụ trên
class BalanceChart < BalanceFind
include ExecutionHooks
attr_reader :options
def initialize(options = {})
@options = options
@begin_at = @options[:begin_at]
end
def months_used
range.map{|date| I18n.l date, format: :month_year}.uniq!
end
before_hook :months_used
end
module ExecutionHooks
def self.included(base)
base.send :extend, ClassMethods
end
module ClassMethods
def before
@hooks.each do |name|
m = instance_method(name)
define_method(name) do |*args, &block|
return if @begin_at.blank? ## the code you can execute before methods
m.bind(self).(*args, &block) ## your old code in the method of the class
end
end
end
def before_hook(*method_name)
@hooks = method_name
before
end
def hooks
@hooks ||= []
end
end
end
Bạn có nghĩa là bạn muốn gọi before_hook ở đầu lớp học của mình? –
Có @FrederickCheung –