Advanced Rails - Building Industrial-Strength Web Apps in Record Time

(Tuis.) #1
Metaprogramming Techniques | 27

def initialize(items)
@batcher = Borges::BatchedList.new items, 8
end

def render_content_on(r)
r.list_do @batcher.batch do |item|
r.anchor item.title do choose item end
end

r.render @batcher
end

end # class SushiNet::StoreItemList

The bulk of the action happens in therender_content_onmethod, which uses a
BatchedList(a paginator) to render a paginated list of links to products. But the fun
happens in the call toanchor, which stores away the call tochoose, to be executed
when the corresponding link is clicked.


However, there is still vast disagreement on how useful continuations are for web
programming. HTT Pwas designed as a stateless protocol, and continuations for
web transactions are the polar opposite of statelessness. All of the continuations
must be stored on the server, which takes additional memory and disk space. Sticky
sessions are required, to direct a user’s traffic to the same server. As a result, if one
server goes down, all of its sessions are lost. The most popular Seaside application,
DabbleDB (http://dabbledb.com/), actually uses continuations very little.


Bindings


Bindingsprovide context for evaluation of Ruby code. A binding is the set of vari-
ables and methods that are available at a particular (lexical) point in the code. Any
place in Ruby code where statements may be evaluated has a binding, and that bind-
ing can be obtained withKernel#binding. Bindings are just objects of classBinding,
and they can be passed around as any objects can:


class C
binding # => #<Binding:0x2533c>
def a_method
binding
end
end
binding # => #<Binding:0x252b0>
C.new.a_method # => #<Binding:0x25238>

The Rails scaffold generator provides a good example of the use of bindings:


class ScaffoldingSandbox
include ActionView::Helpers::ActiveRecordHelper
attr_accessor :form_action, :singular_name, :suffix, :model_instance
Free download pdf