Module: Mcmire::ArOoSelect::ArExt

Defined in:
lib/mcmire/ar_oo_select/ar_ext.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
# File 'lib/mcmire/ar_oo_select/ar_ext.rb', line 4

def self.included(base)
  base.class_eval do
    alias_method :oo_select, :oo_select_all
  end
end

Instance Method Details

#oo_select_all(query, options = {}) ⇒ Object

Executes the query and returns an array of openhashes.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/mcmire/ar_oo_select/ar_ext.rb', line 11

def oo_select_all(query, options={})
  options.reverse_merge!(:sanitize => true, :type_cast => true)
  query = sanitize_sql(query) if options[:sanitize]
  rows = connection.select_all(query)
  if options[:type_cast]
    rows.map! do |row|
      oh = ArOoSelect.open_hash.new
      row.each {|k, v| oh.send("#{k}=", oo_select_type_cast(v)) }
      oh
    end
  end
  rows
end

#oo_select_one(query, options = {}) ⇒ Object

Executes the query and returns the first row as an openhash.



26
27
28
# File 'lib/mcmire/ar_oo_select/ar_ext.rb', line 26

def oo_select_one(query, options={})
  oo_select_all(query, options).first
end

#oo_select_rows(query, options = {}) ⇒ Object

Executes the query and returns an array of rows.



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/mcmire/ar_oo_select/ar_ext.rb', line 40

def oo_select_rows(query, options={})
  options.reverse_merge!(:sanitize => true, :type_cast => true)
  query = sanitize_sql(query) if options[:sanitize]
  rows = connection.select_rows(query)
  if options[:type_cast]
    rows.map! do |row|
      row.map {|v| oo_select_type_cast(v) }
    end
  end
  rows
end

#oo_select_value(query, options = {}) ⇒ Object

Executes the query and returns the value of the first column in the first row.



31
32
33
34
35
36
37
# File 'lib/mcmire/ar_oo_select/ar_ext.rb', line 31

def oo_select_value(query, options={})
  options.reverse_merge!(:sanitize => true, :type_cast => true)
  query = sanitize_sql(query) if options[:sanitize]
  value = connection.select_value(query)
  value = oo_select_type_cast(value) if options[:type_cast]
  value
end