Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
140 lines (100 sloc) 3.49 KB

Customize Shoes Class

You know,... creating Shoes app is writing Ruby code.

Hence, we can customize Shoes Class with Ruby overwriting and overloading feature.

This is no wonder, but I just noticed. :-P

# sample53.rb
class Shoes::Image
  def small
    self.style :width => self.width / 2, :height => self.height / 2
  end
  
  def big
    self.style :width => self.width * 2, :height => self.height * 2
  end
end

PATH = '../images/yar.png'

Shoes.app :width => 250, :height => 150 do
  w, h = imagesize(PATH)
  img = image PATH, :width => w, :height => h, :name => PATH.split('/').last
  msg = para 'ready', :left => w, :top => h
  every 3 do
    img.style[:width] > w ? img.small : img.big
    msg.text = "#{img.style[:name]} width is : #{img.style[:width]}" + "\n" +
               "#{img.style[:name]} height is : #{img.style[:height]}"
  end
end

sample53.png

sample53.png

Policeman

The sample53.rb didn't work with Shoes-0.r1263 and got this error:

sample53-1.png

sample53-1.png

I wrote sample53-1.rb and sample53-2.rb to confirm the behavior and ran them with Policeman and Raisins.

# sample53-1.rb
class Shoes::Image; end
Shoes.app :width => 200, :height => 150, :title => 'sample53-1.rb' do
  stack do
    img = image '../images/yar.png'
    para(img.class == Shoes::Image)
    para "Shoes-0.r#{REVISION}"
  end
end
  

# sample53-2.rb
#class Shoes::Image; end
Shoes.app :width => 200, :height => 150, :title => 'sample53-2.rb' do
  stack do
    img = image '../images/yar.png'
    para(img.class == Shoes::Image)
    para "Shoes-0.r#{REVISION}"
  end
end

sample53-2.png

sample53-2.png

Umm... I'm not sure Policeman changed the spec or .... bug.

Another way

Not customize Shoes class, though...

# sample53-3.rb
module ExImage
  def small
    self.style :width => self.width / 2, :height => self.height / 2
  end
  
  def big
    self.style :width => self.width * 2, :height => self.height * 2
  end
end

PATH = '../images/yar.png'

Shoes.app :width => 250, :height => 150 do
  w, h = imagesize(PATH)
  img = image PATH, :width => w, :height => h, :name => PATH.split('/').last
  img.extend ExImage
  msg = para 'ready', :left => w, :top => h
  every 3 do
    img.style[:width] > w ? img.small : img.big
    msg.text = "#{img.style[:name]} width is : #{img.style[:width]}" + "\n" +
               "#{img.style[:name]} height is : #{img.style[:height]}"
  end
end

This code works well both with Policeman and Raisins.

Solution

It seems good to use class_eval instead of class like this:

# sample53-4.rb
Shoes::Image.class_eval do
  def small
    self.style :width => self.width / 2, :height => self.height / 2
  end
  
  def big
    self.style :width => self.width * 2, :height => self.height * 2
  end
end

PATH = '../images/yar.png'

Shoes.app :width => 250, :height => 150 do
  w, h = imagesize(PATH)
  img = image PATH, :width => w, :height => h, :name => PATH.split('/').last
  msg = para 'ready', :left => w, :top => h
  every 3 do
    img.style[:width] > w ? img.small : img.big
    msg.text = "#{img.style[:name]} width is : #{img.style[:width]}" + "\n" +
               "#{img.style[:name]} height is : #{img.style[:height]}"
  end
end