- has_many :containing_relations, :class_name => "Relation", :through => :containing_relation_members, :source => :relation, :extend => ObjectFinder
-
- validates_presence_of :id, :on => :update
- validates_presence_of :changeset_id,:version, :timestamp
- validates_uniqueness_of :id
- validates_inclusion_of :visible, :in => [ true, false ]
- validates_numericality_of :changeset_id, :version, :integer_only => true
- validates_numericality_of :id, :on => :update, :integer_only => true
- validates_associated :changeset
-
- def self.from_xml(xml, create=false)
- begin
- p = XML::Parser.new
- p.string = xml
- doc = p.parse
-
- doc.find('//osm/way').each do |pt|
- return Way.from_xml_node(pt, create)
- end
- rescue
- return nil
+ has_many :containing_relations, :class_name => "Relation", :through => :containing_relation_members, :source => :relation
+
+ validates :id, :uniqueness => true, :presence => { :on => :update },
+ :numericality => { :on => :update, :only_integer => true }
+ validates :version, :presence => true,
+ :numericality => { :only_integer => true }
+ validates :changeset_id, :presence => true,
+ :numericality => { :only_integer => true }
+ validates :timestamp, :presence => true
+ validates :changeset, :associated => true
+ validates :visible, :inclusion => [true, false]
+
+ scope :visible, -> { where(:visible => true) }
+ scope :invisible, -> { where(:visible => false) }
+
+ # Read in xml as text and return it's Way object representation
+ def self.from_xml(xml, create = false)
+ p = XML::Parser.string(xml, :options => XML::Parser::Options::NOERROR)
+ doc = p.parse
+ pt = doc.find_first("//osm/way")
+
+ if pt
+ Way.from_xml_node(pt, create)
+ else
+ raise OSM::APIBadXMLError.new("node", xml, "XML doesn't contain an osm/way element.")