Module: Shawty::Helpers
Instance Method Summary collapse
- #connection ⇒ Object
- #database_config ⇒ Object
- #execute(sql) ⇒ Object
- #init(environment) ⇒ Object
- #initialize_database ⇒ Object
- #quote(string) ⇒ Object
- #select_column(sql) ⇒ Object
- #table_name ⇒ Object
Instance Method Details
#connection ⇒ Object
20 21 22 |
# File 'lib/shawty/helpers.rb', line 20 def connection ActiveRecord::Base.connection end |
#database_config ⇒ Object
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_database ⇒ Object
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_name ⇒ Object
16 17 18 |
# File 'lib/shawty/helpers.rb', line 16 def table_name connection.quote_table_name 'shawty' end |