Class: Rosemary::Relation

Inherits:
Element
  • Object
show all
Defined in:
lib/rosemary/relation.rb

Overview

OpenStreetMap Relation.

To create a new Rosemary::Relation object:

relation = Rosemary::Relation.new()

To get a relation from the API:

relation = Rosemary::Relation.find(17)

Instance Attribute Summary collapse

Attributes inherited from Element

#changeset, #id, #tags, #timestamp, #uid, #user, #version

Instance Method Summary collapse

Methods inherited from Element

#[], #[]=, #add_tags, #attribute_list, #attributes, from_api, from_xml, #get_history_from_api, #get_relations_from_api, #initialize_copy, #is_tagged?, #method_missing, #shape, #update_attributes

Constructor Details

#initialize(attrs) ⇒ Relation

Create new Relation object.

If id is nil a new unique negative ID will be allocated.



17
18
19
20
21
# File 'lib/rosemary/relation.rb', line 17

def initialize(attrs)
  attrs.stringify_keys!
  @members = extract_member(attrs['member'])
  super(attrs)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Rosemary::Element

Instance Attribute Details

#membersObject (readonly)

Array of Member objects



12
13
14
# File 'lib/rosemary/relation.rb', line 12

def members
  @members
end

Instance Method Details

#<=>(another_relation) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rosemary/relation.rb', line 42

def <=>(another_relation)
  parent_compare = super(another_relation)
  # 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_relation.send(:tags)

  members_compare = self.send(:members).sort <=> another_relation.send(:members).sort
  # don't bother to compare more stuff if nodes comparison failed
  return members_compare unless members_compare == 0

  0
end

#to_xml(options = {}) ⇒ Object

Return XML for this relation. This method uses the Builder library. The only parameter ist the builder object.



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rosemary/relation.rb', line 29

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.relation(attributes) do
      members.each do |member|
        member.to_xml(:builder => xml, :skip_instruct => true)
      end
      tags.to_xml(:builder => xml, :skip_instruct => true)
    end
  end
end

#typeObject



23
24
25
# File 'lib/rosemary/relation.rb', line 23

def type
  'relation'
end