Class: Tourist
- Inherits:
-
Object
- Object
- Tourist
- Extended by:
- Forwardable
- Includes:
- Test::Unit::Assertions, Webrat::Matchers, Webrat::Methods, Webrat::SaveAndOpenPage
- Defined in:
- lib/tourist.rb
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#number ⇒ Object
readonly
Returns the value of attribute number.
-
#tour_type ⇒ Object
readonly
Returns the value of attribute tour_type.
-
#tourist_id ⇒ Object
readonly
Returns the value of attribute tourist_id.
-
#tours ⇒ Object
readonly
Returns list of tours this tourist knows about.
Class Method Summary collapse
-
.make_tourist(tourist_name, host = "http://localhost:3000", tours = [], number = 1, tourist_id = nil) ⇒ Object
Factory method, creates the named child class instance.
-
.tourist?(tourist_name) ⇒ Boolean
Returns true if the given tourist name can be found in the tours folder, and defines a similarly-named subclass of Tourist.
-
.tourists(filter = []) ⇒ Object
Lists tourists in tours folder.
- .tours(tourist_name) ⇒ Object
Instance Method Summary collapse
-
#after_tours ⇒ Object
after_tour runs once per tour, after all the tours have run.
-
#before_tours ⇒ Object
before_tour runs once per tour, before any tours get run.
-
#initialize(host, tours, number, tourist_id) ⇒ Tourist
constructor
A new instance of Tourist.
- #run_tour(tour_name) ⇒ Object
- #setup ⇒ Object
- #teardown ⇒ Object
- #wait(time) ⇒ Object
Constructor Details
#initialize(host, tours, number, tourist_id) ⇒ Tourist
Returns a new instance of Tourist.
28 29 30 31 |
# File 'lib/tourist.rb', line 28 def initialize(host, tours, number, tourist_id) @host, @tours, @number, @tourist_id = host, tours, number, tourist_id @tour_type = self.send(:class).to_s end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
26 27 28 |
# File 'lib/tourist.rb', line 26 def host @host end |
#number ⇒ Object (readonly)
Returns the value of attribute number.
26 27 28 |
# File 'lib/tourist.rb', line 26 def number @number end |
#tour_type ⇒ Object (readonly)
Returns the value of attribute tour_type.
26 27 28 |
# File 'lib/tourist.rb', line 26 def tour_type @tour_type end |
#tourist_id ⇒ Object (readonly)
Returns the value of attribute tourist_id.
26 27 28 |
# File 'lib/tourist.rb', line 26 def tourist_id @tourist_id end |
#tours ⇒ Object (readonly)
Returns list of tours this tourist knows about. (Meant to be run on a subclass instance; returns the list of tours available).
79 80 81 |
# File 'lib/tourist.rb', line 79 def tours @tours end |
Class Method Details
.make_tourist(tourist_name, host = "http://localhost:3000", tours = [], number = 1, tourist_id = nil) ⇒ Object
Factory method, creates the named child class instance
73 74 75 |
# File 'lib/tourist.rb', line 73 def self.make_tourist(tourist_name,host="http://localhost:3000",tours=[],number=1,tourist_id=nil) tourist_name.classify.constantize.new(host,tours,number,tourist_id) end |
.tourist?(tourist_name) ⇒ Boolean
Returns true if the given tourist name can be found in the tours folder, and defines a similarly-named subclass of Tourist
68 69 70 |
# File 'lib/tourist.rb', line 68 def self.tourist?(tourist_name) Object.const_defined?(tourist_name.classify) && tourist_name.classify.constantize.ancestors.include?(Tourist) end |
.tourists(filter = []) ⇒ Object
Lists tourists in tours folder. If a string is given, filters the list by that string. If an array of filter strings is given, returns items that match ANY filter string in the array.
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/tourist.rb', line 52 def self.tourists(filter=[]) filter = [filter].flatten # All files in tours folder, stripped to basename, that match any item in filter # I do loves me a long chain. This returns an array containing # 1. All *.rb files in tour folder (recursive) # 2. Each filename stripped to its basename # 3. If you passed in any filters, these basenames are rejected unless they match at least one filter # 4. The filenames remaining are then checked to see if they define a class of the same name that inherits from Tourist Dir[File.join('.', 'tours', '**', '*.rb')].map {|fn| File.basename(fn, ".rb")}.select {|fn| filter.size.zero? || filter.any?{|f| fn =~ /#{f}/}}.select {|tour| Tourist.tourist? tour } end |
.tours(tourist_name) ⇒ Object
63 64 65 |
# File 'lib/tourist.rb', line 63 def self.tours(tourist_name) Tourist.make_tourist(tourist_name).tours end |
Instance Method Details
#after_tours ⇒ Object
after_tour runs once per tour, after all the tours have run
37 |
# File 'lib/tourist.rb', line 37 def after_tours; end |
#before_tours ⇒ Object
before_tour runs once per tour, before any tours get run
34 |
# File 'lib/tourist.rb', line 34 def before_tours; end |
#run_tour(tour_name) ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/tourist.rb', line 83 def run_tour(tour_name) @current_tour = "tour_#{tour_name}" raise TourBusException.new("run_tour couldn't run tour '#{tour_name}' because this tourist did not respond to :#{@current_tour}") unless respond_to? @current_tour setup send @current_tour teardown end |
#setup ⇒ Object
39 40 |
# File 'lib/tourist.rb', line 39 def setup end |
#teardown ⇒ Object
42 43 |
# File 'lib/tourist.rb', line 42 def teardown end |
#wait(time) ⇒ Object
45 46 47 |
# File 'lib/tourist.rb', line 45 def wait(time) sleep time.to_i end |