From fa3035907e01d6469f0a403d8375f163594393ea Mon Sep 17 00:00:00 2001 From: Michael Lippold Date: Mon, 23 Jul 2012 10:34:18 -0700 Subject: [PATCH] Added field converter function for resultsets --- lib/rfm/metadata/field.rb | 9 +++++---- lib/rfm/resultset.rb | 18 +++++++++++++----- lib/rfm/utilities/config.rb | 3 ++- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/lib/rfm/metadata/field.rb b/lib/rfm/metadata/field.rb index 18e38a0a..7444c836 100644 --- a/lib/rfm/metadata/field.rb +++ b/lib/rfm/metadata/field.rb @@ -63,12 +63,13 @@ class Field # Initializes a field object. You'll never need to do this. Instead, get your Field objects from # ResultSet::fields - def initialize(field) + def initialize(field, converter = nil) @name = field.name #['name'] @result = field.result #['result'] @type = field.type #['type'] @max_repeats = field.max_repeats #['max-repeats'] @global = field.global #['global'] + @converter = converter end # Coerces the text value from an +fmresultset+ document into proper Ruby types based on the @@ -76,7 +77,7 @@ def initialize(field) # access field data through the Record object. def coerce(value, resultset) return nil if (value.nil? or value.empty?) - case self.result.downcase + value = case self.result.downcase when "text" then value when "number" then BigDecimal.new(value) when "date" then Date.strptime(value, resultset.date_format) @@ -85,9 +86,9 @@ def coerce(value, resultset) when "container" then URI.parse("#{resultset.server.scheme}://#{resultset.server.host_name}:#{resultset.server.port}#{value}") else nil end - + @converter.nil? ? value : @converter.call(value) end end # Field end # Metadata -end # Rfm \ No newline at end of file +end # Rfm diff --git a/lib/rfm/resultset.rb b/lib/rfm/resultset.rb index 20a34656..bafda15b 100644 --- a/lib/rfm/resultset.rb +++ b/lib/rfm/resultset.rb @@ -103,9 +103,11 @@ def initialize(*args) # xml_response, caller, portals @foundset_count = doc.foundset_count @total_count = doc.total_count @table = doc.table - - (layout.table = @table) if layout and layout.table_no_load.blank? + @converter_fields = desensitize_converter_fields + + (layout.table = @table) if layout and layout.table_no_load.blank? + parse_fields(doc) # This will always load portal meta, even if :include_portals was not specified. @@ -155,7 +157,7 @@ def parse_fields(doc) return if doc.fields.blank? doc.fields.each do |field| - @field_meta[field.name] = Rfm::Metadata::Field.new(field) + @field_meta[field.name] = Rfm::Metadata::Field.new(field, @converter_fields[field.name]) end (layout.field_names = field_names) if layout and layout.field_names_no_load.blank? end @@ -175,7 +177,13 @@ def parse_portals(doc) end (layout.portal_meta = @portal_meta) if layout and layout.portal_meta_no_load.blank? end - + + def desensitize_converter_fields + rfm_hash = Rfm::CaseInsensitiveHash.new + (state[:converters] || {} ).each_pair { |k,v| rfm_hash[k] = v } + rfm_hash + end + # def convert_date_time_format(fm_format) # fm_format.gsub!('MM', '%m') # fm_format.gsub!('dd', '%d') @@ -187,4 +195,4 @@ def parse_portals(doc) # end end -end \ No newline at end of file +end diff --git a/lib/rfm/utilities/config.rb b/lib/rfm/utilities/config.rb index 522c3dc5..b7ba0271 100644 --- a/lib/rfm/utilities/config.rb +++ b/lib/rfm/utilities/config.rb @@ -11,6 +11,7 @@ module Rfm password database layout + converters ignore_bad_data ssl root_cert @@ -180,4 +181,4 @@ def config_filter(conf, filters=nil) end # module Config -end # module Rfm \ No newline at end of file +end # module Rfm