xmlblog (owner)

Revisions

gist: 160687 Download_button fork
public
Description:
Itinerary and Event classes
Public Clone URL: git://gist.github.com/160687.git
Embed All Files: show embed
event.rb #
1
2
3
4
class Event < ActiveRecord::Base
  belongs_to :itinerary
  validates_presence_of :code, :port, :event_date
end
itinerary.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Itinerary < ActiveRecord::Base
  has_many :events
 
  def duration
    # ... omitted for clarity
  end
 
  def visited_ports
    # ... omitted for clarity
  end
 
  def self.define(*args, &block)
    returning(new(*args)) do |itin|
      itin.instance_eval(&block)
    end
  end
 
  private
 
  def depart(port, time_spec)
    build_itinerary_event('D', port, time_spec)
  end
 
  def arrive(port, time_spec)
    build_itinerary_event('A', port, time_spec)
  end
 
  def build_itinerary_event(code, port, time_spec)
    events.build(:code => code, :port => port,
      :event_date => Chronic.parse(time_spec))
  end
end