Class: ActiveRoad::Path

Inherits:
Object
  • Object
show all
Defined in:
app/models/active_road/path.rb

Overview

A path is a link between differents objects :

- Departure
- Arrival

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Path

Returns a new instance of Path.



9
10
11
12
13
# File 'app/models/active_road/path.rb', line 9

def initialize(attributes = {})
  attributes.each do |k, v|
    send("#{k}=", v)
  end
end

Instance Attribute Details

#arrivalObject

Returns the value of attribute arrival.



6
7
8
# File 'app/models/active_road/path.rb', line 6

def arrival
  @arrival
end

#departureObject

Returns the value of attribute departure.



6
7
8
# File 'app/models/active_road/path.rb', line 6

def departure
  @departure
end

#physical_roadObject Also known as: road

Returns the value of attribute physical_road.



6
7
8
# File 'app/models/active_road/path.rb', line 6

def physical_road
  @physical_road
end

Class Method Details

.all(departure, arrivals, physical_road) ⇒ Object



40
41
42
43
44
# File 'app/models/active_road/path.rb', line 40

def self.all(departure, arrivals, physical_road)
  Array(arrivals).collect do |arrival|
    new :departure => departure, :arrival => arrival, :physical_road => physical_road
  end
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



74
75
76
77
78
# File 'app/models/active_road/path.rb', line 74

def ==(other)
  [:departure, :arrival, :physical_road].all? do |attribute|
    other.respond_to?(attribute) and send(attribute) == other.send(attribute)
  end
end

#geometry_with_cacheObject Also known as: geometry



64
65
66
# File 'app/models/active_road/path.rb', line 64

def geometry_with_cache
  @geometry ||= geometry_without_cache
end

#geometry_without_cacheObject



56
57
58
59
60
61
62
# File 'app/models/active_road/path.rb', line 56

def geometry_without_cache
  sorted_locations_on_road = locations_on_road.sort
  reverse = (sorted_locations_on_road != locations_on_road)
  geometry = road.line_substring *sorted_locations_on_road
  geometry = geometry.reverse if reverse
  geometry
end

#hashObject



81
82
83
# File 'app/models/active_road/path.rb', line 81

def hash
  [departure, arrival, physical_road].hash
end

#length_in_meterObject



31
32
33
# File 'app/models/active_road/path.rb', line 31

def length_in_meter
  length_on_road * road.length_in_meter
end

#length_on_roadObject



35
36
37
38
# File 'app/models/active_road/path.rb', line 35

def length_on_road
  begin_on_road, end_on_road = locations_on_road.sort
  end_on_road - begin_on_road
end

#locations_on_roadObject



19
20
21
22
23
24
25
26
27
28
29
# File 'app/models/active_road/path.rb', line 19

def locations_on_road
  [departure, arrival].collect do |endpoint|
    location =
      if GeoRuby::SimpleFeatures::Point === endpoint
        road.locate_point endpoint
      else
        endpoint.location_on_road road
      end
    location = [0, [location, 1].min].max
  end
end

#nameObject



15
16
17
# File 'app/models/active_road/path.rb', line 15

def name
  "Path : #{departure} -> #{arrival}"
end

#paths(tags = {}) ⇒ Object



48
49
50
# File 'app/models/active_road/path.rb', line 48

def paths(tags = {})
  arrival.paths(tags) - [reverse]
end

#reverseObject



52
53
54
# File 'app/models/active_road/path.rb', line 52

def reverse
  self.class.new :departure => arrival, :arrival => departure, :physical_road => physical_road
end

#to_sObject



70
71
72
# File 'app/models/active_road/path.rb', line 70

def to_s
  name
end