class Filemaker::Resultset
Attributes
@return [Integer] number of records
@return [String] Ruby's date format directive
@return [Hash] representing the top-level non-portal field-definition
@return [Array] hold records
@return [Hash] the request params
@return [Hash] representing the portal field-definition
@return [Filemaker::Server] the server
@return [String] Ruby's time format directive
@return [String] Ruby's date and time format directive
@return [Integer] total count of the record
@return [String] the raw XML for inspection
Public Class Methods
@param [Filemaker::Server] server @param [String] xml The XML string from response @param [Hash] params The request params used to construct request
# File lib/filemaker/resultset.rb, line 44 def initialize(server, xml, params = nil) @list = [] @fields = {} @portal_fields = {} @server = server @params = params # Useful for debugging doc = Nokogiri::XML(xml).remove_namespaces! @xml = doc.to_xml(indent: 2) # Just want to pretty print it error_code = doc.xpath('/fmresultset/error').attribute('code').value.to_i raise_potential_error!(error_code) datasource = doc.xpath('/fmresultset/datasource') metadata = doc.xpath('/fmresultset/metadata') resultset = doc.xpath('/fmresultset/resultset') records = resultset.xpath('record') @date_format = convert_format(datasource.attribute('date-format').value) @time_format = convert_format(datasource.attribute('time-format').value) @timestamp_format = \ convert_format(datasource.attribute('timestamp-format').value) @count = resultset.attribute('count').value.to_i @total_count = datasource.attribute('total-count').value.to_i build_metadata(metadata) build_records(records) end
Public Instance Methods
Delegate to list -> map, filter, reverse, etc
# File lib/filemaker/resultset.rb, line 75 def each(*args, &block) list.each(*args, &block) end
# File lib/filemaker/resultset.rb, line 79 def size list.size end
Private Instance Methods
# File lib/filemaker/resultset.rb, line 95 def build_metadata(metadata) metadata.xpath('field-definition').each do |definition| @fields[definition['name']] = Metadata::Field.new(definition, self) end metadata.xpath('relatedset-definition').each do |relatedset| table_name = relatedset.attribute('table').value p_fields = {} @portal_fields[table_name] ||= p_fields relatedset.xpath('field-definition').each do |definition| # Right now, I do not want to mess with the field name # name = definition['name'].gsub("#{table_name}::", '') name = definition['name'] @portal_fields[table_name][name] = \ Metadata::Field.new(definition, self) end end end
# File lib/filemaker/resultset.rb, line 116 def build_records(records) records.each do |record| # record is Nokogiri::XML::Element list << Filemaker::Record.new(record, self) end end
# File lib/filemaker/resultset.rb, line 123 def convert_format(format) format .gsub('MM', '%m') .gsub('dd', '%d') .gsub('yyyy', '%Y') .gsub('HH', '%H') .gsub('mm', '%M') .gsub('ss', '%S') end
For 401 (No records match the request) and 101 (Record
is missing), we will return empty array or nil back rather than raise those errors. Is it a good design? We need to find out after production usage.
# File lib/filemaker/resultset.rb, line 89 def raise_potential_error!(error_code) return if error_code.zero? || error_code == 401 || error_code == 101 Filemaker::Errors.raise_error_by_code(error_code) end