Class: Mixpannenkoek::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/mixpannenkoek/query.rb

Defined Under Namespace

Classes: MissingConfiguration

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, where = {}, where_not = {}, vars = {}, group = nil) ⇒ Query

Returns a new instance of Query.



10
11
12
13
14
15
16
# File 'lib/mixpannenkoek/query.rb', line 10

def initialize(klass, where = {}, where_not = {}, vars = {}, group = nil)
  @where = where
  @where_not = where_not
  @vars  = vars
  @group = group
  @klass = klass
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object



43
44
45
# File 'lib/mixpannenkoek/query.rb', line 43

def method_missing(*args, &block)
  results.send(*args, &block)
end

Instance Attribute Details

#group(field) ⇒ Object

Returns the value of attribute group.



8
9
10
# File 'lib/mixpannenkoek/query.rb', line 8

def group
  @group
end

#klassObject

Returns the value of attribute klass.



8
9
10
# File 'lib/mixpannenkoek/query.rb', line 8

def klass
  @klass
end

#where(condition = nil) ⇒ Object

Returns the value of attribute where.



8
9
10
# File 'lib/mixpannenkoek/query.rb', line 8

def where
  @where
end

Instance Method Details

#not(condition) ⇒ Object



23
24
25
# File 'lib/mixpannenkoek/query.rb', line 23

def not(condition)
  chain(where_not: @where_not.merge(condition))
end

#queryObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/mixpannenkoek/query.rb', line 61

def query
  query = @vars

  if (@where && @where != {}) || (@where_not && @where_not != {})
    query[:where] = []

    extract_dates(query, @where)

    query[:where] += @where.map do |key,value|
      where_clause(key, value)
    end

    query[:where] += @where_not.map do |key,value|
      where_not_clause(key, value)
    end

    if query[:where].compact != []
      query[:where] = query[:where].compact.join(' and ')
    else
      query.delete(:where)
    end
  end

  query[:on] = %Q(properties["#{@group}"]) if @group

  query
end

#query_with_default_scopesObject



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/mixpannenkoek/query.rb', line 47

def query_with_default_scopes
  (@klass.default_scopes.map(&:query) + [query]).inject({}) do |final_query,query|
    where = [final_query[:where], query.delete(:where)].compact.reject(&:empty?).compact
    final_query[:where] =
      if where.count > 1
        "(#{where.join(' and ')})"
      else
        where.first
      end

    final_query.merge(query)
  end
end

#request_parametersObject



39
40
41
# File 'lib/mixpannenkoek/query.rb', line 39

def request_parameters
  [@klass.endpoint, query_with_default_scopes]
end

#resultsObject



35
36
37
# File 'lib/mixpannenkoek/query.rb', line 35

def results
  Mixpannenkoek::Results.new(@klass.endpoint, response_data)
end

#set(variable) ⇒ Object



27
28
29
# File 'lib/mixpannenkoek/query.rb', line 27

def set(variable)
  chain(vars: @vars.merge(variable))
end