From b374a40752906a3dcc8473eb1ce9634ae3350dd0 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Thu, 17 Mar 2011 13:30:19 -0400 Subject: [PATCH] more updates for hash conversion and stuff --- lib/avm/contact.rb | 7 +++++ lib/avm/controlled_vocabulary.rb | 4 +++ lib/avm/creator.rb | 4 +++ lib/avm/image.rb | 14 ++++++++++ lib/avm/observation.rb | 4 +++ spec/avm/contact_spec.rb | 41 ++++++++++++++++++++++++++++ spec/avm/creator_spec.rb | 14 ++++++++++ spec/avm/image_spec.rb | 46 +++++++++++++++++++++++++++----- spec/avm/observation_spec.rb | 12 +++++++++ 9 files changed, 140 insertions(+), 6 deletions(-) diff --git a/lib/avm/contact.rb b/lib/avm/contact.rb index 52f6867..852098a 100644 --- a/lib/avm/contact.rb +++ b/lib/avm/contact.rb @@ -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 diff --git a/lib/avm/controlled_vocabulary.rb b/lib/avm/controlled_vocabulary.rb index cb11428..34f5113 100644 --- a/lib/avm/controlled_vocabulary.rb +++ b/lib/avm/controlled_vocabulary.rb @@ -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) diff --git a/lib/avm/creator.rb b/lib/avm/creator.rb index d84344b..52b64a8 100644 --- a/lib/avm/creator.rb +++ b/lib/avm/creator.rb @@ -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 diff --git a/lib/avm/image.rb b/lib/avm/image.rb index 3601ad1..d4ebb52 100644 --- a/lib/avm/image.rb +++ b/lib/avm/image.rb @@ -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 diff --git a/lib/avm/observation.rb b/lib/avm/observation.rb index 7928f9d..982a559 100644 --- a/lib/avm/observation.rb +++ b/lib/avm/observation.rb @@ -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 = {} diff --git a/spec/avm/contact_spec.rb b/spec/avm/contact_spec.rb index 0c3e8df..53cb9db 100644 --- a/spec/avm/contact_spec.rb +++ b/spec/avm/contact_spec.rb @@ -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 == "John Bintz" } 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 diff --git a/spec/avm/creator_spec.rb b/spec/avm/creator_spec.rb index 20e64d3..9db92d5 100644 --- a/spec/avm/creator_spec.rb +++ b/spec/avm/creator_spec.rb @@ -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 diff --git a/spec/avm/image_spec.rb b/spec/avm/image_spec.rb index a9b869b..12bdc5d 100644 --- a/spec/avm/image_spec.rb +++ b/spec/avm/image_spec.rb @@ -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 diff --git a/spec/avm/observation_spec.rb b/spec/avm/observation_spec.rb index c9e59ee..039197c 100644 --- a/spec/avm/observation_spec.rb +++ b/spec/avm/observation_spec.rb @@ -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