module CrawlStation::Utils
Public Class Methods
create_database(module_name)
click to toggle source
# File lib/crawl_station/utils.rb, line 23 def create_database(module_name) config = database_config(module_name) ActiveRecord::Base.logger = CS.logger begin ActiveRecord::Base.establish_connection config ActiveRecord::Base.connection rescue ActiveRecord::Base.establish_connection config.merge(database: nil) ActiveRecord::Base.connection.create_database config[:database] end end
database_config(module_name = nil)
click to toggle source
# File lib/crawl_station/utils.rb, line 18 def database_config(module_name = nil) result = ERB.new(IO.read(database_path(module_name))).result ::YAML.load(result).deep_symbolize_keys[CS.env.to_sym] end
database_path(module_name = nil)
click to toggle source
# File lib/crawl_station/utils.rb, line 10 def database_path(module_name = nil) global_db_path = "#{CS.root}/config/database.yml" return global_db_path if module_name.nil? path = "#{module_path(module_name)}/config/database.yml" return path if File.exist?(path) global_db_path end
gem_path()
click to toggle source
# File lib/crawl_station/utils.rb, line 43 def gem_path File.expand_path('../../crawl_station/', __FILE__) end
module_path(module_name)
click to toggle source
# File lib/crawl_station/utils.rb, line 6 def module_path(module_name) "#{CS.root}/module/#{module_name}" end
render(dest_path, context)
click to toggle source
# File lib/crawl_station/utils.rb, line 52 def render(dest_path, context) File.open(dest_path,'w+') { |f| f.write context } end
render_context(path, opts = {})
click to toggle source
# File lib/crawl_station/utils.rb, line 47 def render_context(path, opts = {}) template = IO.read(path) ERB.new(template).result(OpenStruct.new(opts).instance_eval { binding }) end
template_filepath(path)
click to toggle source
# File lib/crawl_station/utils.rb, line 39 def template_filepath(path) "#{templates_path}/#{path}" end
templates_path()
click to toggle source
# File lib/crawl_station/utils.rb, line 35 def templates_path "#{gem_path}/templates" end