Module: CrawlStation::Utils
- Defined in:
- lib/crawl_station/utils.rb
Class Method Summary collapse
- .create_database(module_name) ⇒ Object
- .database_config(module_name = nil) ⇒ Object
- .database_path(module_name = nil) ⇒ Object
- .gem_path ⇒ Object
- .module_path(module_name) ⇒ Object
- .render(dest_path, context) ⇒ Object
- .render_context(path, opts = {}) ⇒ Object
- .template_filepath(path) ⇒ Object
- .templates_path ⇒ Object
Class Method Details
.create_database(module_name) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/crawl_station/utils.rb', line 24 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) ⇒ Object
19 20 21 22 |
# File 'lib/crawl_station/utils.rb', line 19 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) ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/crawl_station/utils.rb', line 11 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 ⇒ Object
44 45 46 |
# File 'lib/crawl_station/utils.rb', line 44 def gem_path File.('../../crawl_station/', __FILE__) end |
.module_path(module_name) ⇒ Object
7 8 9 |
# File 'lib/crawl_station/utils.rb', line 7 def module_path(module_name) "#{CS.root}/module/#{module_name}" end |
.render(dest_path, context) ⇒ Object
53 54 55 |
# File 'lib/crawl_station/utils.rb', line 53 def render(dest_path, context) File.open(dest_path,'w+') { |f| f.write context } end |
.render_context(path, opts = {}) ⇒ Object
48 49 50 51 |
# File 'lib/crawl_station/utils.rb', line 48 def render_context(path, opts = {}) template = IO.read(path) ERB.new(template).result(OpenStruct.new(opts).instance_eval { binding }) end |
.template_filepath(path) ⇒ Object
40 41 42 |
# File 'lib/crawl_station/utils.rb', line 40 def template_filepath(path) "#{templates_path}/#{path}" end |
.templates_path ⇒ Object
36 37 38 |
# File 'lib/crawl_station/utils.rb', line 36 def templates_path "#{gem_path}/templates" end |