Class: CityObject
- Inherits:
-
Object
- Object
- CityObject
- Defined in:
- lib/ttr/objects/city.rb
Overview
Copyright © 2011 Jesse Sielaff
Instance Attribute Summary collapse
-
#connected_city_objs ⇒ Object
readonly
Returns the value of attribute connected_city_objs.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#route_objs ⇒ Object
readonly
Returns the value of attribute route_objs.
Instance Method Summary collapse
-
#add_route_obj(route_obj) ⇒ Object
Adds the given Route to the City’s Routes.
-
#connected?(city_obj) ⇒ Boolean
Returns true if the two Cities are connected by a single direct Route, false otherwise.
-
#connected_using?(city_obj, route_objs) ⇒ Boolean
Returns true if the two Cities are connected using any combination of the given Routes.
-
#initialize(name) ⇒ CityObject
constructor
A new instance of CityObject.
-
#routes_to(city_obj) ⇒ Object
Returns an Array of the Routes directly connecting the two Cities.
Constructor Details
#initialize(name) ⇒ CityObject
Returns a new instance of CityObject.
7 8 9 10 11 |
# File 'lib/ttr/objects/city.rb', line 7 def initialize (name) @name = name @route_objs = [] @connected_city_objs = [] end |
Instance Attribute Details
#connected_city_objs ⇒ Object (readonly)
Returns the value of attribute connected_city_objs.
13 14 15 |
# File 'lib/ttr/objects/city.rb', line 13 def connected_city_objs @connected_city_objs end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
13 14 15 |
# File 'lib/ttr/objects/city.rb', line 13 def name @name end |
#route_objs ⇒ Object (readonly)
Returns the value of attribute route_objs.
13 14 15 |
# File 'lib/ttr/objects/city.rb', line 13 def route_objs @route_objs end |
Instance Method Details
#add_route_obj(route_obj) ⇒ Object
Adds the given Route to the City’s Routes.
17 18 19 20 |
# File 'lib/ttr/objects/city.rb', line 17 def add_route_obj (route_obj) @route_objs << route_obj @connected_city_objs |= (route_obj.city_objs - [self]) end |
#connected?(city_obj) ⇒ Boolean
Returns true if the two Cities are connected by a single direct Route, false otherwise.
25 26 27 |
# File 'lib/ttr/objects/city.rb', line 25 def connected? (city_obj) @connected_city_objs.include?(city_obj) end |
#connected_using?(city_obj, route_objs) ⇒ Boolean
Returns true if the two Cities are connected using any combination of the given Routes.
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/ttr/objects/city.rb', line 32 def connected_using? (city_obj, route_objs) valid_routes = route_objs & @route_objs return false if valid_routes.empty? valid_routes.any? do |obj| return true if obj.connects?(self, city_obj) obj.city_objs.any? do |c_obj| next if c_obj == self c_obj.connected_using?(city_obj, route_objs - valid_routes) end end end |
#routes_to(city_obj) ⇒ Object
Returns an Array of the Routes directly connecting the two Cities.
48 49 50 |
# File 'lib/ttr/objects/city.rb', line 48 def routes_to (city_obj) @route_objs.select {|obj| obj.connects?(self, city_obj) } end |