Class: RESO::API::QueryBuilder
- Inherits:
-
Object
- Object
- RESO::API::QueryBuilder
- Includes:
- Enumerable
- Defined in:
- lib/reso_api/app/models/reso/api/query_builder.rb
Constant Summary collapse
- DEFAULT_PROPERTIES_SCOPE =
"StandardStatus in ('Active','Pending')"
Instance Method Summary collapse
- #[](index) ⇒ Object
-
#add_condition(fragment) ⇒ Object
— Internal —.
- #count ⇒ Object
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
-
#find(key) ⇒ Object
— Terminal methods —.
- #find_by(conditions) ⇒ Object
- #find_each(batch_size: 200, &block) ⇒ Object
- #first(n = nil) ⇒ Object
- #includes(*names) ⇒ Object
-
#initialize(client:, resource:) ⇒ QueryBuilder
constructor
A new instance of QueryBuilder.
- #inspect ⇒ Object
- #length ⇒ Object
- #limit(value) ⇒ Object
- #offset(value) ⇒ Object
- #order(*args) ⇒ Object
- #reload ⇒ Object
- #select(*fields) ⇒ Object
- #size ⇒ Object
- #to_a ⇒ Object
- #to_ary ⇒ Object
- #unscoped ⇒ Object
-
#where(conditions = nil) ⇒ Object
— Chainable methods —.
Constructor Details
#initialize(client:, resource:) ⇒ QueryBuilder
Returns a new instance of QueryBuilder.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/reso_api/app/models/reso/api/query_builder.rb', line 8 def initialize(client:, resource:) @client = client @resource = resource @conditions = [] @select_fields = nil @order_clauses = nil @limit_value = nil @offset_value = nil @includes_values = nil @count_flag = false @unscoped_flag = false @loaded = false @records = nil end |
Instance Method Details
#[](index) ⇒ Object
166 167 168 |
# File 'lib/reso_api/app/models/reso/api/query_builder.rb', line 166 def [](index) to_a[index] end |
#add_condition(fragment) ⇒ Object
— Internal —
172 173 174 175 176 177 |
# File 'lib/reso_api/app/models/reso/api/query_builder.rb', line 172 def add_condition(fragment) @conditions << fragment @loaded = false @records = nil self end |
#count ⇒ Object
112 113 114 115 116 117 118 |
# File 'lib/reso_api/app/models/reso/api/query_builder.rb', line 112 def count builder = clone_builder builder.instance_variable_set(:@count_flag, true) builder.instance_variable_set(:@limit_value, 1) response = builder.execute_raw response['@odata.totalCount'].to_i end |
#each(&block) ⇒ Object
120 121 122 123 124 125 126 |
# File 'lib/reso_api/app/models/reso/api/query_builder.rb', line 120 def each(&block) if block_given? execute_with_pagination(&block) else to_a.each end end |
#empty? ⇒ Boolean
162 163 164 |
# File 'lib/reso_api/app/models/reso/api/query_builder.rb', line 162 def empty? to_a.empty? end |
#find(key) ⇒ Object
— Terminal methods —
88 89 90 91 92 |
# File 'lib/reso_api/app/models/reso/api/query_builder.rb', line 88 def find(key) endpoint = Client::DETAIL_ENDPOINTS[@resource.to_s.singularize.to_sym] raise ArgumentError, "Unknown resource: #{@resource}" unless endpoint @client.send(:perform_call, "#{endpoint}('#{key}')", nil) end |
#find_by(conditions) ⇒ Object
94 95 96 97 98 |
# File 'lib/reso_api/app/models/reso/api/query_builder.rb', line 94 def find_by(conditions) builder = where(conditions).limit(1) builder.load builder.records.first end |
#find_each(batch_size: 200, &block) ⇒ Object
128 129 130 131 132 |
# File 'lib/reso_api/app/models/reso/api/query_builder.rb', line 128 def find_each(batch_size: 200, &block) builder = clone_builder builder.instance_variable_set(:@limit_value, batch_size) builder.execute_with_pagination(&block) end |
#first(n = nil) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/reso_api/app/models/reso/api/query_builder.rb', line 100 def first(n = nil) if n builder = limit(n) builder.load builder.records else builder = limit(1) builder.load builder.records.first end end |
#includes(*names) ⇒ Object
74 75 76 77 78 |
# File 'lib/reso_api/app/models/reso/api/query_builder.rb', line 74 def includes(*names) builder = clone_builder builder.instance_variable_set(:@includes_values, names.flatten.map(&:to_s)) builder end |
#inspect ⇒ Object
149 150 151 152 |
# File 'lib/reso_api/app/models/reso/api/query_builder.rb', line 149 def inspect load @records.inspect end |
#length ⇒ Object
158 159 160 |
# File 'lib/reso_api/app/models/reso/api/query_builder.rb', line 158 def length to_a.length end |
#limit(value) ⇒ Object
62 63 64 65 66 |
# File 'lib/reso_api/app/models/reso/api/query_builder.rb', line 62 def limit(value) builder = clone_builder builder.instance_variable_set(:@limit_value, value) builder end |
#offset(value) ⇒ Object
68 69 70 71 72 |
# File 'lib/reso_api/app/models/reso/api/query_builder.rb', line 68 def offset(value) builder = clone_builder builder.instance_variable_set(:@offset_value, value) builder end |
#order(*args) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/reso_api/app/models/reso/api/query_builder.rb', line 46 def order(*args) builder = clone_builder clauses = args.flat_map do |arg| case arg when Hash arg.map { |field, dir| "#{field} #{dir}" } when String [arg] else [arg.to_s] end end builder.instance_variable_set(:@order_clauses, clauses) builder end |
#reload ⇒ Object
143 144 145 146 147 |
# File 'lib/reso_api/app/models/reso/api/query_builder.rb', line 143 def reload @loaded = false @records = nil self end |
#select(*fields) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/reso_api/app/models/reso/api/query_builder.rb', line 36 def select(*fields) if fields.first.is_a?(Proc) || (fields.empty? && block_given?) return super end builder = clone_builder builder.instance_variable_set(:@select_fields, fields.flatten.map(&:to_s)) builder end |
#size ⇒ Object
154 155 156 |
# File 'lib/reso_api/app/models/reso/api/query_builder.rb', line 154 def size to_a.size end |
#to_a ⇒ Object
134 135 136 137 |
# File 'lib/reso_api/app/models/reso/api/query_builder.rb', line 134 def to_a load @records end |
#to_ary ⇒ Object
139 140 141 |
# File 'lib/reso_api/app/models/reso/api/query_builder.rb', line 139 def to_ary to_a end |
#unscoped ⇒ Object
80 81 82 83 84 |
# File 'lib/reso_api/app/models/reso/api/query_builder.rb', line 80 def unscoped builder = clone_builder builder.instance_variable_set(:@unscoped_flag, true) builder end |
#where(conditions = nil) ⇒ Object
— Chainable methods —
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/reso_api/app/models/reso/api/query_builder.rb', line 25 def where(conditions = nil) if conditions.nil? return WhereChain.new(clone_builder) end builder = clone_builder fragments = QueryConditions.parse(conditions) fragments.each { |f| builder.add_condition(f) } builder end |