module ActiveHouse::Querying::Collect
Public Class Methods
new(*)
click to toggle source
Calls superclass method
# File lib/active_house/querying/collect.rb, line 28 def initialize(*) @collection = nil super end
Public Instance Methods
build_query()
click to toggle source
# File lib/active_house/querying/collect.rb, line 75 def build_query query_parts.reject(&:nil?).join("\n") end
collection()
click to toggle source
# File lib/active_house/querying/collect.rb, line 53 def collection @collection ||= fetch_collection end
count(value = 'COUNT() AS cnt')
click to toggle source
# File lib/active_house/querying/collect.rb, line 97 def count(value = 'COUNT() AS cnt') return 0 if group_values except(:select, :limit, :offset, :order).pluck(value).first end
fetch_collection()
click to toggle source
# File lib/active_house/querying/collect.rb, line 57 def fetch_collection to_hashes.map { |row| model_class.load!(row) } end
group_values()
click to toggle source
# File lib/active_house/querying/collect.rb, line 83 def group_values values[:group_by].empty? ? nil : values[:group_by] end
klass()
click to toggle source
# File lib/active_house/querying/collect.rb, line 79 def klass model_class end
loaded?()
click to toggle source
# File lib/active_house/querying/collect.rb, line 41 def loaded? !@collection.nil? end
pluck(*fields)
click to toggle source
# File lib/active_house/querying/collect.rb, line 87 def pluck(*fields) result = except(:select).select(*fields).to_hashes return [] if result.empty? if result.first.keys.size == 1 result.map { |row| row.values.first } else result.map(&:values) end end
query_parts()
click to toggle source
# File lib/active_house/querying/collect.rb, line 61 def query_parts [ build_select_query_part, build_from_query_part, build_array_join_query_part, build_where_query_part, build_group_by_query_part, build_having_query_part, build_order_by_query_part, build_limit_query_part, build_union_query_part ] end
reset()
click to toggle source
# File lib/active_house/querying/collect.rb, line 37 def reset @collection = nil end
to_a()
click to toggle source
# File lib/active_house/querying/collect.rb, line 33 def to_a collection end
to_hashes()
click to toggle source
# File lib/active_house/querying/collect.rb, line 45 def to_hashes connection.select_rows(build_query.squish) end
to_query()
click to toggle source
# File lib/active_house/querying/collect.rb, line 49 def to_query build_query end