Class: ParamsReady::Query::GroupingOperator

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



119
120
121
# File 'lib/params_ready/query/grouping.rb', line 119

def type
  @type
end

Class Method Details

.instance(type) ⇒ Object

Raises:



121
122
123
124
# File 'lib/params_ready/query/grouping.rb', line 121

def self.instance(type)
  raise ParamsReadyError, "Unimplemented operator: #{type}" unless @instances.key? type
  @instances[type]
end

Instance Method Details

#==(other) ⇒ Object



149
150
151
152
# File 'lib/params_ready/query/grouping.rb', line 149

def ==(other)
  return false unless other.class <= GroupingOperator
  type == other.type
end

#arel_methodObject



126
127
128
129
130
131
132
# File 'lib/params_ready/query/grouping.rb', line 126

def arel_method
  case type
  when :and, :or then type
  else
    raise ParamsReadyError, "Unimplemented operator: #{type}"
  end
end

#connect(a, b) ⇒ Object



143
144
145
146
147
# File 'lib/params_ready/query/grouping.rb', line 143

def connect(a, b)
  return b if a.nil?
  return a if b.nil?
  a.send arel_method, b
end

#test(record, predicates) ⇒ Object



154
155
156
157
158
159
160
161
162
# File 'lib/params_ready/query/grouping.rb', line 154

def test(record, predicates)
  definite = predicates.map do |predicate|
    predicate.test(record)
  end.compact

  return nil if definite.empty?

  definite.send(test_method)
end

#test_methodObject



134
135
136
137
138
139
140
141
# File 'lib/params_ready/query/grouping.rb', line 134

def test_method
  case type
  when :and then :all?
  when :or then :any?
  else
    raise ParamsReadyError, "Unimplemented operator: #{type}"
  end
end