examples/hello.rb in spqr-0.0.4 vs examples/hello.rb in spqr-0.1.0
- old
+ new
@@ -1,41 +1,72 @@
-require 'rubygems'
require 'spqr/spqr'
require 'spqr/app'
require 'logger'
class Hello
include SPQR::Manageable
- def hello(args)
- @people_greeted ||= 0
+
+ # Input (in and inout) parameters are passed to exposed methods in
+ # the order they are declared in the expose block (see below).
+ def hello(name)
+ # We will keep track of the number of people we have greeted over
+ # the lifetime of this agent (see below for the people_greeted
+ # statistic declaration).
@people_greeted = @people_greeted + 1
- args["result"] = "Hello, #{args['name']}!"
+
+ # In methods that return only one value --- that is, those which
+ # have only one parameter that is either out or inout --- the
+ # return value is provided normally. Methods that have multiple
+ # out and inout parameters should return a list that includes the
+ # returned values in the order they are specified in the expose block.
+ "Hello, #{name}!"
end
- spqr_expose :hello do |args|
+ # This block indicates that we intend to expose the "hello" method
+ # over QMF, that "name" is the first (and only) formal input
+ # parameter, and "result" is the first (and only) formal output
+ # parameter. Argument declarations include a name, a type, a
+ # direction, and optional keyword arguments.
+ expose :hello do |args|
args.declare :name, :lstr, :in
args.declare :result, :lstr, :out
end
- # This is for the service_name property
+ # This is the method that will be called to get the value of the
+ # service_name property
def service_name
@service_name = "HelloAgent"
end
- spqr_package :hello
- spqr_class :Hello
- spqr_statistic :people_greeted, :int
- spqr_property :service_name, :lstr
+ # The following two declarations provide the QMF package and class
+ # names for the published class. These can be provided either as
+ # symbols or as strings. If the class name is omitted, it will be
+ # generated from the class name.
+ qmf_package_name :hello
+ qmf_class_name :Hello
+
+ # The following two declarations create named QMF statistics and
+ # properties with given types. The value for a statistic or
+ # property is taken from the return values of the instance method
+ # with the same name as that statistic or property. If a method
+ # with this name does not exist, one is created with attr_reader
+ # and attr_writer.
+ qmf_statistic :people_greeted, :int
+ qmf_property :service_name, :lstr
# These should return the same object for the lifetime of the agent
# app, since this example has no persistent objects.
def Hello.find_all
@@hellos ||= [Hello.new]
end
def Hello.find_by_id(id)
@@hellos ||= [Hello.new]
@@hellos[0]
+ end
+
+ def initialize
+ @people_greeted = 0
end
end
app = SPQR::App.new(:loglevel => :debug)
app.register Hello