Sha256: 6e00bd4b9034384ed065b15fc8d55a4fbb9f1f59af90a76085de21cc0a5ea1e5

Contents?: true

Size: 1.83 KB

Versions: 9

Compression:

Stored size: 1.83 KB

Contents

require 'builder'
module Rosemary
  # OpenStreetMap Node.
  #
  # To create a new Rosemary::Node object:
  #   node = Rosemary::Node.new(:id => "123", :lat => "52.2", :lon => "13.4", :changeset => "12", :user => "fred", :uid => "123", :visible => true, :timestamp => "2005-07-30T14:27:12+01:00")
  #
  # To get a node from the API:
  #   node = Rosemary::Node.find(17)
  #
  class Node < Element
    # Longitude in decimal degrees
    attr_accessor :lon

    # Latitude in decimal degrees
    attr_accessor :lat

    validates :lat, :presence => true, :numericality => {:greater_than_or_equal_to => -180, :less_than_or_equal_to => 180}
    validates :lon, :presence => true, :numericality => {:greater_than_or_equal_to => -90,  :less_than_or_equal_to => 90}

    # Create new Node object.
    #
    # If +id+ is +nil+ a new unique negative ID will be allocated.
    def initialize(attrs = {})
      attrs = attrs.dup.stringify_keys!
      @lon = attrs['lon'].to_f rescue nil
      @lat = attrs['lat'].to_f rescue nil
      super(attrs)
    end


    def type
      'Node'
    end

    # List of attributes for a Node
    def attribute_list
      [:id, :version, :uid, :user, :timestamp, :lon, :lat, :changeset]
    end

    def to_xml(options = {})
      xml = options[:builder] ||= Builder::XmlMarkup.new
      xml.instruct! unless options[:skip_instruct]
      xml.osm(:generator => "rosemary v#{Rosemary::VERSION}", :version => Rosemary::Api::API_VERSION) do
        xml.node(attributes) do
          tags.to_xml(:builder => xml, :skip_instruct => true)
        end
      end
    end

    def <=>(another_node)
      parent_compare = super(another_node)
      # don't bother to compare more stuff if parent comparison failed
      return parent_compare unless parent_compare == 0

      return -1 if self.send(:tags) != another_node.send(:tags)

      0
    end


  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rosemary-0.4.4 lib/rosemary/node.rb
rosemary-0.4.3 lib/rosemary/node.rb
rosemary-0.4.2 lib/rosemary/node.rb
rosemary-0.4.1 lib/rosemary/node.rb
rosemary-0.4.0 lib/rosemary/node.rb
rosemary-0.3.12 lib/rosemary/node.rb
rosemary-0.3.11 lib/rosemary/node.rb
rosemary-0.3.10 lib/rosemary/node.rb
rosemary-0.3.9 lib/rosemary/node.rb