ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> ruby-talk
ruby-talk
Re: [QUIZ] Price Ranges (#164)
by Steven Hahn other posts by this author
Jun 3 2008 11:48AM messages near this date
Re: [QUIZ] Price Ranges (#164) | Re: [QUIZ] Price Ranges (#164)
I hadn't planned on making another submission, but after tinkering with it a bit I realized 
a few things:

1. A simple GUI would be nice.  I used FXRuby.
2. The SAX Listener was barfing when the number of vendors was six or more.  Funny thing was
 that it only had a problem when the XML was formatted.  If I removed the whitespace it did 
just fine.  In any case, I switched to using a StreamListener instead.
3. There was an inconsistency in the logic in my original in_range? method.

Anyway, here is the Ruby code followed by sample XML:
--------------------------------------------------------------
#!/usr/local/bin/ruby -w
require 'rexml/document'
require 'rexml/streamlistener'
require 'fox16'
include Fox

class VendorListener 
  include REXML::StreamListener
  def initialize(low_price, high_price, vendor_search_window)
    begin
      @low_price = Float(low_price)
    rescue
      @low_price = 0
    end
    
    begin
      @high_price = Float(high_price)
    rescue
      #if someone can spend more than this then
      #she or he can afford a better program
      @high_price = 10**15
    end
    
    @vdr_srch_wdw = vendor_search_window
  end
  
  def tag_start(name, attrs)
    if name == 'Vendor'
      @vendor_name = attrs['name']
    end
  end
  
  def tag_end(name)
    if name == 'Vendor' and 
      if in_range?
        @vdr_srch_wdw.add_vendor(@vendor_name)
      end
    elsif name == 'LowPrice'
      @vendor_low_price = Float(@data)
    elsif name == 'HighPrice'
      @vendor_high_price = Float(@data)
    end
  end

  def text(text)
    @data = text
  end

  def in_range?
    @low_price <= @high_price and
      (@low_price > = @vendor_low_price or @high_price >= @vendor_low_price) and
      (@low_price <= @vendor_high_price or @high_price <= @vendor_high_price)
  end
end

class VendorSearchWindow < FXMainWindow
  def initialize(app)
    # Invoke base class initialize first
    super(app, "Ruby Quiz \#164: Vendor Search", nil, nil, DECOR_TITLE | DECOR_CLOSE)

    #Add text field frame at the top
    textfields = FXHorizontalFrame.new(self, LAYOUT_SIDE_TOP|LAYOUT_CENTER_X)
    FXLabel.new(textfields, "Enter a range:", nil, JUSTIFY_LEFT)
    FXLabel.new(textfields, "low:", nil, JUSTIFY_RIGHT)
    @low_field = FXTextField.new(textfields, 10, nil, 0, JUSTIFY_RIGHT|FRAME_SUNKEN|FRAME_TH
ICK|LAYOUT_SIDE_TOP)
    FXLabel.new(textfields, "high:", nil, JUSTIFY_RIGHT)
    @high_field = FXTextField.new(textfields, 10, nil, 0, JUSTIFY_RIGHT|FRAME_SUNKEN|FRAME_T
HICK|LAYOUT_SIDE_TOP)
    
    #add button frame at the bottom
    buttons = FXHorizontalFrame.new(self, LAYOUT_SIDE_BOTTOM|LAYOUT_CENTER_X|PACK_UNIFORM_WI
DTH)
    show_button = FXButton.new(buttons, "Show Vendors")
    show_button.connect(SEL_COMMAND, method(:on_show_vendors))
    exit_button = FXButton.new(buttons, "Exit")
    exit_button.connect(SEL_COMMAND, method(:on_exit))

    #Place the list in a sunken frame
    sunken_frame = FXHorizontalFrame.new(self,
            LAYOUT_FILL_X|LAYOUT_FILL_Y|FRAME_SUNKEN|FRAME_THICK, :padding =>  0)
    @vendor_list = FXList.new(sunken_frame, :opts =>  LIST_SINGLESELECT|LAYOUT_FILL_X|LAYOUT_
FILL_Y)
  end

  def on_exit(sender, sel, ptr)
    getApp().exit
  end
  
  def on_show_vendors(sender, sel, ptr)
    @vendor_list.clearItems(false)
    REXML::Document.parse_stream(File.new('vendors.xml' ),
      VendorListener.new(@low_field.text, @high_field.text, self))
  end
  
  def add_vendor(vndr_name)
    @vendor_list.appendItem(vndr_name)
  end
  
  def create
    super
    show(PLACEMENT_SCREEN)
  end
end

application = FXApp.new
VendorSearchWindow.new(application)
application.create
application.run
--------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?> 
<Vendors> 
    <Vendor name="Company A"> 
        <PriceRange> 
            <LowPrice> 1000</LowPrice>
            <HighPrice> 3000</HighPrice>
        </PriceRange> 
    </Vendor> 
    <Vendor name="Company B"> 
        <PriceRange> 
            <LowPrice> 6000</LowPrice>
            <HighPrice> 10000</HighPrice>
        </PriceRange> 
    </Vendor> 
    <Vendor name="Company C"> 
        <PriceRange> 
            <LowPrice> 500</LowPrice>
            <HighPrice> 2500</HighPrice>
        </PriceRange> 
    </Vendor> 
</Vendors> 



_________________________________________________________________
Now you can invite friends from Facebook and other groups to join you on Windows Live� Mes
senger. Add now.
https://www.invite2messenger.net/im/?source=TXT_EML_WLH_AddNow_Now
Thread:
Matthew Moss
Sharon and Dave
Matthew Rudy Jacobs
Sharon and Dave
Steven Hahn
Harry Kakueki
Toby O'Rourke
jgabrielygalan
Steven Hahn
Andrea Fazzi
S2
Matthew Moss
Matthew Moss
Robert Dober

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved