class Filemaker::Resultset

Attributes

count[R]

@return [Integer] number of records

date_format[R]

@return [String] Ruby's date format directive

fields[R]

@return [Hash] representing the top-level non-portal field-definition

list[R]

@return [Array] hold records

params[R]

@return [Hash] the request params

portal_fields[R]

@return [Hash] representing the portal field-definition

server[R]

@return [Filemaker::Server] the server

time_format[R]

@return [String] Ruby's time format directive

timestamp_format[R]

@return [String] Ruby's date and time format directive

total_count[R]

@return [Integer] total count of the record

xml[R]

@return [String] the raw XML for inspection

Public Class Methods

new(server, xml, params = nil) click to toggle source

@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

each(*args, &block) click to toggle source

Delegate to list -> map, filter, reverse, etc

# File lib/filemaker/resultset.rb, line 75
def each(*args, &block)
  list.each(*args, &block)
end
length()
Alias for: size
size() click to toggle source
# File lib/filemaker/resultset.rb, line 79
def size
  list.size
end
Also aliased as: length

Private Instance Methods

build_metadata(metadata) click to toggle source
# 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
build_records(records) click to toggle source
# 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
convert_format(format) click to toggle source
# 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
raise_potential_error!(error_code) click to toggle source

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