Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added field converter feature #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions lib/rfm/metadata/field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,21 @@ 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
# type of the field. You'll never need to do this: Rfm does it automatically for you when you
# 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)
Expand All @@ -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
end # Rfm
18 changes: 13 additions & 5 deletions lib/rfm/resultset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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')
Expand All @@ -187,4 +195,4 @@ def parse_portals(doc)
# end

end
end
end
3 changes: 2 additions & 1 deletion lib/rfm/utilities/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Rfm
password
database
layout
converters
ignore_bad_data
ssl
root_cert
Expand Down Expand Up @@ -180,4 +181,4 @@ def config_filter(conf, filters=nil)

end # module Config

end # module Rfm
end # module Rfm