Module: RJR::HandlesMethods::ClassMethods

Defined in:
lib/rjr/util/handles_methods.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#jr_handlersObject

Returns the value of attribute jr_handlers.



39
40
41
# File 'lib/rjr/util/handles_methods.rb', line 39

def jr_handlers
  @jr_handlers
end

Instance Method Details

#create_handler_for(handler_method) ⇒ Object

Create handler for specified method.

Creates a proc that gets evaluated via instance_exec in request



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rjr/util/handles_methods.rb', line 70

def create_handler_for(handler_method)
  @jr_handlers ||= {}
  handler_class = self

  @jr_handlers[handler_method] = proc { |*args|
    # instantiate new handler instance
    jr_instance = handler_class.new

    # setup scope to include request variables
    instance_variables.each { |iv|
      jr_instance.instance_variable_set(iv, instance_variable_get(iv))
    }

    # invoke handler method
    jr_instance.method(handler_method).call *args
  }
end

#dispatch_to(dispatcher) ⇒ Object

Register locally stored methods w/ the specified dispatcher



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/rjr/util/handles_methods.rb', line 99

def dispatch_to(dispatcher)
  @jr_method_args.each { |args|
    # copy args so original is preserved
    handler_method, jr_methods =
      extract_handler_method(Array.new(args))
    jr_methods.map! { |m| m.to_s }

    handler = has_handler_for?(handler_method) ?
                   handler_for(handler_method) :
            create_handler_for(handler_method)

    dispatcher.handle jr_methods, handler
  }
end

#extract_handler_method(args) ⇒ Object

Return the handler method matching the argument set



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rjr/util/handles_methods.rb', line 42

def extract_handler_method(args)
  handler = nil

  if method_defined?(args.last)
    handler = args.last
    args.delete_at(-1)

  else
    handler = :handle
  end

  [handler, args]
end

#handler_for(handler_method) ⇒ Object

Returns handler for specified method



63
64
65
# File 'lib/rjr/util/handles_methods.rb', line 63

def handler_for(handler_method)
  @jr_handlers[handler_method]
end

#has_handler_for?(handler_method) ⇒ Boolean

Return bool indicating if handler exists for the specified method

Returns:

  • (Boolean)


57
58
59
60
# File 'lib/rjr/util/handles_methods.rb', line 57

def has_handler_for?(handler_method)
  @jr_handlers ||= {}
  @jr_handlers.has_key?(handler_method)
end

#jr_method(*args) ⇒ Object

Register one or more json-rpc methods.

Invoke w/ list of method signatures to match in dispatcher w/ optional id of local method to dispatch to. If no method specified, the :handle method will be used



93
94
95
96
# File 'lib/rjr/util/handles_methods.rb', line 93

def jr_method(*args)
  @jr_method_args  ||= []
  @jr_method_args  << args
end