Class: NoSE::Condition

Inherits:
Object show all
Defined in:
lib/nose/statements.rb

Overview

A single condition in a where clause

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field, operator, value) ⇒ Condition

Returns a new instance of Condition.



9
10
11
12
13
14
15
16
17
# File 'lib/nose/statements.rb', line 9

def initialize(field, operator, value)
  @field = field
  @operator = operator
  @is_range = [:>, :>=, :<, :<=].include? operator
  @value = value

  # XXX: Not frozen by now to support modification during query execution
  # freeze
end

Instance Attribute Details

#fieldObject (readonly)

Returns the value of attribute field.



6
7
8
# File 'lib/nose/statements.rb', line 6

def field
  @field
end

#is_rangeObject (readonly) Also known as: range?

Returns the value of attribute is_range.



6
7
8
# File 'lib/nose/statements.rb', line 6

def is_range
  @is_range
end

#operatorObject (readonly)

Returns the value of attribute operator.



6
7
8
# File 'lib/nose/statements.rb', line 6

def operator
  @operator
end

#valueObject (readonly)

Returns the value of attribute value.



6
7
8
# File 'lib/nose/statements.rb', line 6

def value
  @value
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Compare conditions equal by their field and operator

Returns:

  • (Boolean)


25
26
27
# File 'lib/nose/statements.rb', line 25

def ==(other)
  @field == other.field && @operator == other.operator
end

#hashObject



30
31
32
# File 'lib/nose/statements.rb', line 30

def hash
  Zlib.crc32 [@field.id, @operator].to_s
end

#inspectObject



19
20
21
# File 'lib/nose/statements.rb', line 19

def inspect
  "#{@field.inspect} #{@operator} #{value}"
end

#resolve_foreign_keyCondition

If the condition is on a foreign key, resolve it to the primary key of the related entity

Returns:



37
38
39
40
41
# File 'lib/nose/statements.rb', line 37

def resolve_foreign_key
  return self unless field.is_a?(Fields::ForeignKeyField)

  Condition.new @field.entity.id_field, @operator, @value
end