Class: TempTable::Fetch

Inherits:
Object
  • Object
show all
Defined in:
lib/temp_table/fetch.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table_name, conditions = nil) ⇒ Fetch

Returns a new instance of Fetch.



7
8
9
10
# File 'lib/temp_table/fetch.rb', line 7

def initialize(table_name, conditions = nil)
  @table_name = table_name
  @conditions = conditions
end

Instance Attribute Details

#conditionsObject

Returns the value of attribute conditions.



5
6
7
# File 'lib/temp_table/fetch.rb', line 5

def conditions
  @conditions
end

#table_nameObject

Returns the value of attribute table_name.



5
6
7
# File 'lib/temp_table/fetch.rb', line 5

def table_name
  @table_name
end

Instance Method Details

#performObject



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/temp_table/fetch.rb', line 12

def perform
  query = "SELECT * FROM #{table_name}"

  if conditions.present?
    condition_strings = conditions.map do |column, value|
      "#{column} = #{ActiveRecord::Base.connection.quote(value)}"
    end
    query += " WHERE #{condition_strings.join(' AND ')}"
  end

  ActiveRecord::Base.connection.execute(query).to_a
end