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.



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

#hostObject (readonly)

Returns the value of attribute host.



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

def host
  @host
end

#numberObject (readonly)

Returns the value of attribute number.



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

def number
  @number
end

#tour_typeObject (readonly)

Returns the value of attribute tour_type.



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

def tour_type
  @tour_type
end

#tourist_idObject (readonly)

Returns the value of attribute tourist_id.



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

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).



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

Returns:

  • (Boolean)


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_toursObject

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



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

def after_tours; end

#before_toursObject

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

Raises:



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

#setupObject



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

def setup
end

#teardownObject



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