1 class Segment < ActiveRecord::Base
3 set_table_name 'current_segments'
5 validates_numericality_of :node_a
6 validates_numericality_of :node_b
8 has_many :old_segments, :foreign_key => :id
11 has_one :from_node, :class => 'Node', :foreign_key => 'node_a'
12 has_one :to_node, :class => 'Node', :foreign_key => 'node_b'
14 def self.from_xml(xml, create=false)
21 doc.find('//osm/segment').each do |pt|
23 segment.node_a = pt['from'].to_i
24 segment.node_b = pt['to'].to_i
27 segment.id = pt['id'].to_i
30 segment.visible = true
33 segment.timestamp = Time.now
36 segment.timestamp = Time.parse(pt['timestamp'])
42 pt.find('tag').each do |tag|
43 tags << [tag['k'],tag['v']]
46 tags = tags.collect { |k,v| "#{k}=#{v}" }.join(';')
47 tags = '' if tags.nil?
57 Segment.transaction do
59 old_segment = OldSegment.from_segment(self)
63 rescue Exception => ex
69 doc = XML::Document.new
70 doc.encoding = 'UTF-8'
71 root = XML::Node.new 'osm'
72 root['version'] = API_VERSION
73 root['generator'] = 'OpenStreetMap server'
80 el1 = XML::Node.new 'segment'
81 el1['id'] = self.id.to_s
82 el1['from'] = self.node_a.to_s
83 el1['to'] = self.node_b.to_s
84 Segment.split_tags(el1, self.tags)
85 el1['visible'] = self.visible.to_s
86 el1['timestamp'] = self.timestamp.xmlschema
90 def self.split_tags(el, tags)
91 tags.split(';').each do |tag|
92 parts = tag.split('=')
95 key = parts[0].strip unless parts[0].nil?
96 val = parts[1].strip unless parts[1].nil?
97 if key != '' && val != ''
98 el2 = XML::Node.new('tag')
107 return from_node and from_node.visible and to_node and to_node.visible