more updates for hash conversion and stuff

This commit is contained in:
John Bintz 2011-03-17 13:30:19 -04:00
parent 58aa391459
commit b374a40752
9 changed files with 140 additions and 6 deletions

View File

@ -6,6 +6,8 @@ module AVM
:province => :state_province
}
HASH_FIELDS = [ :name, :email, :telephone, :address, :city, :state, :postal_code, :country ]
attr_accessor :primary
def initialize(info)
@ -18,6 +20,7 @@ module AVM
end
def <=>(other)
return -1 if primary?
self.name <=> other.name
end
@ -28,5 +31,9 @@ module AVM
def primary?
@primary
end
def to_h
Hash[HASH_FIELDS.collect { |key| [ key, send(key) ] } ]
end
end
end

View File

@ -7,6 +7,10 @@ module AVM
def to_s
self.class.to_s.split('::').last
end
def ==(other)
self.to_s == other.to_s
end
end
klass.const_set(type.to_sym, new_klass)

View File

@ -29,6 +29,10 @@ module AVM
contacts[which]
end
def to_a
contacts.sort.collect(&:to_h)
end
def method_missing(key, *opts)
if key.to_s[-1..-1] == '='
@options[key.to_s[0..-2].to_sym] = opts.first

View File

@ -95,6 +95,14 @@ module AVM
:reference_value
]
HASH_FIELDS = [ :title, :headline, :description, :distance_notes,
:spectral_notes, :reference_url, :credit, :date,
:id, :image_type, :image_quality, :coordinate_frame,
:equinox, :reference_value, :reference_dimension, :reference_pixel,
:spatial_scale, :spatial_rotation, :coordinate_system_projection, :spatial_quality,
:spatial_notes, :fits_header, :spatial_cd_matrix, :distance
]
attr_reader :creator, :observations
def initialize(options = {})
@ -244,6 +252,12 @@ module AVM
image
end
def to_h
hash = Hash[HASH_FIELDS.collect { |key| [ key, send(key) ] }]
hash[:creator] = creator.to_a
hash
end
def method_missing(method)
@options[method]
end

View File

@ -32,6 +32,10 @@ module AVM
end
end
def to_h
Hash[@options.keys.reject { |key| key == :string_start_time }.collect { |key| [ key, send(key) ] }]
end
def self.from_xml(image, document)
observation_parts = {}

View File

@ -37,6 +37,17 @@ describe AVM::Contact do
its(:zip) { should == postal_code }
its(:country) { should == country }
its(:to_h) { should == {
:name => name,
:email => email,
:telephone => telephone,
:address => address,
:city => city,
:state => state,
:postal_code => postal_code,
:country => country
} }
its(:to_creator_list_element) { should == "<rdf:li>John Bintz</rdf:li>" }
describe 'mappings' do
@ -48,5 +59,35 @@ describe AVM::Contact do
end
end
end
context '#<=>' do
let(:second_contact) { AVM::Contact.new(second_contact_info) }
let(:contacts) { [ contact, second_contact ] }
let(:second_name) { 'Aohn Bintz' }
let(:second_contact_info) { {
:name => second_name,
:email => email,
:telephone => telephone,
:address => address,
:city => city,
:state => state,
:postal_code => postal_code,
:country => country
} }
subject { contacts.sort }
context 'primary not set' do
it { should == [ second_contact, contact ] }
end
context 'primary set' do
before { contact.primary = true }
it { should == [ contact, second_contact ] }
end
end
end

View File

@ -55,6 +55,10 @@ describe AVM::Creator do
fields = [ :address, :city, :state, :province, :postal_code, :zip, :country ]
fields.each { |field| its(field) { should == first_contact_address } }
specify { creator.to_a.should == [
first_contact.to_h
] }
context 'two contacts' do
let(:second_contact) { AVM::Contact.new(
:name => 'aa bill',
@ -69,13 +73,23 @@ describe AVM::Creator do
before { creator.contacts << second_contact }
context 'no primary, first alphabetical is primary' do
subject { second_contact }
fields.each { |field| its(field) { should == second_contact_address } }
specify { creator.to_a.should == [
second_contact.to_h, first_contact.to_h
] }
end
context 'one is primary, use it' do
before { first_contact.primary = true }
fields.each { |field| its(field) { should == first_contact_address } }
specify { creator.to_a.should == [
first_contact.to_h, second_contact.to_h
] }
end
end
end

View File

@ -27,7 +27,7 @@ describe AVM::Image do
let(:reference_dimension) { [ 200, 150 ] }
let(:reference_pixel) { [ 25, 15 ] }
let(:spatial_scale) { [ 40, 35 ] }
let(:spatial_rotation) { 10 }
let(:spatial_rotation) { 10.0 }
let(:coordinate_system_projection) { 'TAN' }
let(:spatial_quality) { 'Full' }
let(:spatial_notes) { 'Spatial Notes' }
@ -64,6 +64,12 @@ describe AVM::Image do
} }
end
let(:avm_image_type) { eval("AVM::ImageType::#{type}") }
let(:avm_image_quality) { eval("AVM::ImageQuality::#{image_quality}") }
let(:avm_coordinate_frame) { eval("AVM::CoordinateFrame::#{coordinate_frame}") }
let(:avm_coordinate_system_projection) { eval("AVM::CoordinateSystemProjection::#{coordinate_system_projection}") }
let(:avm_spatial_quality) { eval("AVM::SpatialQuality::#{spatial_quality}") }
def self.has_most_options
its(:creator) { should be_a_kind_of(AVM::Creator) }
its(:title) { should == title }
@ -75,18 +81,18 @@ describe AVM::Image do
its(:credit) { should == credit }
its(:date) { should == Time.parse(date) }
its(:id) { should == id }
its(:image_type) { should be_a_kind_of eval("AVM::ImageType::#{type}") }
its(:image_quality) { should be_a_kind_of eval("AVM::ImageQuality::#{image_quality}") }
its(:image_type) { should be_a_kind_of avm_image_type }
its(:image_quality) { should be_a_kind_of avm_image_quality }
its(:coordinate_frame) { should be_a_kind_of eval("AVM::CoordinateFrame::#{coordinate_frame}") }
its(:coordinate_frame) { should be_a_kind_of avm_coordinate_frame }
its(:equinox) { should == equinox }
its(:reference_value) { should == reference_value }
its(:reference_dimension) { should == reference_dimension }
its(:reference_pixel) { should == reference_pixel }
its(:spatial_scale) { should == spatial_scale }
its(:spatial_rotation) { should == spatial_rotation }
its(:coordinate_system_projection) { should be_a_kind_of eval("AVM::CoordinateSystemProjection::#{coordinate_system_projection}") }
its(:spatial_quality) { should be_a_kind_of eval("AVM::SpatialQuality::#{spatial_quality}") }
its(:coordinate_system_projection) { should be_a_kind_of avm_coordinate_system_projection }
its(:spatial_quality) { should be_a_kind_of avm_spatial_quality }
its(:spatial_notes) { should == spatial_notes }
its(:fits_header) { should == fits_header }
its(:spatial_cd_matrix) { should == spatial_cd_matrix }
@ -99,6 +105,34 @@ describe AVM::Image do
has_most_options
its(:to_h) { should == {
:title => title,
:headline => headline,
:description => description,
:distance_notes => distance_notes,
:spectral_notes => spectral_notes,
:reference_url => reference_url,
:credit => credit,
:date => Time.parse(date),
:id => id,
:image_type => avm_image_type.new,
:image_quality => avm_image_quality.new,
:coordinate_frame => avm_coordinate_frame.new,
:equinox => equinox,
:reference_value => reference_value,
:reference_dimension => reference_dimension,
:reference_pixel => reference_pixel,
:spatial_scale => spatial_scale,
:spatial_rotation => spatial_rotation,
:coordinate_system_projection => avm_coordinate_system_projection.new,
:spatial_quality => avm_spatial_quality.new,
:spatial_notes => spatial_notes,
:fits_header => fits_header,
:spatial_cd_matrix => spatial_cd_matrix,
:distance => [ light_years, redshift ],
:creator => []
} }
its(:distance) { should == [ light_years, redshift ] }
end

View File

@ -64,6 +64,18 @@ describe AVM::Observation do
its(:start_time) { should == Time.parse(start_time) }
its(:integration_time) { should == integration_time }
its(:dataset_id) { should == dataset_id }
its(:to_h) { should == {
:facility => facility,
:instrument => instrument,
:color_assignment => color_assignment,
:band => band,
:bandpass => bandpass,
:wavelength => wavelength,
:start_time => Time.parse(start_time),
:integration_time => integration_time,
:dataset_id => dataset_id
} }
end
context 'defaults' do