Class: Sequel::SQL::Expression

Inherits:
Object
  • Object
show all
Defined in:
lib/sequel/sql.rb,
lib/sequel/extensions/eval_inspect.rb

Overview

Base class for all SQL expression objects.

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.comparison_attrsObject (readonly)

All attributes used for equality and hash methods.



69
70
71
# File 'lib/sequel/sql.rb', line 69

def comparison_attrs
  @comparison_attrs
end

Class Method Details

.attr_reader(*args) ⇒ Object

Expression objects are assumed to be value objects, where their attribute values can't change after assignment. In order to make it easy to define equality and hash methods, subclass instances assume that the only values that affect the results of such methods are the values of the object's attributes.



76
77
78
79
# File 'lib/sequel/sql.rb', line 76

def attr_reader(*args)
  super
  comparison_attrs.concat(args)
end

.inherited(subclass) ⇒ Object

Copy the comparison_attrs into the subclass.



82
83
84
85
# File 'lib/sequel/sql.rb', line 82

def inherited(subclass)
  super
  subclass.instance_variable_set(:@comparison_attrs, comparison_attrs.dup)
end

Instance Method Details

#==(other) ⇒ Object

Alias of eql?



109
110
111
# File 'lib/sequel/sql.rb', line 109

def ==(other)
  eql?(other)
end

#cloneObject Also known as: dup

Make clone/dup return self, since Expression objects are supposed to be frozen value objects



103
104
105
# File 'lib/sequel/sql.rb', line 103

def clone
  self
end

#eql?(other) ⇒ Boolean

Returns true if the receiver is the same expression as the the other expression.

Returns:

  • (Boolean)


115
116
117
# File 'lib/sequel/sql.rb', line 115

def eql?(other)
  other.is_a?(self.class) && !self.class.comparison_attrs.find{|a| public_send(a) != other.public_send(a)}
end

#hashObject

Make sure that the hash value is the same if the attributes are the same.



120
121
122
# File 'lib/sequel/sql.rb', line 120

def hash
  ([self.class] + self.class.comparison_attrs.map{|x| public_send(x)}).hash
end

#inspectObject

Attempt to produce a string suitable for eval, such that:

eval(obj.inspect) == obj


125
126
127
# File 'lib/sequel/sql.rb', line 125

def inspect
  "#<#{self.class} #{instance_variables.map{|iv| "#{iv}=>#{instance_variable_get(iv).inspect}"}.join(', ')}>"
end