Class: Magento::Query
- Inherits:
-
Object
- Object
- Magento::Query
- Defined in:
- lib/magento/query.rb
Constant Summary collapse
- ACCEPTED_CONDITIONS =
[ 'eq', # Equals. 'finset', # A value within a set of values 'from', # The beginning of a range. Must be used with to 'gt', # Greater than 'gteq', # Greater than or equal 'in', # In. The value can contain a comma-separated list of values. 'like', # Like. The value can contain the SQL wildcard characters when like is specified. 'lt', # Less than 'lteq', # Less than or equal 'moreq', # More or equal 'neq', # Not equal 'nfinset', # A value that is not within a set of values 'nin', # Not in. The value can contain a comma-separated list of values. 'notnull', # Not null 'null', # Null 'to' # The end of a range. Must be used with from ].freeze
Instance Method Summary collapse
- #all ⇒ Object
- #count ⇒ Object
- #find_by(attributes) ⇒ Object
-
#find_each ⇒ Object
Loop all products on each page, starting from the first to the last page.
- #first ⇒ Object
-
#initialize(model, request: Request.new, api_resource: nil) ⇒ Query
constructor
A new instance of Query.
- #order(*attributes) ⇒ Object
- #page(current_page) ⇒ Object
- #page_size(page_size) ⇒ Object (also: #per)
- #select(*fields) ⇒ Object
- #where(attributes) ⇒ Object
Constructor Details
#initialize(model, request: Request.new, api_resource: nil) ⇒ Query
Returns a new instance of Query.
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/magento/query.rb', line 26 def initialize(model, request: Request.new, api_resource: nil) @model = model @request = request @filter_groups = nil @current_page = 1 @page_size = 50 @sort_orders = nil @fields = nil @endpoint = api_resource || model.api_resource end |
Instance Method Details
#all ⇒ Object
89 90 91 92 93 94 95 96 |
# File 'lib/magento/query.rb', line 89 def all result = request.get("#{endpoint}?#{query_params}").parse if model == Magento::Category model.build(result) else RecordCollection.from_magento_response(result, model: model) end end |
#count ⇒ Object
128 129 130 |
# File 'lib/magento/query.rb', line 128 def count select(:id).page_size(1).page(1).all.total_count end |
#find_by(attributes) ⇒ Object
124 125 126 |
# File 'lib/magento/query.rb', line 124 def find_by(attributes) where(attributes).first end |
#find_each ⇒ Object
Loop all products on each page, starting from the first to the last page
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/magento/query.rb', line 100 def find_each if @model == Magento::Category raise NoMethodError, 'undefined method `find_each` for Magento::Category' end @current_page = 1 loop do redords = all redords.each do |record| yield record end break if redords.last_page? @current_page = redords.next_page end end |
#first ⇒ Object
120 121 122 |
# File 'lib/magento/query.rb', line 120 def first page_size(1).page(1).all.first end |
#order(*attributes) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/magento/query.rb', line 73 def order(*attributes) self.sort_orders = [] attributes.each do |sort_order| if sort_order.is_a?(String) || sort_order.is_a?(Symbol) sort_orders << { field: verify_id(sort_order), direction: :asc } elsif sort_order.is_a?(Hash) sort_order.each do |field, direction| raise "Invalid sort order direction '#{direction}'" unless %w[asc desc].include?(direction.to_s) sort_orders << { field: verify_id(field), direction: direction } end end end self end |
#page(current_page) ⇒ Object
49 50 51 52 |
# File 'lib/magento/query.rb', line 49 def page(current_page) self.current_page = current_page self end |
#page_size(page_size) ⇒ Object Also known as: per
54 55 56 57 |
# File 'lib/magento/query.rb', line 54 def page_size(page_size) @page_size = page_size self end |
#select(*fields) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/magento/query.rb', line 61 def select(*fields) fields = fields.map { |field| parse_field(field, root: true) } if model == Magento::Category self.fields = "children_data[#{fields.join(',')}]" else self.fields = "items[#{fields.join(',')}],search_criteria,total_count" end self end |
#where(attributes) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/magento/query.rb', line 37 def where(attributes) self.filter_groups = [] unless filter_groups filters = [] attributes.each do |key, value| field, condition = parse_filter(key) value = parse_value_filter(condition, value) filters << { field: field, conditionType: condition, value: value } end filter_groups << { filters: filters } self end |