Module: QuickbaseRecord::Queries::ClassMethods
- Defined in:
- lib/quickbase_record/queries.rb
Instance Method Summary collapse
- #build_collection(query_response) ⇒ Object
- #build_query(query_hash) ⇒ Object
- #build_query_options(options) ⇒ Object
- #clist ⇒ Object
- #convert_field_name_to_fid(field_name) ⇒ Object
- #convert_query_string(query_string) ⇒ Object
- #convert_quickbase_response(response) ⇒ Object
- #covert_fid_to_field_name(fid) ⇒ Object
- #create(attributes = {}) ⇒ Object
- #dbid ⇒ Object
- #find(id, query_options = {}) ⇒ Object
- #join_with_and(fid, value, comparator = "EX") ⇒ Object
- #join_with_custom(fid, hash) ⇒ Object
- #join_with_or(fid, array, comparator = "EX") ⇒ Object
- #qid(id) ⇒ Object
- #where(query_hash) ⇒ Object
Instance Method Details
#build_collection(query_response) ⇒ Object
98 99 100 101 102 103 |
# File 'lib/quickbase_record/queries.rb', line 98 def build_collection(query_response) query_response.map do |response| converted_response = convert_quickbase_response(response) new(converted_response) end end |
#build_query(query_hash) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/quickbase_record/queries.rb', line 60 def build_query(query_hash) return convert_query_string(query_hash) if query_hash.is_a? String query_hash.map do |field_name, values| if field_name.is_a? Hash return field_name.map do |field_name, value| fid = convert_field_name_to_fid(field_name) join_with_or(fid, [value]) end.join('OR') end fid = convert_field_name_to_fid(field_name) if values.is_a? Array join_with_or(fid, values) elsif values.is_a? Hash join_with_custom(fid, values) else join_with_and(fid, values) end end.join("AND") end |
#build_query_options(options) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/quickbase_record/queries.rb', line 82 def () return {} unless result = {} .each do |option_name, value| if option_name.to_sym == :options result[option_name] = value else result[option_name] = convert_field_name_to_fid(value) end end return result end |
#clist ⇒ Object
15 16 17 |
# File 'lib/quickbase_record/queries.rb', line 15 def clist @clist ||= fields.reject{ |field_name| field_name == :dbid }.values.join('.') end |
#convert_field_name_to_fid(field_name) ⇒ Object
129 130 131 |
# File 'lib/quickbase_record/queries.rb', line 129 def convert_field_name_to_fid(field_name) self.fields[field_name.to_sym].to_s end |
#convert_query_string(query_string) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/quickbase_record/queries.rb', line 148 def convert_query_string(query_string) match_found = false uses_field_name = query_string.match(/\{'?(.*)'?\..*\.'?.*'?\}/)[1].to_i == 0 return query_string unless uses_field_name fields.each do |field_name, fid| field_name = field_name.to_s match_string = "\{'?(#{field_name})'?\..*\.'?.*'?\}" if query_string.scan(/#{match_string}/).length > 0 query_string.gsub!(field_name, fid.to_s) match_found = true end end if !match_found raise ArgumentError, "Invalid arguments on #{self}.where() - no matching field name found. \nMake sure the field is part of your class configuration." end return query_string end |
#convert_quickbase_response(response) ⇒ Object
137 138 139 140 141 142 143 144 145 146 |
# File 'lib/quickbase_record/queries.rb', line 137 def convert_quickbase_response(response) result = {} response.each do |fid, value| field_name = covert_fid_to_field_name(fid) result[field_name] = value end return result end |
#covert_fid_to_field_name(fid) ⇒ Object
133 134 135 |
# File 'lib/quickbase_record/queries.rb', line 133 def covert_fid_to_field_name(fid) self.fields.invert[fid.to_i] end |
#create(attributes = {}) ⇒ Object
45 46 47 48 49 |
# File 'lib/quickbase_record/queries.rb', line 45 def create(attributes = {}) object = new(attributes) object.save return object end |
#dbid ⇒ Object
11 12 13 |
# File 'lib/quickbase_record/queries.rb', line 11 def dbid @dbid ||= fields[:dbid] end |
#find(id, query_options = {}) ⇒ Object
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/quickbase_record/queries.rb', line 19 def find(id, = {}) = ([:query_options]) query = { query: build_query(id: id), clist: clist }.merge() query_response = qb_client.do_query(dbid, query).first return nil if query_response.nil? converted_response = convert_quickbase_response(query_response) new(converted_response) end |
#join_with_and(fid, value, comparator = "EX") ⇒ Object
105 106 107 |
# File 'lib/quickbase_record/queries.rb', line 105 def join_with_and(fid, value, comparator="EX") "{'#{fid}'.#{comparator}.'#{value}'}" end |
#join_with_custom(fid, hash) ⇒ Object
119 120 121 122 123 124 125 126 127 |
# File 'lib/quickbase_record/queries.rb', line 119 def join_with_custom(fid, hash) hash.map do |comparator, value| if value.is_a? Array join_with_or(fid, value, comparator) else "{'#{fid}'.#{comparator}.'#{value}'}" end end.join('AND') end |
#join_with_or(fid, array, comparator = "EX") ⇒ Object
109 110 111 112 113 114 115 116 117 |
# File 'lib/quickbase_record/queries.rb', line 109 def join_with_or(fid, array, comparator="EX") array.map do |value| if value.is_a? Hash join_with_custom(fid, value) else "{'#{fid}'.#{comparator}.'#{value}'}" end end.join("OR") end |
#qid(id) ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/quickbase_record/queries.rb', line 51 def qid(id) query = { qid: id, clist: clist } query_response = qb_client.do_query(dbid, query) return [] if query_response.first.nil? build_collection(query_response) end |
#where(query_hash) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/quickbase_record/queries.rb', line 30 def where(query_hash) if !query_hash.is_a? String = (query_hash.delete(:query_options)) else = {} end query = { query: build_query(query_hash), clist: clist }.merge() query_response = qb_client.do_query(dbid, query) return [] if query_response.first.nil? build_collection(query_response) end |