Class: Relation
- Includes:
- Enumerable
- Defined in:
- lib/activeleopard/relation.rb
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
-
#source_class ⇒ Object
readonly
Returns the value of attribute source_class.
Class Method Summary collapse
Instance Method Summary collapse
- #as_sql ⇒ Object
- #bind_params ⇒ Object
- #defaults ⇒ Object
- #distinct ⇒ Object
- #each(&prc) ⇒ Object
- #empty_cache! ⇒ Object
- #execute! ⇒ Object
- #group(grouping_attr) ⇒ Object
-
#initialize(query, source_class) ⇒ Relation
constructor
A new instance of Relation.
- #inspect ⇒ Object
- #joins(association, join_class = source_class) ⇒ Object
- #limit(n) ⇒ Object
- #order(ordering_attr) ⇒ Object
- #select(*params) ⇒ Object
- #to_a ⇒ Object
- #where(*where_params) ⇒ Object
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
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
4 5 6 |
# File 'lib/activeleopard/relation.rb', line 4 def cache @cache end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
4 5 6 |
# File 'lib/activeleopard/relation.rb', line 4 def data @data end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
4 5 6 |
# File 'lib/activeleopard/relation.rb', line 4 def query @query end |
#source_class ⇒ Object (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_clauses ⇒ Object
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_sql ⇒ Object
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_params ⇒ Object
86 87 88 |
# File 'lib/activeleopard/relation.rb', line 86 def bind_params query[:where].values end |
#defaults ⇒ Object
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 |
#distinct ⇒ Object
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 |
#inspect ⇒ Object
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) = join_class.[association] query[:join].append(, 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_a ⇒ Object
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 |