Class: Relation

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/activeleopard/relation.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query, source_class) ⇒ Relation

Returns a new instance of Relation.



22
23
24
25
26
# File 'lib/activeleopard/relation.rb', line 22

def initialize(query, source_class)
  @source_class = source_class
  @query = defaults.merge(query)
  @cache = nil
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



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

def cache
  @cache
end

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#queryObject (readonly)

Returns the value of attribute query.



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

def query
  @query
end

#source_classObject (readonly)

Returns the value of attribute source_class.



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

def source_class
  @source_class
end

Class Method Details

.ordered_clausesObject



18
19
20
# File 'lib/activeleopard/relation.rb', line 18

def self.ordered_clauses
  [:select, :from, :join, :where, :group, :order, :limit]
end

Instance Method Details

#as_sqlObject



71
72
73
74
75
# File 'lib/activeleopard/relation.rb', line 71

def as_sql
  Relation.ordered_clauses.map do |clause|
    query[clause].as_sql
  end.join(" \n ")
end

#bind_paramsObject



86
87
88
# File 'lib/activeleopard/relation.rb', line 86

def bind_params
  query[:where].values
end

#defaultsObject



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/activeleopard/relation.rb', line 6

def defaults
  {
    select: SelectClause.new(["#{source_class.table_name}.*"]),
    from: FromClause.new(source_class.table_name),
    join: JoinOptions.new,
    where: WhereClause.new,
    limit: LimitClause.new,
    group: GroupClause.new,
    order: OrderClause.new
  }
end

#distinctObject



53
54
55
56
57
# File 'lib/activeleopard/relation.rb', line 53

def distinct
  query[:select].distinct = true
  empty_cache!
  self
end

#each(&prc) ⇒ Object



82
83
84
# File 'lib/activeleopard/relation.rb', line 82

def each(&prc)
  to_a.each { |el| prc.call(el) }
end

#empty_cache!Object



103
104
105
# File 'lib/activeleopard/relation.rb', line 103

def empty_cache!
  @cache = nil
end

#execute!Object



95
96
97
# File 'lib/activeleopard/relation.rb', line 95

def execute!
  @cache = DBConnection.execute(as_sql, bind_params)
end

#group(grouping_attr) ⇒ Object



59
60
61
62
63
# File 'lib/activeleopard/relation.rb', line 59

def group(grouping_attr)
  query[:group].grouping_attr = grouping_attr
  empty_cache!
  self
end

#inspectObject



99
100
101
# File 'lib/activeleopard/relation.rb', line 99

def inspect
  p to_a
end

#joins(association, join_class = source_class) ⇒ Object



34
35
36
37
38
39
# File 'lib/activeleopard/relation.rb', line 34

def joins(association, join_class = source_class)
  options = join_class.assoc_options[association]
  query[:join].append(options, join_class.table_name)
  empty_cache!
  self
end

#limit(n) ⇒ Object



41
42
43
44
45
# File 'lib/activeleopard/relation.rb', line 41

def limit(n)
  query[:limit].set(n)
  empty_cache!
  self
end

#order(ordering_attr) ⇒ Object



65
66
67
68
69
# File 'lib/activeleopard/relation.rb', line 65

def order(ordering_attr)
  query[:order].ordering_attr = ordering_attr
  empty_cache!
  self
end

#select(*params) ⇒ Object



47
48
49
50
51
# File 'lib/activeleopard/relation.rb', line 47

def select(*params)
  query[:select].params = params
  empty_cache!
  self
end

#to_aObject



90
91
92
93
# File 'lib/activeleopard/relation.rb', line 90

def to_a
  execute! if cache.nil?
  cache.map { |datum| source_class.new(datum) }
end

#where(*where_params) ⇒ Object



28
29
30
31
32
# File 'lib/activeleopard/relation.rb', line 28

def where(*where_params)
  query[:where] << where_params
  empty_cache!
  self
end