Created
November 4, 2014 10:07
-
-
Save tbuehlmann/35a8f1564aa6f4b88624 to your computer and use it in GitHub Desktop.
Overriding invoke_method example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Calculator < MyHandler | |
before_method_invokation :log_method_call | |
expose_all | |
def add(left, right) | |
left + right | |
end | |
private | |
def log_method_call | |
puts "Received Calculator##{json_rpc_request.method} with params: #{json_rpc_request.params}" | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'active_support' | |
class MyHandler | |
include ActiveSupport::Callbacks | |
include JsonRpc::Handler | |
define_callbacks :method_invokation | |
class << self | |
[:before, :around, :after].each do |kind| | |
define_method "#{kind}_method_invokation" do |*method_names| | |
options = method_names.extract_options! | |
_normalize_callback_options(options) | |
method_names.each do |method_name| | |
set_callback :method_invokation, kind, method_name, options | |
end | |
end | |
end | |
private | |
def _normalize_callback_options(options) | |
_normalize_callback_option(options, :only, :if) | |
_normalize_callback_option(options, :except, :unless) | |
end | |
def _normalize_callback_option(options, from, to) | |
if from = options[from] | |
from = Array(from).map { |o| "json_rpc_request.method.split('.').last == '#{o}'" }.join(' || ') | |
options[to] = Array(options[to]).unshift(from) | |
end | |
end | |
end | |
def invoke_method(method_name, params) | |
run_callbacks(:method_invokation) do | |
super | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment