Class: RCAP::Base::Area

Inherits:
Object
  • Object
show all
Includes:
Validation
Defined in:
lib/rcap/base/area.rb

Direct Known Subclasses

CAP_1_0::Area, CAP_1_1::Area, CAP_1_2::Area

Constant Summary

XML_ELEMENT_NAME =
'area'
AREA_DESC_ELEMENT_NAME =
'areaDesc'
ALTITUDE_ELEMENT_NAME =
'altitude'
CEILING_ELEMENT_NAME =
'ceiling'
XPATH =
"cap:#{ XML_ELEMENT_NAME }"
AREA_DESC_XPATH =
"cap:#{ AREA_DESC_ELEMENT_NAME }"
ALTITUDE_XPATH =
"cap:#{ ALTITUDE_ELEMENT_NAME }"
CEILING_XPATH =
"cap:#{ CEILING_ELEMENT_NAME }"
AREA_DESC_YAML =
'Area Description'
ALTITUDE_YAML =
'Altitude'
CEILING_YAML =
'Ceiling'
CIRCLES_YAML =
'Circles'
GEOCODES_YAML =
'Geocodes'
POLYGONS_YAML =
'Polygons'
AREA_DESC_KEY =
'area_desc'
ALTITUDE_KEY =
'altitude'
CEILING_KEY =
'ceiling'
CIRCLES_KEY =
'circles'
GEOCODES_KEY =
'geocodes'
POLYGONS_KEY =
'polygons'

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Area) initialize {|_self| ... }

A new instance of Area

Examples:

Area.new do |area|
  area.area_desc = 'Cape Town CVD'
end

Yields:

  • (_self)

Yield Parameters:



37
38
39
40
41
42
43
44
45
46
# File 'lib/rcap/base/area.rb', line 37

def initialize
  @area_desc = nil
  @altitude  = nil
  @ceiling   = nil

  @circles  = []
  @geocodes = []
  @polygons = []
  yield( self ) if block_given?
end

Instance Attribute Details

- (Numeric) altitude

Expressed in feet above sea level

Returns:

  • (Numeric)

    Expressed in feet above sea level



9
10
11
# File 'lib/rcap/base/area.rb', line 9

def altitude
  @altitude
end

- (String) area_desc

Textual description of area.

Returns:

  • (String)

    Textual description of area.



7
8
9
# File 'lib/rcap/base/area.rb', line 7

def area_desc
  @area_desc
end

- (Numeric) ceiling

Expressed in feet above sea level.

Returns:

  • (Numeric)

    Expressed in feet above sea level.



11
12
13
# File 'lib/rcap/base/area.rb', line 11

def ceiling
  @ceiling
end

- (Array<Circle>) circles (readonly)

Returns:



13
14
15
# File 'lib/rcap/base/area.rb', line 13

def circles
  @circles
end

- (Array<Geocode>) geocodes (readonly)

Returns:



15
16
17
# File 'lib/rcap/base/area.rb', line 15

def geocodes
  @geocodes
end

- (Array<Polygon>) polygons (readonly)

Returns:



17
18
19
# File 'lib/rcap/base/area.rb', line 17

def polygons
  @polygons
end

Class Method Details

+ (Area) from_h(area_hash)

Parameters:

  • area_hash (Hash)

Returns:



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/rcap/base/area.rb', line 197

def self.from_h( area_hash )
  self.new do |area|
    area.area_desc = area_hash[ AREA_DESC_KEY ]
    area.altitude  = area_hash[ ALTITUDE_KEY ]
    area.ceiling   = area_hash[ CEILING_KEY ]

    Array( area_hash[ CIRCLES_KEY ]).each do |circle_array|
      area.circles << area.circle_class.from_a( circle_array )
    end

    Array( area_hash[ GEOCODES_KEY ]).each do |geocode_hash|
      area.geocodes << area.geocode_class.from_h( geocode_hash )
    end

    Array( area_hash[ POLYGONS_KEY ]).each do |polygon_hash|
      area.polygons << area.polygon_class.from_h( polygon_hash )
    end
  end
end

+ (Object) from_xml_element(area_xml_element)



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rcap/base/area.rb', line 78

def self.from_xml_element( area_xml_element )
  self.new do |area|
    area.area_desc = RCAP.xpath_text( area_xml_element, AREA_DESC_XPATH, area.xmlns )
    area.altitude  = RCAP.to_f_if_given( RCAP.xpath_text( area_xml_element, ALTITUDE_XPATH, area.xmlns ))
    area.ceiling   = RCAP.to_f_if_given( RCAP.xpath_text( area_xml_element, CEILING_XPATH, area.xmlns ))

    RCAP.xpath_match( area_xml_element, area.circle_class::XPATH, area.xmlns ).each do |circle_element|
      area.circles << area.circle_class.from_xml_element( circle_element )
    end

    RCAP.xpath_match( area_xml_element, area.geocode_class::XPATH, area.xmlns ).each do |geocode_element|
      area.geocodes  << area.geocode_class.from_xml_element( geocode_element )
    end

    RCAP.xpath_match( area_xml_element, area.polygon_class::XPATH, area.xmlns ).each do |polygon_element|
      area.polygons  << area.polygon_class.from_xml_element( polygon_element )
    end
  end
end

+ (Area) from_yaml_data(area_yaml_data)

Parameters:

  • area_yaml_data (Hash)

Returns:



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/rcap/base/area.rb', line 165

def self.from_yaml_data( area_yaml_data )
  self.new do |area|
    area.area_desc = area_yaml_data[ AREA_DESC_YAML ]
    area.altitude  = area_yaml_data[ ALTITUDE_YAML ]
    area.ceiling   = area_yaml_data[ CEILING_YAML ]

    Array( area_yaml_data[ CIRCLES_YAML ]).each do |circle_yaml_data|
      area.circles << area.circle_class.from_yaml_data( circle_yaml_data )
    end

    Array( area_yaml_data[ GEOCODES_YAML ]).each do |name, value|
      area.add_geocode do |geocode|
        geocode.name = name
        geocode.value = value
      end
    end

    Array( area_yaml_data[ POLYGONS_YAML ]).each do |polyon_yaml_data|
      area.polygons << area.polygon_class.from_yaml_data( polyon_yaml_data )
    end
  end
end

Instance Method Details

- (true, false) ==(other)

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

Parameters:

  • other (Area)

    Area object to compare

Returns:

  • (true, false)


123
124
125
126
# File 'lib/rcap/base/area.rb', line 123

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

- (Circle) add_circle

Creates a new Circle object and adds it to the #circles array.

Returns:



61
62
63
64
65
66
# File 'lib/rcap/base/area.rb', line 61

def add_circle
  self.circle_class.new.tap do |circle|
    yield( circle ) if block_given?
    @circles << circle
  end
end

- (Geocode) add_geocode

Creates a new Geocode object and adds it to the #geocodes array.

Returns:



71
72
73
74
75
76
# File 'lib/rcap/base/area.rb', line 71

def add_geocode
  self.geocode_class.new do |geocode|
    yield( geocode ) if block_given?
    @geocodes << geocode
  end
end

- (Polygon) add_polygon

Creates a new Polygon object and adds it to the #polygons array.

Returns:



51
52
53
54
55
56
# File 'lib/rcap/base/area.rb', line 51

def add_polygon
  self.polygon_class.new.tap do |polygon|
    yield( polygon ) if block_given?
    @polygons << polygon
  end
end

- (String) inspect

Returns:



129
130
131
132
133
134
135
136
# File 'lib/rcap/base/area.rb', line 129

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

- (Hash) to_h

Returns:

  • (Hash)


218
219
220
221
222
223
224
225
# File 'lib/rcap/base/area.rb', line 218

def to_h
  RCAP.attribute_values_to_hash( [ AREA_DESC_KEY, @area_desc ],
                                 [ ALTITUDE_KEY,  @altitude ],
                                 [ CEILING_KEY,   @ceiling ],
                                 [ CIRCLES_KEY,   @circles.map{ |circle| circle.to_a }],
                                 [ GEOCODES_KEY,  @geocodes.map{ |geocode| geocode.to_h }],
                                 [ POLYGONS_KEY,  @polygons.map{ |polygon| polygon.to_h }])
end

- (String) to_s

Returns the area description

Returns:



141
142
143
# File 'lib/rcap/base/area.rb', line 141

def to_s
  @area_desc
end

- (String) to_xml

XML representation of the Area

Returns:

  • (String)

    XML representation of the Area



115
116
117
# File 'lib/rcap/base/area.rb', line 115

def to_xml
  self.to_xml_element.to_s
end

- (REXML::Element) to_xml_element

Returns:

  • (REXML::Element)


99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/rcap/base/area.rb', line 99

def to_xml_element
  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 @altitude.blank?
  xml_element.add_element( CEILING_ELEMENT_NAME ).add_text( @ceiling.to_s )   unless @altitude.blank?
  xml_element
end

- (String) to_yaml(options = {})

YAML representation of object

Returns:

  • (String)

    YAML representation of object



154
155
156
157
158
159
160
161
# File 'lib/rcap/base/area.rb', line 154

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