Skip to content

Instantly share code, notes, and snippets.

@melborne
Created November 10, 2008 08:12
Show Gist options
  • Save melborne/23446 to your computer and use it in GitHub Desktop.
Save melborne/23446 to your computer and use it in GitHub Desktop.
shoes app with druby & observer pattern
require "drb/drb"
DRb.start_service
$lists = DRbObject.new_with_uri('druby://192.168.1.3:12345')
Shoes.app :width => 250, :height => 300 do
flow do
@input = edit_line :width => 150 do |ed|
$lists[:client_line] = ed.text
end
button 'send' do
$lists[:history].unshift("client: " + @input.text)
@input.text = ''
end
end
stack :margin_left => 5 do
@my_line = para strong(""), :font => "Osaka"
@his_line = para strong(""), :font => "Osaka"
@history = edit_box :width => 200, :height => 200
end
every 0.2 do
@history.text = $lists[:history].join("\n")
@my_line.text = $lists[:client_line]
@his_line.text = $lists[:server_line]
end
start { @input.focus}
end
require "drb/drb"
$front = {}
DRb.start_service('druby://192.168.1.3:12345', $front)
Shoes.app :width => 250, :height => 300 do
$front[:history] = DRbObject.new([])
flow do
@input = edit_line :width => 150 do |ed|
$front[:server_line] = ed.text
end
button 'send' do
$front[:history].unshift("server: " + @input.text)
@input.text = ''
end
end
stack :margin_left => 5 do
@my_line = para strong(""), :font => "Osaka"
@his_line = para strong(""), :font => "Osaka"
@history = edit_box :width => 200, :height => 200
end
every 0.2 do
@history.text = $front[:history].join("\n")
@my_line.text = $front[:server_line]
@his_line.text = $front[:client_line]
end
start { @input.focus}
end
require "drb/drb"
module DRuby
include DRbUndumped
URI = 'druby://192.168.1.3:9000'
def self.uv=(obj)
DRb.start_service(URI, obj)
end
def self.uv
DRb.start_service
DRbObject.new_with_uri(URI)
end
end
module Observable
def add_observer(observer)
@observers ||= []
@observers << observer
end
def notify_observers(*objs)
return nil unless defined? @observers
@observers.each do |obs|
obs.update(*objs)
end
end
end
require "druby"
class Tick
include Observable
def start
now = Time.now
notify_observers(now)
now
end
end
$tick = Tick.new
DRuby.uv = $tick
Shoes.app :width => 200, :height => 60, :resizable => false do
time = title "helo"
animate 60 do |f|
t = $tick.start
time.replace t.strftime("%H:%M:%S")
end
end
require "druby"
class Timer
def update(now)
$now = now
end
end
$timer = Timer.new
$tick = DRuby.uv
$tick.add_observer($timer)
Shoes.app :width => 160, :height => 100 do
flow do
@line = edit_line :width => 100
button "OK" do
@h,@m,@s = @line.text.split(':').map { |t| t.to_i }
@set_time.replace [@h, @m, @s].join(":")
end
end
stack do
@set_time = para ''
end
animate 60 do |f|
if $now.hour == @h && $now.min == @m && $now.sec == @s
alert "It's time!"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment