Class: GoodGuide::Gibbon::StaticClient
- Inherits:
-
AbstractClient
- Object
- AbstractClient
- GoodGuide::Gibbon::StaticClient
- Includes:
- Util
- Defined in:
- lib/goodguide/gibbon.rb
Class Method Summary collapse
-
.query(type, opts = {}, &impl) ⇒ Object
Define custom queries to be made avaialble in Gibbon clients.
Instance Method Summary collapse
Methods included from Util
#hash_to_ruby, #obj_to_js, #obj_to_ruby
Methods inherited from AbstractClient
Class Method Details
.query(type, opts = {}, &impl) ⇒ Object
Define custom queries to be made avaialble in Gibbon clients. Note: DO NOT use ‘return` within these blocks. Because they’re not functions, this results in a ‘LocalJumpError` (in MRI ruby) or, even worse, silently kills the current thread (in JRuby).
E.g., BAD: query :foo do |arg|
return false unless arg.present?
# ...
end
GOOD: query :foo do |arg|
break false unless arg.present?
# ...
end
364 365 366 367 368 369 370 371 372 373 374 375 376 |
# File 'lib/goodguide/gibbon.rb', line 364 def self.query(type, opts={}, &impl) arity = opts.fetch(:arity, 1) super type, opts do |input, args, builder| if args.size != arity query_error!( "wrong number of arguments: " + "#{args.size} for #{arity} (in .#{type} #{args.join(' ')})" ) end self.instance_exec(input, *args, builder, &impl) end end |
Instance Method Details
#analyze_query(input_id, query, t) ⇒ Object
378 379 380 381 382 383 |
# File 'lib/goodguide/gibbon.rb', line 378 def analyze_query(input_id, query, t) query_impl = get_query(query.type) or query_error! "no such query #{query.type.inspect}" analysis = instance_exec(input_id, obj_to_ruby(query.args), t, &query_impl) analysis[:annotations]['_query_type'] = query.type analysis end |
#query_error!(*a) ⇒ Object
343 344 345 |
# File 'lib/goodguide/gibbon.rb', line 343 def query_error!(*a) raise StaticError.new(*a) end |
#to_js(gibbon) ⇒ Object
385 386 387 388 389 390 391 392 393 394 395 396 397 |
# File 'lib/goodguide/gibbon.rb', line 385 def to_js(gibbon) analyze_proc = proc do |*args| args.shift unless defined?(JRUBY_VERSION) begin { :success => true, :analysis => analyze_query(*args) } rescue GibbonError => e { :success => false, :error => e.to_js(gibbon) } end end { :analyzeQuery => analyze_proc } end |