Class: Arel::Attribute

Inherits:
Object
  • Object
show all
Includes:
Expressions, Orderings, Types
Defined in:
lib/arel/algebra/attributes/attribute.rb

Defined Under Namespace

Modules: Expressions, Orderings, Types

Constant Summary collapse

PREDICATES =
[
  :eq, :eq_any, :eq_all, :not_eq, :not_eq_any, :not_eq_all, :lt, :lt_any,
  :lt_all, :lteq, :lteq_any, :lteq_all, :gt, :gt_any, :gt_all, :gteq,
  :gteq_any, :gteq_all, :matches, :matches_any, :matches_all, :not_matches,
  :not_matches_any, :not_matches_all, :in, :in_any, :in_all, :not_in,
  :not_in_any, :not_in_all
]
Predications =
Class.new do
  def self.instance_methods *args
    warn "this module is deprecated, please use the PREDICATES constant"
    PREDICATES
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Types

#type_cast, #type_cast_to_numeric, #typecast_error

Methods included from Orderings

#asc, #desc

Methods included from Expressions

#average, #count, #maximum, #minimum, #sum

Constructor Details

#initialize(relation, name, options = {}) ⇒ Attribute



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/arel/algebra/attributes/attribute.rb', line 9

def initialize(relation, name, options = {})
  @relation = relation # this is actually a table (I think)
  @name     = name
  @alias    = options[:alias]
  @ancestor = options[:ancestor]
  @history  = [self] + (@ancestor ? @ancestor.history : [])
  @root = @history.last
  @original_relation = nil
  @original_attribute = nil

  # FIXME: I think we can remove this eventually
  @hash     = name.hash + root.relation.class.hash
end

Instance Attribute Details

#aliasObject (readonly)

Returns the value of attribute alias.



6
7
8
# File 'lib/arel/algebra/attributes/attribute.rb', line 6

def alias
  @alias
end

#ancestorObject (readonly)

Returns the value of attribute ancestor.



6
7
8
# File 'lib/arel/algebra/attributes/attribute.rb', line 6

def ancestor
  @ancestor
end

#hashObject (readonly)

Returns the value of attribute hash.



6
7
8
# File 'lib/arel/algebra/attributes/attribute.rb', line 6

def hash
  @hash
end

#historyObject (readonly)

Returns the value of attribute history.



7
8
9
# File 'lib/arel/algebra/attributes/attribute.rb', line 7

def history
  @history
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/arel/algebra/attributes/attribute.rb', line 6

def name
  @name
end

#relationObject (readonly)

Returns the value of attribute relation.



6
7
8
# File 'lib/arel/algebra/attributes/attribute.rb', line 6

def relation
  @relation
end

Instance Method Details

#/(other) ⇒ Object



90
91
92
# File 'lib/arel/algebra/attributes/attribute.rb', line 90

def /(other)
  other ? (history & other.history).size : 0
end

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



31
32
33
34
35
36
37
38
# File 'lib/arel/algebra/attributes/attribute.rb', line 31

def == other
  super ||
    Attribute === other &&
    @name      == other.name &&
    @alias     == other.alias &&
    @ancestor  == other.ancestor &&
    @relation  == other.relation
end

#aggregation?Boolean



46
47
48
# File 'lib/arel/algebra/attributes/attribute.rb', line 46

def aggregation?
  false
end

#as(aliaz = nil) ⇒ Object



54
55
56
# File 'lib/arel/algebra/attributes/attribute.rb', line 54

def as(aliaz = nil)
  Attribute.new(relation, name, :alias => aliaz, :ancestor => self)
end

#bind(new_relation) ⇒ Object



58
59
60
# File 'lib/arel/algebra/attributes/attribute.rb', line 58

def bind(new_relation)
  relation == new_relation ? self : Attribute.new(new_relation, name, :alias => @alias, :ancestor => self)
end

#christenerObject



27
28
29
# File 'lib/arel/algebra/attributes/attribute.rb', line 27

def christener
  @relation.christener
end

#columnObject



292
293
294
# File 'lib/arel/algebra/attributes/attribute.rb', line 292

def column
  original_relation.column_for(self)
end

#descends_from?(other) ⇒ Boolean



86
87
88
# File 'lib/arel/algebra/attributes/attribute.rb', line 86

def descends_from?(other)
  history.include?(other)
end

#engineObject



23
24
25
# File 'lib/arel/algebra/attributes/attribute.rb', line 23

def engine
  @relation.engine
end

#eq(other) ⇒ Object



109
110
111
# File 'lib/arel/algebra/attributes/attribute.rb', line 109

def eq(other)
  Predicates::Equality.new(self, other)
end

#eq_all(*others) ⇒ Object



117
118
119
# File 'lib/arel/algebra/attributes/attribute.rb', line 117

def eq_all(*others)
  Predicates::All.build(Predicates::Equality, self, *others)
end

#eq_any(*others) ⇒ Object



113
114
115
# File 'lib/arel/algebra/attributes/attribute.rb', line 113

def eq_any(*others)
  Predicates::Any.build(Predicates::Equality, self, *others)
end

#eval(row) ⇒ Object



50
51
52
# File 'lib/arel/algebra/attributes/attribute.rb', line 50

def eval(row)
  row[self]
end

#find_correlate_in(relation) ⇒ Object



82
83
84
# File 'lib/arel/algebra/attributes/attribute.rb', line 82

def find_correlate_in(relation)
  relation[self] || self
end

#format(object) ⇒ Object



296
297
298
# File 'lib/arel/algebra/attributes/attribute.rb', line 296

def format(object)
  object.to_sql(Sql::Attribute.new(self))
end

#gt(other) ⇒ Object



157
158
159
# File 'lib/arel/algebra/attributes/attribute.rb', line 157

def gt(other)
  Predicates::GreaterThan.new(self, other)
end

#gt_all(*others) ⇒ Object



165
166
167
# File 'lib/arel/algebra/attributes/attribute.rb', line 165

def gt_all(*others)
  Predicates::All.build(Predicates::GreaterThan, self, *others)
end

#gt_any(*others) ⇒ Object



161
162
163
# File 'lib/arel/algebra/attributes/attribute.rb', line 161

def gt_any(*others)
  Predicates::Any.build(Predicates::GreaterThan, self, *others)
end

#gteq(other) ⇒ Object



169
170
171
# File 'lib/arel/algebra/attributes/attribute.rb', line 169

def gteq(other)
  Predicates::GreaterThanOrEqualTo.new(self, other)
end

#gteq_all(*others) ⇒ Object



177
178
179
# File 'lib/arel/algebra/attributes/attribute.rb', line 177

def gteq_all(*others)
  Predicates::All.build(Predicates::GreaterThanOrEqualTo, self, *others)
end

#gteq_any(*others) ⇒ Object



173
174
175
# File 'lib/arel/algebra/attributes/attribute.rb', line 173

def gteq_any(*others)
  Predicates::Any.build(Predicates::GreaterThanOrEqualTo, self, *others)
end

#in(other) ⇒ Object



205
206
207
# File 'lib/arel/algebra/attributes/attribute.rb', line 205

def in(other)
  Predicates::In.new(self, other)
end

#in_all(*others) ⇒ Object



213
214
215
# File 'lib/arel/algebra/attributes/attribute.rb', line 213

def in_all(*others)
  Predicates::All.build(Predicates::In, self, *others)
end

#in_any(*others) ⇒ Object



209
210
211
# File 'lib/arel/algebra/attributes/attribute.rb', line 209

def in_any(*others)
  Predicates::Any.build(Predicates::In, self, *others)
end

#join?Boolean



66
67
68
# File 'lib/arel/algebra/attributes/attribute.rb', line 66

def join?
  relation.join?
end

#lt(other) ⇒ Object



133
134
135
# File 'lib/arel/algebra/attributes/attribute.rb', line 133

def lt(other)
  Predicates::LessThan.new(self, other)
end

#lt_all(*others) ⇒ Object



141
142
143
# File 'lib/arel/algebra/attributes/attribute.rb', line 141

def lt_all(*others)
  Predicates::All.build(Predicates::LessThan, self, *others)
end

#lt_any(*others) ⇒ Object



137
138
139
# File 'lib/arel/algebra/attributes/attribute.rb', line 137

def lt_any(*others)
  Predicates::Any.build(Predicates::LessThan, self, *others)
end

#lteq(other) ⇒ Object



145
146
147
# File 'lib/arel/algebra/attributes/attribute.rb', line 145

def lteq(other)
  Predicates::LessThanOrEqualTo.new(self, other)
end

#lteq_all(*others) ⇒ Object



153
154
155
# File 'lib/arel/algebra/attributes/attribute.rb', line 153

def lteq_all(*others)
  Predicates::All.build(Predicates::LessThanOrEqualTo, self, *others)
end

#lteq_any(*others) ⇒ Object



149
150
151
# File 'lib/arel/algebra/attributes/attribute.rb', line 149

def lteq_any(*others)
  Predicates::Any.build(Predicates::LessThanOrEqualTo, self, *others)
end

#matches(other) ⇒ Object



181
182
183
# File 'lib/arel/algebra/attributes/attribute.rb', line 181

def matches(other)
  Predicates::Match.new(self, other)
end

#matches_all(*others) ⇒ Object



189
190
191
# File 'lib/arel/algebra/attributes/attribute.rb', line 189

def matches_all(*others)
  Predicates::All.build(Predicates::Match, self, *others)
end

#matches_any(*others) ⇒ Object



185
186
187
# File 'lib/arel/algebra/attributes/attribute.rb', line 185

def matches_any(*others)
  Predicates::Any.build(Predicates::Match, self, *others)
end

#named?(hypothetical_name) ⇒ Boolean



42
43
44
# File 'lib/arel/algebra/attributes/attribute.rb', line 42

def named?(hypothetical_name)
  (@alias || name).to_s == hypothetical_name.to_s
end

#not_eq(other) ⇒ Object



121
122
123
# File 'lib/arel/algebra/attributes/attribute.rb', line 121

def not_eq(other)
  Predicates::Inequality.new(self, other)
end

#not_eq_all(*others) ⇒ Object



129
130
131
# File 'lib/arel/algebra/attributes/attribute.rb', line 129

def not_eq_all(*others)
  Predicates::All.build(Predicates::Inequality, self, *others)
end

#not_eq_any(*others) ⇒ Object



125
126
127
# File 'lib/arel/algebra/attributes/attribute.rb', line 125

def not_eq_any(*others)
  Predicates::Any.build(Predicates::Inequality, self, *others)
end

#not_in(other) ⇒ Object



217
218
219
# File 'lib/arel/algebra/attributes/attribute.rb', line 217

def not_in(other)
  Predicates::NotIn.new(self, other)
end

#not_in_all(*others) ⇒ Object



225
226
227
# File 'lib/arel/algebra/attributes/attribute.rb', line 225

def not_in_all(*others)
  Predicates::All.build(Predicates::NotIn, self, *others)
end

#not_in_any(*others) ⇒ Object



221
222
223
# File 'lib/arel/algebra/attributes/attribute.rb', line 221

def not_in_any(*others)
  Predicates::Any.build(Predicates::NotIn, self, *others)
end

#not_matches(other) ⇒ Object



193
194
195
# File 'lib/arel/algebra/attributes/attribute.rb', line 193

def not_matches(other)
  Predicates::NotMatch.new(self, other)
end

#not_matches_all(*others) ⇒ Object



201
202
203
# File 'lib/arel/algebra/attributes/attribute.rb', line 201

def not_matches_all(*others)
  Predicates::All.build(Predicates::NotMatch, self, *others)
end

#not_matches_any(*others) ⇒ Object



197
198
199
# File 'lib/arel/algebra/attributes/attribute.rb', line 197

def not_matches_any(*others)
  Predicates::Any.build(Predicates::NotMatch, self, *others)
end

#original_attributeObject



78
79
80
# File 'lib/arel/algebra/attributes/attribute.rb', line 78

def original_attribute
  @original_attribute ||= history.detect { |a| !a.join? }
end

#original_relationObject



74
75
76
# File 'lib/arel/algebra/attributes/attribute.rb', line 74

def original_relation
  @original_relation ||= original_attribute.relation
end

#rootObject



70
71
72
# File 'lib/arel/algebra/attributes/attribute.rb', line 70

def root
  history.last
end

#to_attribute(relation) ⇒ Object



62
63
64
# File 'lib/arel/algebra/attributes/attribute.rb', line 62

def to_attribute(relation)
  bind(relation)
end

#to_sql(formatter = Sql::WhereCondition.new(relation)) ⇒ Object



300
301
302
# File 'lib/arel/algebra/attributes/attribute.rb', line 300

def to_sql(formatter = Sql::WhereCondition.new(relation))
  formatter.attribute self
end