Class: Tourist

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Test::Unit::Assertions, Webrat::Matchers, Webrat::Methods, Webrat::SaveAndOpenPage
Defined in:
lib/tourist.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, tours, number, tourist_id) ⇒ Tourist

Returns a new instance of Tourist.



27
28
29
30
# File 'lib/tourist.rb', line 27

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

#hostObject (readonly)

Returns the value of attribute host.



25
26
27
# File 'lib/tourist.rb', line 25

def host
  @host
end

#numberObject (readonly)

Returns the value of attribute number.



25
26
27
# File 'lib/tourist.rb', line 25

def number
  @number
end

#tour_typeObject (readonly)

Returns the value of attribute tour_type.



25
26
27
# File 'lib/tourist.rb', line 25

def tour_type
  @tour_type
end

#tourist_idObject (readonly)

Returns the value of attribute tourist_id.



25
26
27
# File 'lib/tourist.rb', line 25

def tourist_id
  @tourist_id
end

#toursObject (readonly)

Returns list of tours this tourist knows about. (Meant to be run on a subclass instance; returns the list of tours available).



78
79
80
# File 'lib/tourist.rb', line 78

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



72
73
74
# File 'lib/tourist.rb', line 72

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

Returns:

  • (Boolean)


67
68
69
# File 'lib/tourist.rb', line 67

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.



51
52
53
54
55
56
57
58
59
60
# File 'lib/tourist.rb', line 51

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



62
63
64
# File 'lib/tourist.rb', line 62

def self.tours(tourist_name)
  Tourist.make_tourist(tourist_name).tours
end

Instance Method Details

#after_toursObject

after_tour runs once per tour, after all the tours have run



36
# File 'lib/tourist.rb', line 36

def after_tours; end

#before_toursObject

before_tour runs once per tour, before any tours get run



33
# File 'lib/tourist.rb', line 33

def before_tours; end

#run_tour(tour_name) ⇒ Object

Raises:



82
83
84
85
86
87
88
# File 'lib/tourist.rb', line 82

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

#setupObject



38
39
# File 'lib/tourist.rb', line 38

def setup
end

#teardownObject



41
42
# File 'lib/tourist.rb', line 41

def teardown
end

#wait(time) ⇒ Object



44
45
46
# File 'lib/tourist.rb', line 44

def wait(time)
  sleep time.to_i
end