Class: Rfm::CompoundQuery

Inherits:
Array show all
Defined in:
lib/rfm/utilities/compound_query.rb

Overview

Class to build complex FMP queries. Perform Filemaker find using complex boolean logic (multiple value options for a single field), or create multiple find requests. Also allow find requests to be :omit.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Array

#_merge_object!, #rfm_extend_member, #rfm_extend_members, #rfm_extract_options!

Constructor Details

#initialize(query, options = {}) ⇒ CompoundQuery

New CompoundQuery objects expect one of 3 data types:

  • string/integer representing FMP internal record id

  • hash of find-criteria

  • array of find-criteria hashes

Returns self as [‘-fmpaction’, :key=>‘values’, :options=>‘hash’]



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rfm/utilities/compound_query.rb', line 23

def initialize(query, options={})
	@options = options
	@field_mapping = options.delete(:field_mapping) || {}
	@original_input = query
@key_values = {}
@key_arrays = []
@key_map = []
@key_map_string = ''
@key_counter = 0

	case query
when Hash
	if query.detect{|k,v| v.kind_of? Array or k == :omit}
		@query_type = 'mixed'
	else
		@query_type = 'standard'
	end
when Array
	@query_type = 'compound'
else
	@query_type = 'recid'
end
build_query
end

Instance Attribute Details

#field_mappingObject

Returns the value of attribute field_mapping.



11
12
13
# File 'lib/rfm/utilities/compound_query.rb', line 11

def field_mapping
  @field_mapping
end

#key_arraysObject

Returns the value of attribute key_arrays.



11
12
13
# File 'lib/rfm/utilities/compound_query.rb', line 11

def key_arrays
  @key_arrays
end

#key_counterObject

Returns the value of attribute key_counter.



11
12
13
# File 'lib/rfm/utilities/compound_query.rb', line 11

def key_counter
  @key_counter
end

#key_mapObject

Returns the value of attribute key_map.



11
12
13
# File 'lib/rfm/utilities/compound_query.rb', line 11

def key_map
  @key_map
end

#key_map_stringObject

Returns the value of attribute key_map_string.



11
12
13
# File 'lib/rfm/utilities/compound_query.rb', line 11

def key_map_string
  @key_map_string
end

#key_valuesObject

Returns the value of attribute key_values.



11
12
13
# File 'lib/rfm/utilities/compound_query.rb', line 11

def key_values
  @key_values
end

#original_inputObject

Returns the value of attribute original_input.



11
12
13
# File 'lib/rfm/utilities/compound_query.rb', line 11

def original_input
  @original_input
end

#query_typeObject

Returns the value of attribute query_type.



11
12
13
# File 'lib/rfm/utilities/compound_query.rb', line 11

def query_type
  @query_type
end

Class Method Details

.build_testObject



13
14
15
# File 'lib/rfm/utilities/compound_query.rb', line 13

def self.build_test
	new([{:field1=>['val1a','val1b','val1c'], :field2=>'val2'},{:omit=>true, :field3=>'val3', :field4=>'val4'}, {:omit=>true, :field5=>['val5a','val5b'], :field6=>['val6a','val6b']}], {})
end

Instance Method Details

#build_key_map(key_array) ⇒ Object

Input array of arrays. Transform single key_array into key_map (array of requests). Creates all combinations of sub-arrays where each combination contains one element of each subarray.



96
97
98
99
100
101
102
103
# File 'lib/rfm/utilities/compound_query.rb', line 96

def build_key_map(key_array)
	key_array = key_array.clone
	omit = key_array.delete(:omit)
	len = key_array.length
	flat = key_array.flatten
	rslt = flat.combination(len).select{|c| key_array.all?{|a| (a & c).size > 0}}.each{|c| c.unshift(:omit) if omit}
	@key_map.concat rslt
end

#build_key_values(input_hash) ⇒ Object

Build key-value definitions and query map ‘-q1…’. Converts query_hash to fmresultset uri format for -findquery query type.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rfm/utilities/compound_query.rb', line 72

def build_key_values(input_hash)
	input_hash = input_hash.clone
	keyarray = []
	omit = input_hash.delete(:omit)
	input_hash.each do |key,val|
		query_tag = []
		val = val.rfm_force_array
		val.each do |v|
			@key_values["-q#{key_counter}"] = field_mapping[key] || key
			@key_values["-q#{key_counter}.value"] = v
			query_tag << "q#{key_counter}"
			@key_counter += 1
		end
		keyarray << query_tag
	end
	(keyarray << :omit) if omit
	@key_arrays << keyarray
	keyarray
end

#build_query(input = original_input) ⇒ Object

Master control method to build output



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rfm/utilities/compound_query.rb', line 49

def build_query(input=original_input)
	case @query_type
	when 'mixed', 'compound'
		input.rfm_force_array.each do |hash|
			build_key_map(build_key_values(hash))
		end
		translate_key_map
		self.push '-findquery'
		self.push @key_values.merge('-query'=>@key_map_string)
	when 'standard'
		self.push '-find'
		self.push @original_input
	when 'recid'
		self.push '-find'
		self.push '-recid' => @original_input.to_s
	end
	self.push @options
	self
end

#translate_key_map(keymap = key_map) ⇒ Object

Translate @key_map to FMP -query string



106
107
108
109
110
111
# File 'lib/rfm/utilities/compound_query.rb', line 106

def translate_key_map(keymap=key_map)
	keymap = keymap.clone
	inner = keymap.collect {|a| "#{'!' if a.delete(:omit)}(#{a.join(',')})"}
	outter = inner.join(';')
	@key_map_string << outter
end