Class: WCC::Contentful::Middleware::Store::DelegatingQuery

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Store::Query::Interface
Defined in:
lib/wcc/contentful/middleware/store.rb

Constant Summary

Constants included from Store::Query::Interface

Store::Query::Interface::OPERATORS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Store::Query::Interface

#eq

Constructor Details

#initialize(wrapped_query, middleware:, options: nil, **extra) ⇒ DelegatingQuery

Returns a new instance of DelegatingQuery.



154
155
156
157
158
159
# File 'lib/wcc/contentful/middleware/store.rb', line 154

def initialize(wrapped_query, middleware:, options: nil, **extra)
  @wrapped_query = wrapped_query
  @middleware = middleware
  @options = options || {}
  @extra = extra
end

Instance Attribute Details

#middlewareObject (readonly)

Returns the value of attribute middleware.



111
112
113
# File 'lib/wcc/contentful/middleware/store.rb', line 111

def middleware
  @middleware
end

#optionsObject (readonly)

Returns the value of attribute options.



111
112
113
# File 'lib/wcc/contentful/middleware/store.rb', line 111

def options
  @options
end

#wrapped_queryObject (readonly)

Returns the value of attribute wrapped_query.



111
112
113
# File 'lib/wcc/contentful/middleware/store.rb', line 111

def wrapped_query
  @wrapped_query
end

Instance Method Details

#apply(filter, context = nil) ⇒ Object



124
125
126
127
128
129
130
131
# File 'lib/wcc/contentful/middleware/store.rb', line 124

def apply(filter, context = nil)
  self.class.new(
    wrapped_query.apply(filter, context),
    middleware: middleware,
    options: options,
    **@extra
  )
end

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



133
134
135
136
137
138
139
140
# File 'lib/wcc/contentful/middleware/store.rb', line 133

def apply_operator(operator, field, expected, context = nil)
  self.class.new(
    wrapped_query.apply_operator(operator, field, expected, context),
    middleware: middleware,
    options: options,
    **@extra
  )
end

#countObject



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/wcc/contentful/middleware/store.rb', line 99

def count
  if middleware.has_select?
    raise NameError, "Count cannot be determined because the middleware '#{middleware}' " \
                     "implements the #select? method.  Please use '.to_a.count' to count entries that " \
                     'pass the #select? method.'
  end

  # The wrapped query may get count from the "Total" field in the response,
  # or apply a "COUNT(*)" to the query.
  wrapped_query.count
end

#to_enumObject



113
114
115
116
117
118
119
120
121
122
# File 'lib/wcc/contentful/middleware/store.rb', line 113

def to_enum
  result = wrapped_query.to_enum
  result = result.select { |x| middleware.select?(x, options) } if middleware.has_select?

  if options && options[:include]
    result = result.map { |x| middleware.resolve_includes(x, options[:include], options) }
  end

  result.map { |x| middleware.transform(x, options) }
end