Class: Tramp::Relation

Inherits:
Object
  • Object
show all
Defined in:
lib/tramp/relation.rb

Instance Method Summary collapse

Constructor Details

#initialize(klass, relation) ⇒ Relation

Returns a new instance of Relation.



4
5
6
# File 'lib/tramp/relation.rb', line 4

def initialize(klass, relation)
  @klass, @relation = klass, relation
end

Instance Method Details

#all(callback = nil, &block) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/tramp/relation.rb', line 17

def all(callback = nil, &block)
  callback ||= block

  @relation.all do |rows|
    objects = rows.map {|r| @klass.instantiate(r) }
    callback.call(objects)
  end
end

#each(callback = nil, &block) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/tramp/relation.rb', line 8

def each(callback = nil, &block)
  callback ||= block

  @relation.each do |row|
    object = @klass.instantiate(row)
    callback.call(object)
  end
end

#first(callback = nil, &block) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/tramp/relation.rb', line 26

def first(callback = nil, &block)
  callback ||= block

  @relation.first do |row|
    object = row ? @klass.instantiate(row) : nil
    callback.call(object)
  end
end

#group(groups) ⇒ Object



43
44
45
# File 'lib/tramp/relation.rb', line 43

def group(groups)
  Relation.new(@klass, @relation.group(groups))
end

#limit(limits) ⇒ Object



51
52
53
# File 'lib/tramp/relation.rb', line 51

def limit(limits)
  Relation.new(@klass, @relation.take(limits))
end

#offset(offsets) ⇒ Object



55
56
57
# File 'lib/tramp/relation.rb', line 55

def offset(offsets)
  Relation.new(@klass, @relation.skip(offsets))
end

#order(orders) ⇒ Object



47
48
49
# File 'lib/tramp/relation.rb', line 47

def order(orders)
  Relation.new(@klass, @relation.order(orders))
end

#select(*selects) ⇒ Object



39
40
41
# File 'lib/tramp/relation.rb', line 39

def select(*selects)
  Relation.new(@klass, @relation.project(*selects))
end

#where(*conditions) ⇒ Object



35
36
37
# File 'lib/tramp/relation.rb', line 35

def where(*conditions)
  Relation.new(@klass, @relation.where(*conditions))
end