Class: WCC::Contentful::Store::CDNAdapter::Query

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

Constant Summary

Constants included from Query::Interface

Query::Interface::OPERATORS

Instance Method Summary collapse

Methods included from Query::Interface

#eq

Constructor Details

#initialize(store, client:, relation:, options: nil, **extra) ⇒ Query

Returns a new instance of Query.

Raises:

  • (ArgumentError)


84
85
86
87
88
89
90
91
92
93
# File 'lib/wcc/contentful/store/cdn_adapter.rb', line 84

def initialize(store, client:, relation:, options: nil, **extra)
  raise ArgumentError, 'Client cannot be nil' unless client.present?
  raise ArgumentError, 'content_type must be provided' unless relation[:content_type].present?

  @store = store
  @client = client
  @relation = relation
  @options = options || {}
  @extra = extra || {}
end

Instance Method Details

#apply(filter, context = nil) ⇒ Object

Called with a filter object by Base#find_by in order to apply the filter.



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/wcc/contentful/store/cdn_adapter.rb', line 96

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



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/wcc/contentful/store/cdn_adapter.rb', line 110

def apply_operator(operator, field, expected, context = nil)
  op = operator == :eq ? nil : operator
  if expected.is_a?(Array)
    expected = expected.join(',')
    op = :in if op.nil?
  end

  param = parameter(field, operator: op, context: context, locale: false)

  self.class.new(
    @store,
    client: @client,
    relation: @relation.merge(param => expected),
    options: @options,
    **@extra
  )
end

#nested_conditions(field, conditions, context) ⇒ Object



128
129
130
131
132
133
134
# File 'lib/wcc/contentful/store/cdn_adapter.rb', line 128

def nested_conditions(field, conditions, context)
  base_param = parameter(field)

  conditions.reduce(self) do |query, (ref, value)|
    query.apply({ "#{base_param}.#{parameter(ref)}" => value }, context)
  end
end

#to_enumObject



74
75
76
77
78
79
80
81
82
# File 'lib/wcc/contentful/store/cdn_adapter.rb', line 74

def to_enum
  return response.each_page.flat_map(&:page_items) unless @options[:include]

  response.each_page
    .flat_map { |page| page.page_items.each_with_object(page).to_a }
    .map do |e, page|
      resolve_includes(e, page.includes, depth: @options[:include])
    end
end