51.2.1. Div a Tofu

Příklad 51.10. FirstDiv.rb

#!/usr/bin/env ruby
# $Id: FirstDiv.rb,v 1.2 2002/09/15 07:21:55 radek Exp $
#
# Copyright (C) 2002 Radek Hnilica

$:.unshift('/home/radek/lib/ruby')
require 'tofu/tofulet'
require 'drb/drb'


def setup_bartender(monolithic=true)
    if monolithic
        require 'yourapp'
        Tofu::Bartender.new(YourTofuSession)
    else
        DRbObject.new(nil, 'druby://localhost:7642')
    end
end

def main(monolithic=true)
    DRb.start_service
    logger = WEBrick::Log::new($stderr, WEBrick::Log::DEBUG)

    s = WEBrick::HTTPServer.new(
        :Port       => 2001,
        :Loger      => logger
    )

    bartender = setup_bartender(monolithic)
    s.mount("/div", WEBrick::Tofulet, bartender)

    trap("INT") { s.shutdown }
    s.start
end

###
main

	    

Příklad 51.11. yourapp.rb

#!/usr/bin/env ruby
# $Id: yourapp.rb,v 1.1 2002/06/07 14:02:21 radek Exp $

require 'div/div'
require 'div/tofusession'

class SumTotal
  def initialize
    @history = []
    @amount = 0
  end
  attr_reader :history, :amount

  def add(value)
    f = value.to_f
    @history.push(f)
    @amount += f
  end
  
  def undo
    tail = @history.pop
    return unless tail
    @amount -= tail
  end
end

class SumDiv < Div::Div
  set_erb('sum.erb')

  def initialize(session)
    super(session)
    @model = SumTotal.new
  end

  def do_add(context, params)
    value, = params['value']
    @model.add(value)
  end

  def do_reset(context, params)
    @model = SumTotal.new
  end

  def do_undo(context, params)
    @model.undo
  end
end

class BaseDiv < Div::Div
  set_erb('base.erb')

  def initialize(session)
    super(session)
    @sum = SumDiv.new(session)
  end
end

class YourTofuSession < Div::TofuSession
  def initialize(bartender, hint=nil)
    super(bartender, hint)
    @base = BaseDiv.new(self)
  end

  def do_GET(context)
    update_div(context)
    context.res_header('content-type', 'text/html; charset=euc-jp')
    context.res_body(@base.to_html(context))
  end
end

	    

Příklad 51.12. base.erb

<html>
  <head>
    <title>First App</title>
  </head>
  <body>
    <h1>First app</h1>
    <p><%=h Time.now%></p>
    <p><%= @sum.to_div(context) %></p>
  </body>
</html>
	    

Příklad 51.13. sum.erb

<%=form('add', context)%>
<table>
<% @model.history.each do |v| %>
<tr><td>&nbsp</td><td align='right'><%=h v%></td><td>&nbsp</td></tr>
<% end %>
<tr><th>total</th><th align='right'><%=h @model.amount%></th><td>&nbsp</td></tr>
<tr>
<th align='right'>add</th>
<th><input size="10" type="text" name="value" value="" /></th>
<th><input type="submit" name="Add" value="Add"/></th>
</tr>
<tr><td align="right" colspan="3"><%=a('undo', {}, context)%>undo</a></td></tr>
<tr><td align="right" colspan="3"><%=a('reset', {}, context)%>reset</a></td></tr>
</table>
</form>

	    

Licence Creative Commons
Tento dokument Ruby, jehož autorem je Radek Hnilica, podléhá licenci Creative Commons Uveďte autora-Nevyužívejte dílo komerčně-Zachovejte licenci 3.0 Česká republika .