SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 253 | Next

Ola Bini

"Practical JRuby on Rails Web 2.0 Projects: Bringing Ruby on Rails to Java"


At the moment, you??™ll instead create the main class that will be used for handling the
sequences. I call it BBEngine:
class BBEngine
def initialize(uid, cred, method)
log "inited: #{uid.inspect}, #{cred.inspect}, #{method.inspect}"
unless authorized?(uid, cred, method)
log "-- not authorized"
raise "Not authorized"
end
@user = AuthorizedUser.find_by_uid(uid)
@method = method.to_sym
end
private
def log str
$stderr.puts str
end
def authorized?(uid, cred, method)
v1 = AuthorizedUser.find_by_uid_and_password(uid,cred)
if v1 && (v = v1.authorizations.find_by_operation(method.to_s))
(!v.respond_to?(:length)) || (v.length > 0)
else
false
end
end
end
CHAPTER 9 ?–  A JRUBY ENTERPRISE BEAN 171
Notice that you create a log method that you use in several places. Currently, you just
print the log message to stderr, but later you should probably make this output go to a file
instead. The initialize method takes a UID, a password, and a method name. It checks if
the user can perform that method and then raises an error if not. Otherwise it just saves the
information.
The only thing missing here is the central invoke method. It looks like this:
def invoke(arg)
log "-- invoked: #{arg.inspect}"
case @method
when :next: Sequence.next(@user,arg)
when :last_updated: Sequence.last_updated(arg)
when :last_updated_by: Sequence.last_updated_by(arg)
when :reset: Sequence.reset(@user,arg)
when :create: Sequence.ensure_exists(@user, arg)
when :current: Sequence.


Pages:
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265