Module: Sinatra::RPC::Utils

Extended by:
Utils
Included in:
Utils
Defined in:
lib/sinatra/rpc/utils.rb

Instance Method Summary collapse

Instance Method Details

#camelize(string, uppercase_first_letter = true) ⇒ String

Returns the camelcase version of the given string.

Examples:


Sinatra::RPC::Utils.camelize 'my_test_method', false  # => 'myTestMethod'
Sinatra::RPC::Utils.camelize 'test_class'             # => 'TestClass'

16
17
18
19
20
# File 'lib/sinatra/rpc/utils.rb', line 16

def camelize(string, uppercase_first_letter = true)
  tokens = string.to_s.split /_+/
  first_token = (uppercase_first_letter ? tokens.first.capitalize : tokens.first.downcase)
  ([first_token] + tokens[1..-1].map(&:capitalize)).join
end

#method_help(method) ⇒ String

Extract the documentation for a given object method.


40
41
42
# File 'lib/sinatra/rpc/utils.rb', line 40

def method_help(method)
  method.comment.gsub(/^#\s/m, '').strip
end

#method_signature(method) ⇒ String

Extract the signature for a given object method, as a list of string starting with the return type. The information is retrieved only by parsing the documentation, and the value [['nil']] is returned if no documentation is available.


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/sinatra/rpc/utils.rb', line 50

def method_signature(method)
  help = method_help(method)
  params = []
  ret = nil
  help.each_line do |l|
    case l 
    when /@param[\s\w]+\[(\w+).*\]/
      params << $1.downcase
    when /@return[\s\w]+\[(\w+).*\]/
      ret = $1.downcase
    end
  end
  ret ||= 'nil'
  [[ret] + params]
end

#rpc_methods(namespace = nil, object) ⇒ Object

Return a hash with all the methods in an object that can be exposed as RPC methods as keys. The values are themselves hashes containing the object, the name of the Ruby method, a method description and its signature.

Examples:


# A simple class.
class MyClass
  # A simple method.
  # @param folks [String] people to greet
  # @return [String] the greeting
  def greet(folks); "hi, #{folks}!"; end
end

Sinatra::RPC::Utils.rpc_methods 'myclass', MyClass.new
# => {'myclass.myMethod' => {
#      handler:   <MyClass instance>, 
#      method:    :my_method, 
#      help:      "A simple method.\[email protected] folks [String] ...",
#      signature: [['string', 'string']]
#    }

87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/sinatra/rpc/utils.rb', line 87

def rpc_methods(namespace = nil, object)
  public_methods = object.class.instance_methods - Object.instance_methods
  method_index = {}
  public_methods.each do |method_name|
    method = object.class.instance_method(method_name)
    rpc_name = camelize method_name, false
    key = [namespace, rpc_name].compact.join '.'
    method_index[key] = {
      handler: object,
      method:  method_name,
      help: method_help(method),
      signature: method_signature(method)
    }
  end
  method_index
end

#underscore(string) ⇒ String

Converts a camelcase string to its underscore version.


26
27
28
29
30
31
32
33
34
# File 'lib/sinatra/rpc/utils.rb', line 26

def underscore(string)
  word = string.to_s.dup
  word.gsub!(/::/, '/')
  word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
  word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
  word.tr!("-", "_")
  word.downcase!
  word
end