Skip to content

Commit

Permalink
Implemented Shoes.url. Try out sample35.rb.
Browse files Browse the repository at this point in the history
  • Loading branch information
ashbb committed Nov 14, 2012
1 parent 7a66d77 commit dbea525
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/shoes.rb
Expand Up @@ -47,3 +47,4 @@ def logger
require 'shoes/logger'
require 'shoes/text_block'
require 'shoes/radio'
require 'shoes/url'
3 changes: 2 additions & 1 deletion lib/shoes/app.rb
Expand Up @@ -28,7 +28,7 @@ def self.app(opts={}, &blk)
class App
include Shoes::ElementMethods

attr_reader :gui, :shell, :top_slot, :contents, :unslotted_elements
attr_reader :gui, :shell, :top_slot, :contents, :unslotted_elements, :location
attr_reader :app, :mouse_motion
attr_accessor :elements, :current_slot
attr_accessor :opts, :blk
Expand All @@ -54,6 +54,7 @@ def initialize(opts={}, &blk)

@gui = Shoes.configuration.backend::App.new @app

blk = $urls[/^#{'/'}$/] unless blk
@top_slot = Flow.new self, {app: @app, left: 0, top: 0, width: @width, height: @height}, &blk

Shoes.register self
Expand Down
4 changes: 4 additions & 0 deletions lib/shoes/element_methods.rb
Expand Up @@ -373,5 +373,9 @@ def clear
contents.each &:clear
@contents.clear
end

def visit url
$urls.each{|k, v| clear{@location = url; v.call self, $1} if k =~ url}
end
end
end
17 changes: 17 additions & 0 deletions lib/shoes/url.rb
@@ -0,0 +1,17 @@
module Shoes
$urls = {}
def self.included klass
def klass.url page, m
klass = self
page = /^#{page}$/
$urls[page] = proc do |s, arg|
klass.class_eval do
define_method :method_missing do |m, *args, &blk|
s.send m, *args, &blk
end
end
arg ? klass.new.send(m, arg) : klass.new.send(m)
end
end
end
end
3 changes: 2 additions & 1 deletion samples/README
Expand Up @@ -27,4 +27,5 @@ sample18.rb
sample19.rb
sample20.rb
sample21.rb
sample28.rb
sample28.rb
sample35.rb
Binary file added samples/cy.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added samples/loogink.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions samples/sample35.rb
@@ -0,0 +1,37 @@
class PhotoFrame
SPACE = " "
include Shoes
url '/', :index
url '/loogink', :loogink
url '/cy', :cy

def index
eval(['loogink', 'cy'][rand 2])
end

def loogink
background tomato
inscription 'Shoes 4'
flow width: 70 # space
image File.join(Shoes::DIR, '../samples/loogink.png')
para SPACE
para SPACE, fg(strong('She is Loogink.'), white),
'->', link(strong('Cy')){visit '/cy'}
p location
end

def cy
background paleturquoise
inscription 'Shoes 4'
flow width: 70 # space
image File.join(Shoes::DIR, '../samples/cy.png')
para SPACE
para SPACE, fg(strong('He is Cy.'), gray), ' ->',
link(strong('loogink')){visit '/loogink'}
p location
end
end

Shoes.app width: 200, height: 120, title: 'Photo Frame'

# TODO: Remove SPACE and use margin style option.
18 changes: 18 additions & 0 deletions spec/shoes/url_spec.rb
@@ -0,0 +1,18 @@
require 'shoes/spec_helper'

describe 'Shoes.url' do
subject do
class Hello
include Shoes
url '/', :index
def index; end
end
Shoes.app
end

it "should have $urls" do
$urls.should be_empty
subject
$urls.should_not be_empty
end
end

1 comment on commit dbea525

@wasnotrice
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome. Works great on my OS X.

Please sign in to comment.