Skip to content

Commit

Permalink
YAML export/import improved
Browse files Browse the repository at this point in the history
  • Loading branch information
farrel committed Mar 6, 2014
1 parent 6310af3 commit 0ba8e65
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 33 deletions.
2 changes: 1 addition & 1 deletion lib/rcap/base/alert.rb
Expand Up @@ -309,7 +309,7 @@ def to_yaml( options = {} )
[ NOTE_YAML, @note ],
[ REFERENCES_YAML, @references ],
[ INCIDENTS_YAML, @incidents ],
[ INFOS_YAML, @infos ]).to_yaml( options )
[ INFOS_YAML, @infos.map( &:to_yaml_data )]).to_yaml( options )
end

# Initialise an Alert object from a YAML string. Any object that is a subclass of IO (e.g. File) can be passed in.
Expand Down
11 changes: 8 additions & 3 deletions lib/rcap/base/area.rb
Expand Up @@ -150,14 +150,19 @@ def to_s
GEOCODES_YAML = 'Geocodes'
POLYGONS_YAML = 'Polygons'

# @return [String] YAML representation of object
def to_yaml( options = {} )
# @return [Hash]
def to_yaml_data
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 )
[ POLYGONS_YAML, @polygons.map( &:to_yaml_data )])
end

# @return [String] YAML representation of object
def to_yaml( options = {} )
self.to_yaml_data.to_yaml( options )
end

# @param [Hash] area_yaml_data
Expand Down
13 changes: 9 additions & 4 deletions lib/rcap/base/info.rb
Expand Up @@ -335,8 +335,8 @@ def to_s
RESOURCES_YAML = 'Resources'
AREAS_YAML = 'Areas'

# @return [String]
def to_yaml( options = {} )
# @return [Hash]
def to_yaml_data
parameter_to_hash = lambda{ |hash, parameter| hash.merge( parameter.name => parameter.value )}

RCAP.attribute_values_to_hash( [ LANGUAGE_YAML, @language ],
Expand All @@ -357,8 +357,13 @@ def to_yaml( options = {} )
[ CONTACT_YAML, @contact ],
[ EVENT_CODES_YAML, @event_codes.inject({}, &parameter_to_hash )],
[ PARAMETERS_YAML, @parameters.inject({}, &parameter_to_hash )],
[ RESOURCES_YAML, @resources ],
[ AREAS_YAML, @areas ]).to_yaml( options )
[ RESOURCES_YAML, @resources.map( &:to_yaml_data )],
[ AREAS_YAML, @areas.map(&:to_yaml_data )])
end

# @return [String]
def to_yaml( options = {} )
self.to_yaml_data.to_yaml( options )
end

# @param [Hash] info_yaml_data
Expand Down
6 changes: 5 additions & 1 deletion lib/rcap/base/polygon.rb
Expand Up @@ -92,9 +92,13 @@ def self.from_yaml_data( polygon_yaml_data )
end
end

def to_yaml_data
@points.map{ |point| [ point.lattitude, point.longitude ]}
end

# @return [String]
def to_yaml( options = {} )
@points.map{ |point| [ point.lattitude, point.longitude ]}.to_yaml( options )
self.to_yaml_data.to_yaml( options )
end

POINTS_KEY = 'points'
Expand Down
18 changes: 11 additions & 7 deletions lib/rcap/base/resource.rb
Expand Up @@ -63,9 +63,9 @@ def self.from_xml_element( resource_xml_element )
end
end

# Calculates the SHA-1 hash and size of the contents of {#deref_uri}.
# Calculates the SHA-1 hash and size of the contents of {RCAP::Base::Resource#deref_uri}.
# Returns an array containing the size (in bytes) and SHA-1 hash if
# {#deref_uri} is present otherwise returns nil.
# {RCAP::Base::Resource#deref_uri} is present otherwise returns nil.
#
# @return [nil,Array(Integer,String)]
def calculate_hash_and_size
Expand All @@ -76,7 +76,7 @@ def calculate_hash_and_size
end
end

# The decoded contents of {#deref_uri} if present otherwise nil.
# The decoded contents of {RCAP::Base::Resource#deref_uri} if present otherwise nil.
#
# @return [nil,String]
def decoded_deref_uri
Expand Down Expand Up @@ -115,14 +115,18 @@ def to_s
SIZE_YAML = "Size"
DIGEST_YAML = "Digest"

# @param [Hash] options
# @return [String]
def to_yaml( options = {} )
def to_yaml_data
RCAP.attribute_values_to_hash( [ RESOURCE_DESC_YAML, @resource_desc ],
[ URI_YAML, @uri ],
[ MIME_TYPE_YAML, @mime_type ],
[ SIZE_YAML, @size ],
[ DIGEST_YAML, @digest ]).to_yaml( options )
[ DIGEST_YAML, @digest ])
end

# @param [Hash] options
# @return [String]
def to_yaml( options = {} )
self.to_yaml_data.to_yaml( options )
end

# @param [Hash] resource_yaml_data
Expand Down
2 changes: 1 addition & 1 deletion lib/rcap/cap_1_0/alert.rb
Expand Up @@ -117,7 +117,7 @@ def to_yaml( options = {} )
[ NOTE_YAML, @note ],
[ REFERENCES_YAML, @references ],
[ INCIDENTS_YAML, @incidents ],
[ INFOS_YAML, @infos ]).to_yaml( options )
[ INFOS_YAML, @infos.map( &:to_yaml_data )]).to_yaml( options )
end

# @param [Hash] alert_yaml_data
Expand Down
15 changes: 10 additions & 5 deletions lib/rcap/cap_1_1/info.rb
Expand Up @@ -190,9 +190,9 @@ def to_s

RESPONSE_TYPES_YAML = 'Response Types'

# @return [String]
def to_yaml( options = {} )
parameter_to_hash = lambda{ |hash, parameter| hash.merge( parameter.name => parameter.value )}
# @return [Hash]
def to_yaml_data
parameter_to_hash = lambda{ |hash, parameter| hash.merge( parameter.name => parameter.value )}

RCAP.attribute_values_to_hash( [ LANGUAGE_YAML, @language ],
[ CATEGORIES_YAML, @categories ],
Expand All @@ -213,8 +213,13 @@ def to_yaml( options = {} )
[ CONTACT_YAML, @contact ],
[ EVENT_CODES_YAML, @event_codes.inject({}, &parameter_to_hash )],
[ PARAMETERS_YAML, @parameters.inject({}, &parameter_to_hash )],
[ RESOURCES_YAML, @resources ],
[ AREAS_YAML, @areas ]).to_yaml( options )
[ RESOURCES_YAML, @resources.map( &:to_yaml_data )],
[ AREAS_YAML, @areas.map( &:to_yaml_data )])
end

# @return [String]
def to_yaml( options = {} )
self.to_yaml_data.to_yaml( options )
end

# @param [Hash] info_yaml_data
Expand Down
12 changes: 8 additions & 4 deletions lib/rcap/cap_1_1/resource.rb
Expand Up @@ -55,15 +55,19 @@ def self.from_xml_element( resource_xml_element )

DEREF_URI_YAML = "Derefrenced URI Data"

# @param [Hash] options
# @return [String]
def to_yaml( options = {} )
def to_yaml_data
RCAP.attribute_values_to_hash( [ RESOURCE_DESC_YAML, @resource_desc ],
[ URI_YAML, @uri ],
[ MIME_TYPE_YAML, @mime_type ],
[ DEREF_URI_YAML, @deref_uri ],
[ SIZE_YAML, @size ],
[ DIGEST_YAML, @digest ]).to_yaml( options )
[ DIGEST_YAML, @digest ])
end

# @param [Hash] options
# @return [String]
def to_yaml( options = {} )
self.to_yaml_data.to_yaml( options )
end

# @param [Hash] resource_yaml_data
Expand Down
13 changes: 9 additions & 4 deletions lib/rcap/cap_1_2/info.rb
Expand Up @@ -164,8 +164,8 @@ def self.from_xml_element( info_xml_element )

RESPONSE_TYPES_YAML = 'Response Types'

# @return [String]
def to_yaml( options = {} )
# @return [Hash]
def to_yaml_data
parameter_to_hash = lambda{ |hash, parameter| hash.merge( parameter.name => parameter.value )}

RCAP.attribute_values_to_hash( [ LANGUAGE_YAML, @language ],
Expand All @@ -187,8 +187,13 @@ def to_yaml( options = {} )
[ CONTACT_YAML, @contact ],
[ EVENT_CODES_YAML, @event_codes.inject({}, &parameter_to_hash )],
[ PARAMETERS_YAML, @parameters.inject({}, &parameter_to_hash )],
[ RESOURCES_YAML, @resources ],
[ AREAS_YAML, @areas ]).to_yaml( options )
[ RESOURCES_YAML, @resources.map( &:to_yaml_data )],
[ AREAS_YAML, @areas.map( &:to_yaml_data )])
end

# @return [String]
def to_yaml( options = {} )
self.to_yaml_data.to_yaml( options )
end

# @param [Hash] info_yaml_data
Expand Down
4 changes: 2 additions & 2 deletions lib/rcap/utilities.rb
Expand Up @@ -131,7 +131,7 @@ def RCAP.strip_if_given( string )
# if the string is given, String#strip and then String#to_f are applied
# otherwise nil is returned.
#
# @param [String] string
# @param [String] number
# @return [Float,nil]
def RCAP.to_f_if_given( number )
if number
Expand All @@ -149,7 +149,7 @@ def RCAP.to_f_if_given( number )
# if the string is given, String#strip and then String#to_i are applied
# otherwise nil is returned.
#
# @param [String] string
# @param [String] number
# @return [Integer,nil]
def RCAP.to_i_if_given( number )
if number
Expand Down
2 changes: 1 addition & 1 deletion lib/rcap/version.rb
@@ -1,3 +1,3 @@
module RCAP
VERSION = '2.2.1'
VERSION = '2.3.0'
end

0 comments on commit 0ba8e65

Please sign in to comment.