Class: WCC::Contentful::Store::Base::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/wcc/contentful/store/base.rb

Overview

The base class for query objects returned by find_all. Subclasses should override the #result method to return an array-like containing the query results.

Constant Summary collapse

OPERATORS =
%i[
  eq
  ne
  all
  in
  nin
  exists
  lt
  lte
  gt
  gte
  query
  match
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(store) ⇒ Query

Returns a new instance of Query.



132
133
134
# File 'lib/wcc/contentful/store/base.rb', line 132

def initialize(store)
  @store = store
end

Instance Method Details

#apply(filter, context = nil) ⇒ Object

Called with a filter object by WCC::Contentful::Store::Base#find_by in order to apply the filter.



147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/wcc/contentful/store/base.rb', line 147

def apply(filter, context = nil)
  filter.reduce(self) do |query, (field, value)|
    if value.is_a?(Hash)
      if op?(k = value.keys.first)
        query.apply_operator(k.to_sym, field.to_s, value[k], context)
      else
        query.nested_conditions(field, value, context)
      end
    else
      query.apply_operator(:eq, field.to_s, value)
    end
  end
end

#apply_operator(operator, field, expected, context = nil) ⇒ Object

This method is abstract.

Subclasses can either override this method to properly respond to find_by query objects, or they can define a method for each supported operator. Ex. ‘#eq`, `#ne`, `#gt`.



139
140
141
142
143
144
# File 'lib/wcc/contentful/store/base.rb', line 139

def apply_operator(operator, field, expected, context = nil)
  respond_to?(operator) ||
    raise(ArgumentError, "Operator not implemented: #{operator}")

  public_send(operator, field, expected, context)
end

#resultObject

This method is abstract.

Subclasses should provide this in order to fetch the results of the query.

Raises:

  • (NotImplementedError)


128
129
130
# File 'lib/wcc/contentful/store/base.rb', line 128

def result
  raise NotImplementedError
end