Class: Alula::QueryInterface
- Inherits:
-
Object
- Object
- Alula::QueryInterface
- Extended by:
- Forwardable
- Defined in:
- lib/alula/query_interface.rb
Instance Method Summary collapse
- #as_json ⇒ Object
- #built_query_options ⇒ Object
- #custom_options(options = {}) ⇒ Object
- #filter(filter_hash) ⇒ Object
- #filter_builder(&block) ⇒ Object
- #include_relationships ⇒ Object
- #includes(*relationships) ⇒ Object
-
#initialize(model_class) ⇒ QueryInterface
constructor
A new instance of QueryInterface.
- #limit(limit) ⇒ Object
- #list(opts = {}) ⇒ Object
- #list_all(opts = {}) ⇒ Object
- #offset(offset) ⇒ Object
- #page(args = {}) ⇒ Object
- #query_sort(options) ⇒ Object
- #retrieve(id) ⇒ Object
- #to_json ⇒ Object
Constructor Details
#initialize(model_class) ⇒ QueryInterface
Returns a new instance of QueryInterface.
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/alula/query_interface.rb', line 5 def initialize(model_class) @model_class = model_class @filter_builder = Alula::FilterBuilder.new(model_class, self) @filter = {} @page = {} @custom_options = {} @includes = {} @opts = {} @sorts = {} end |
Instance Method Details
#as_json ⇒ Object
89 90 91 |
# File 'lib/alula/query_interface.rb', line 89 def as_json JSON.parse(to_json) end |
#built_query_options ⇒ Object
136 137 138 139 140 141 142 143 |
# File 'lib/alula/query_interface.rb', line 136 def built = Util.deep_merge({}, @page) built = Util.deep_merge(built, @includes) built = Util.deep_merge(built, @sorts) built = Util.deep_merge(built, @custom_options) Util.deep_merge(built, @filter) end |
#custom_options(options = {}) ⇒ Object
53 54 55 56 57 58 |
# File 'lib/alula/query_interface.rb', line 53 def ( = {}) @custom_options = Util.deep_merge(@custom_options, { customOptions: }) self end |
#filter(filter_hash) ⇒ Object
16 17 18 19 |
# File 'lib/alula/query_interface.rb', line 16 def filter(filter_hash) @filter = Util.deep_merge @filter, filter_hash self end |
#filter_builder(&block) ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/alula/query_interface.rb', line 21 def filter_builder(&block) if block_given? yield @filter_builder @filter = Util.deep_merge(@filter, @filter_builder.as_json) self else @filter_builder end end |
#include_relationships ⇒ Object
60 61 62 |
# File 'lib/alula/query_interface.rb', line 60 def include_relationships (omitRelationships: false) end |
#includes(*relationships) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/alula/query_interface.rb', line 64 def includes(*relationships) relationships = @model_class.get_relationships.keys if relationships == [:all] validated_relationships = relationships.map do |relationship| @model_class.check_relationship!(relationship) end @includes = Util.deep_merge(@includes, { include: validated_relationships.compact.map { |r| Util.camelize(r) }.join(',') }) self end |
#limit(limit) ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/alula/query_interface.rb', line 44 def limit(limit) @page = Util.deep_merge(@page, { page: { size: limit } }) self end |
#list(opts = {}) ⇒ Object
97 98 99 |
# File 'lib/alula/query_interface.rb', line 97 def list(opts = {}) @model_class.list(, @opts.merge(opts)) end |
#list_all(opts = {}) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/alula/query_interface.rb', line 101 def list_all(opts = {}) # We have defaults we want to use, but if the caller wants to change, we support it size = @page.to_h.dig(:page, :size) || 250 number = @page.to_h.dig(:page, :number) || 1 return to_enum(:list_all, opts) unless block_given? page = self.page(number: number, size: size).list(opts) return page unless page.is_a? Alula::ListObject loop do yield page break unless page..page.has_next? page = self.page(number: number += 1, size: size).list(opts) end end |
#offset(offset) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/alula/query_interface.rb', line 35 def offset(offset) @page = Util.deep_merge(@page, { page: { number: offset, } }) self end |
#page(args = {}) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/alula/query_interface.rb', line 123 def page(args = {}) number = args[:number] || 1 size = args[:size] || 10 @page = Util.deep_merge(@page, { page: { number: number, size: size } }) self end |
#query_sort(options) ⇒ Object
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/alula/query_interface.rb', line 78 def query_sort() cleaned_sorts = .each_pair.each_with_object([]) do |(key, value), collector| collector << "#{value == :desc ? '-' : ''}#{key}" end @sorts = Util.deep_merge(@sorts, { sort: cleaned_sorts.join(',') }) self end |
#retrieve(id) ⇒ Object
119 120 121 |
# File 'lib/alula/query_interface.rb', line 119 def retrieve(id) @model_class.retrieve(id, ) end |
#to_json ⇒ Object
93 94 95 |
# File 'lib/alula/query_interface.rb', line 93 def to_json JSON.generate() end |