Class: Area

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Validation
Defined in:
lib/generators/rcap/models/templates/models/area.rb

Overview

An Area object is valid if

  • it has an area description

  • all Circle objects contained in circles are valid

  • all Geocode objects contained in geocodes are valid

  • all Polygon objects contained in polygons are valid

  • altitude has a value if ceiling is set

Constant Summary collapse

XML_ELEMENT_NAME =

:nodoc:

'area'
AREA_DESC_ELEMENT_NAME =

:nodoc:

'areaDesc'
ALTITUDE_ELEMENT_NAME =

:nodoc:

'altitude'
CEILING_ELEMENT_NAME =

:nodoc:

'ceiling'
XPATH =

:nodoc:

"cap:#{ XML_ELEMENT_NAME }"
AREA_DESC_XPATH =

:nodoc:

"cap:#{ AREA_DESC_ELEMENT_NAME }"
ALTITUDE_XPATH =

:nodoc:

"cap:#{ ALTITUDE_ELEMENT_NAME }"
CEILING_XPATH =

:nodoc:

"cap:#{ CEILING_ELEMENT_NAME }"
AREA_DESC_YAML =

:nodoc:

'Area Description'
ALTITUDE_YAML =

:nodoc:

'Altitude'
CEILING_YAML =

:nodoc:

'Ceiling'
CIRCLES_YAML =

:nodoc:

'Circles'
GEOCODES_YAML =

:nodoc:

'Geocodes'
POLYGONS_YAML =

:nodoc:

'Polygons'
AREA_DESC_KEY =

:nodoc:

'area_desc'
ALTITUDE_KEY =

:nodoc:

'altitude'
CEILING_KEY =

:nodoc:

'ceiling'
CIRCLES_KEY =

:nodoc:

'circles'
GEOCODES_KEY =

:nodoc:

'geocodes'
POLYGONS_KEY =

:nodoc:

'polygons'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Area

Returns a new instance of Area.



39
40
41
42
43
44
45
46
# File 'lib/generators/rcap/models/templates/models/area.rb', line 39

def initialize( attributes = {})
  @area_desc = attributes[ :area_desc ]
  @altitude  = attributes[ :altitude ]
  @ceiling   = attributes[ :ceiling ]
  @circles   = Array( attributes[ :circles ])
  @geocodes  = Array( attributes[ :geocodes ])
  @polygons  = Array( attributes[ :polygons ])
end

Instance Attribute Details

#altitudeObject

Expressed in feet above sea level



15
16
17
# File 'lib/generators/rcap/models/templates/models/area.rb', line 15

def altitude
  @altitude
end

#area_descObject

Area Description - Textual description of area.



13
14
15
# File 'lib/generators/rcap/models/templates/models/area.rb', line 13

def area_desc
  @area_desc
end

#ceilingObject

Expressed in feet above sea level.



17
18
19
# File 'lib/generators/rcap/models/templates/models/area.rb', line 17

def ceiling
  @ceiling
end

#circlesObject (readonly)

Collection of Circle objects



19
20
21
# File 'lib/generators/rcap/models/templates/models/area.rb', line 19

def circles
  @circles
end

#geocodesObject (readonly)

Collection of Geocode objects



21
22
23
# File 'lib/generators/rcap/models/templates/models/area.rb', line 21

def geocodes
  @geocodes
end

#polygonsObject (readonly)

Collection of Polygon objects



23
24
25
# File 'lib/generators/rcap/models/templates/models/area.rb', line 23

def polygons
  @polygons
end

Class Method Details

.from_h(area_hash) ⇒ Object

:nodoc:



147
148
149
150
151
152
153
154
155
# File 'lib/generators/rcap/models/templates/models/area.rb', line 147

def self.from_h( area_hash ) # :nodoc:
  self.new(
   :area_desc => area_hash[ AREA_DESC_KEY ],
   :altitude  => area_hash[ ALTITUDE_KEY ],
   :ceiling   => area_hash[ CEILING_KEY ],
   :circles   => area_hash[ CIRCLES_KEY ].map{ |circle_hash| RCAP::Circle.from_h( circle_hash )},
   :geocodes  => area_hash[ GEOCODES_KEY ].map{ |geocode_hash| RCAP::Geocode.from_h( geocode_hash )},
   :polygons  => area_hash[ POLYGONS_KEY ].map{ |polygon_hash| RCAP::Polygon.from_h( polygon_hash )})
end

.from_xml_element(area_xml_element) ⇒ Object

:nodoc:



90
91
92
93
94
95
96
97
# File 'lib/generators/rcap/models/templates/models/area.rb', line 90

def self.from_xml_element( area_xml_element ) # :nodoc:
  RCAP::Area.new( :area_desc => RCAP.xpath_text( area_xml_element, AREA_DESC_XPATH ),
								:altitude  => (( alt = RCAP.xpath_text( area_xml_element, ALTITUDE_XPATH )) ? alt.to_f : nil ),
								:ceiling   => (( ceil = RCAP.xpath_text( area_xml_element, CEILING_XPATH )) ? ceil.to_f : nil ),
								:circles   => RCAP.xpath_match( area_xml_element, RCAP::Circle::XPATH ).map{ |circle_element| RCAP::Circle.from_xml_element( circle_element )},
								:geocodes  => RCAP.xpath_match( area_xml_element, RCAP::Geocode::XPATH ).map{ |geocode_element| RCAP::Geocode.from_xml_element( geocode_element )},
								:polygons  => RCAP.xpath_match( area_xml_element, RCAP::Polygon::XPATH ).map{ |polygon_element| RCAP::Polygon.from_xml_element( polygon_element )})
end

Instance Method Details

#==(other) ⇒ Object

Implements an equality operator for the Area object. Two Area objects are equal if all their attributes are equal.



68
69
70
71
# File 'lib/generators/rcap/models/templates/models/area.rb', line 68

def ==( other )
	comparison_attributes = lambda{ |area| [ area.area_desc, area.altitude, area.ceiling, area.circles, area.geocodes, area.polygons ]}
	comparison_attributes.call( self ) == comparison_attributes.call( other )
end

#from_yaml_data(area_yaml_data) ⇒ Object

:nodoc:



120
121
122
123
124
125
126
127
128
129
# File 'lib/generators/rcap/models/templates/models/area.rb', line 120

def from_yaml_data( area_yaml_data )  # :nodoc:
  Area.new(
    :area_desc => area_yaml_data[ AREA_DESC_YAML ],
    :altitude  => area_yaml_data[ ALTITUDE_YAML ],
    :ceiling   => area_yaml_data[ CEILING_YAML ],
    :circles   => Array( area_yaml_data[ CIRCLES_YAML ]).map{ |circle_yaml_data| RCAP::Circle.from_yaml_data( circle_yaml_data )},
    :geocodes  => Array( area_yaml_data[ GEOCODES_YAML ]).map{ |name, value| RCAP::Geocode.new( :name => name, :value => value )},
    :polygons  => Array( area_yaml_data[ POLYGONS_YAML ]).map{ |polyon_yaml_data| RCAP::Polygon.from_yaml_data( polyon_yaml_data )}
  )
end

#inspectObject

:nodoc:



73
74
75
76
77
78
79
80
81
82
# File 'lib/generators/rcap/models/templates/models/area.rb', line 73

def inspect # :nodoc:
   area_inspect =  <<EOF
Area Description: #{ self.area_desc }
Polygons:
#{ self.polygons.map{ |polygon| "  " + polygon.inspect }.join("\n" )}
Circles:          #{ self.circles.inspect }
Geocodes:         #{ self.geocodes.inspect }
EOF
   RCAP.format_lines_for_inspect( 'AREA', area_inspect )
end

#to_hObject

:nodoc:



138
139
140
141
142
143
144
145
# File 'lib/generators/rcap/models/templates/models/area.rb', line 138

def to_h # :nodoc:
  { AREA_DESC_KEY => self.area_desc,
    ALTITUDE_KEY  => self.altitude,
    CEILING_KEY   => self.ceiling,
    CIRCLES_KEY   => self.circles.map{ |circle| circle.to_h },
    GEOCODES_KEY  => self.geocodes.map{ |geocode| geocode.to_h },
    POLYGONS_KEY  => self.polygons.map{ |polygon| polygon.to_h }}
end

#to_sObject

Returns a string representation of the area of the form

area_desc


86
87
88
# File 'lib/generators/rcap/models/templates/models/area.rb', line 86

def to_s
  self.area_desc
end

#to_xmlObject

:nodoc:



63
64
65
# File 'lib/generators/rcap/models/templates/models/area.rb', line 63

def to_xml # :nodoc:
  self.to_xml_element.to_s
end

#to_xml_elementObject

:nodoc:



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/generators/rcap/models/templates/models/area.rb', line 48

def to_xml_element # :nodoc:
  xml_element = REXML::Element.new( XML_ELEMENT_NAME )
  xml_element.add_element( AREA_DESC_ELEMENT_NAME ).add_text( @area_desc.to_s )
  add_to_xml_element = lambda do |element, object|
    element.add_element( object.to_xml_element )
    element
  end
  @polygons.inject( xml_element, &add_to_xml_element )
  @circles.inject( xml_element, &add_to_xml_element )
  @geocodes.inject( xml_element, &add_to_xml_element )
  xml_element.add_element( ALTITUDE_ELEMENT_NAME ).add_text( @altitude.to_s ) unless self.altitude.blank?
  xml_element.add_element( CEILING_ELEMENT_NAME ).add_text( @ceiling.to_s )   unless self.altitude.blank?
  xml_element
end

#to_yaml(options = {}) ⇒ Object

:nodoc:



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/generators/rcap/models/templates/models/area.rb', line 106

def to_yaml( options = {} ) # :nodoc:
  circles_yaml = self.circles.map{ |circle| [ circle.lattitude, circle.longitude, circle.radius ]}
  def circles_yaml.to_yaml_style; :inline; end

  RCAP.attribute_values_to_hash(
    [ AREA_DESC_YAML,  self.area_desc ],
    [ ALTITUDE_YAML,   self.altitude ],
    [ CEILING_YAML,    self.ceiling ],
    [ CIRCLES_YAML,    circles_yaml ],
    [ GEOCODES_YAML,   self.geocodes.inject({}){|h,geocode| h.merge( geocode.name => geocode.value )}],
    [ POLYGONS_YAML,   self.polygons ]
  ).to_yaml( options )
end