Class: Dynomite::Item::Query::Relation::WhereGroup

Inherits:
Object
  • Object
show all
Includes:
ComparisionMap
Defined in:
lib/dynomite/item/query/relation/where_group.rb

Constant Summary

Constants included from ComparisionMap

ComparisionMap::COMPARISION_MAP

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ComparisionMap

#comparision_for, #comparision_operators

Constructor Details

#initialize(relation, hash = {}, meta = {}) ⇒ WhereGroup

Returns a new instance of WhereGroup.



7
8
9
10
11
# File 'lib/dynomite/item/query/relation/where_group.rb', line 7

def initialize(relation, hash={}, meta={})
  @relation = relation
  @hash = hash
  @meta = meta
end

Instance Attribute Details

#hashObject

Returns the value of attribute hash.



6
7
8
# File 'lib/dynomite/item/query/relation/where_group.rb', line 6

def hash
  @hash
end

#metaObject

Returns the value of attribute meta.



6
7
8
# File 'lib/dynomite/item/query/relation/where_group.rb', line 6

def meta
  @meta
end

Instance Method Details

#build_compare(where_field) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/dynomite/item/query/relation/where_group.rb', line 53

def build_compare(where_field)
  reference = where_field.reference
  operator = where_field.operator
  expression = case operator
  when 'in'
    # ProductStatus in (:avail, :back, :disc)
    array = Array(where_field.value)
    list = array.map.with_index { |v, i| ":#{reference}_#{i}" }.join(', ') # values
    "##{reference} in (#{list})"
  when 'between'
    # sortKeyName between :sortkeyval1 AND :sortkeyval2
    "##{reference} between :#{reference}_0 AND :#{reference}_1"
  when 'begins_with'
    # begins_with ( sortKeyName, :sortkeyval )
    "begins_with(##{reference}, :#{reference})"
  when *comparision_operators # eq, gt, gte, lt, lte, =, >, >=, <, <=
    comparision = comparision_for(operator)
    "##{reference} #{comparision} :#{reference}"
  else
    "##{reference} = :#{reference}"
  end
  expression = "NOT (#{expression})" if where_field.not?
  expression
end

#build_compare_expression_ifObject

Method helps remove duplication and DRY up building of compare expression. It a bit confusing but unsure how to make it clearer.



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/dynomite/item/query/relation/where_group.rb', line 41

def build_compare_expression_if
  comparisions = []
  each do |where_field|
    field = where_field.field
    next unless yield(field) # only build if condition is true
    comparisions << build_compare(where_field)
  end
  unless comparisions.empty?
    ComparisionExpression.new(self, comparisions) # to pass in where_group for or? and not?
  end
end

#eachObject



17
18
19
20
21
22
23
# File 'lib/dynomite/item/query/relation/where_group.rb', line 17

def each
  @hash.each do |full_field, value|
    where_field = WhereField.new(full_field, value, @relation.index)
    yield where_field
    @relation.index += 1
  end
end

#fieldsObject



25
26
27
28
29
# File 'lib/dynomite/item/query/relation/where_group.rb', line 25

def fields
  @hash.map do |full_field, value|
    full_field.to_s.split('.').first
  end
end

#not?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/dynomite/item/query/relation/where_group.rb', line 31

def not?
  @meta[:not]
end

#or?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/dynomite/item/query/relation/where_group.rb', line 35

def or?
  @meta[:or]
end

#to_sObject



13
14
15
# File 'lib/dynomite/item/query/relation/where_group.rb', line 13

def to_s
  "#<#{self.class.name} @hash=#{@hash} @meta=#{@meta}>"
end