Class: Rack::BertRpc::Mod

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/bert_rpc/mod.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_module) ⇒ Mod

Returns a new instance of Mod.



7
8
9
10
11
# File 'lib/rack/bert_rpc/mod.rb', line 7

def initialize(source_module)
  @source_module = source_module
  @loaded = false
  @funs = {}
end

Instance Attribute Details

#funsObject (readonly)

Returns the value of attribute funs.



4
5
6
# File 'lib/rack/bert_rpc/mod.rb', line 4

def funs
  @funs
end

#loaded=(value) ⇒ Object (writeonly)

Sets the attribute loaded

Parameters:

  • value

    the value to set the attribute loaded to.



5
6
7
# File 'lib/rack/bert_rpc/mod.rb', line 5

def loaded=(value)
  @loaded = value
end

#source_moduleObject (readonly)

Returns the value of attribute source_module.



4
5
6
# File 'lib/rack/bert_rpc/mod.rb', line 4

def source_module
  @source_module
end

Instance Method Details

#call_fun(fun, args) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rack/bert_rpc/mod.rb', line 13

def call_fun(fun, args)
  unless loaded?
    load_module
  end

  if funs[fun].nil?
    raise(ServerError.new("No such function " +
                          "'#{source_module.name}##{fun}'"))
  end

  funs[fun].call(*args)
end

#load_moduleObject



30
31
32
33
34
35
36
37
# File 'lib/rack/bert_rpc/mod.rb', line 30

def load_module
  @context = Object.new
  @context.extend source_module
  source_module.public_instance_methods.each do |meth|
    funs[meth.to_sym] = @context.method(meth)
  end
  self.loaded = true
end

#loaded?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/rack/bert_rpc/mod.rb', line 26

def loaded?
  @loaded
end