Module: Shawty::Helpers

Included in:
Shawty, Shawty
Defined in:
lib/shawty/helpers.rb

Instance Method Summary collapse

Instance Method Details

#connectionObject



20
21
22
# File 'lib/shawty/helpers.rb', line 20

def connection
  ActiveRecord::Base.connection
end

#database_configObject



35
36
37
# File 'lib/shawty/helpers.rb', line 35

def database_config
  YAML.load(File.read('config/database.yml'))
end

#execute(sql) ⇒ Object



8
9
10
# File 'lib/shawty/helpers.rb', line 8

def execute sql
  connection.execute sql
end

#init(environment) ⇒ Object



39
40
41
42
# File 'lib/shawty/helpers.rb', line 39

def init(environment)
  ActiveRecord::Base.establish_connection database_config[environment]
  initialize_database
end

#initialize_databaseObject



24
25
26
27
28
29
30
31
32
33
# File 'lib/shawty/helpers.rb', line 24

def initialize_database
  table = execute %Q{select * from pg_tables where tablename = #{quote 'shawty'}}
  return if table.any?
  execute %Q{
    CREATE TABLE #{table_name} (
      id SERIAL PRIMARY KEY,
      url varchar(2048) UNIQUE
    )
  }
end

#quote(string) ⇒ Object



12
13
14
# File 'lib/shawty/helpers.rb', line 12

def quote string
  connection.quote string
end

#select_column(sql) ⇒ Object



4
5
6
# File 'lib/shawty/helpers.rb', line 4

def select_column sql
  connection.select_rows(sql).flatten.first
end

#table_nameObject



16
17
18
# File 'lib/shawty/helpers.rb', line 16

def table_name
  connection.quote_table_name 'shawty'
end