Permalink
Browse files

add yml_parser

  • Loading branch information...
0 parents commit 8a7d93fefb6ce372cce4c3aec7896eb685c398e0 @spaghetticode committed Mar 22, 2012
@@ -0,0 +1,4 @@
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in db-manager.gemspec
+gemspec
33 README
@@ -0,0 +1,33 @@
+## dbmanager
+
+usage:
+rake db:import
+clones the remote db on the fs then imports it via mysql-dump
+
+rake db:dump
+saves the remote db on the fs
+
+All tasks will ask you which env you want to save, and if you want to import, to which env.
+
+es:
+
+be rake db:import
+
+Select the source db:
+1) beta
+2) production
+3) development
+
+1
+
+Select the destination db:
+
+1) beta
+3) development
+
+3
+
+Dumping beta db....
+Importing data to development db...
+
+done
@@ -0,0 +1,8 @@
+require 'bundler'
+Bundler::GemHelper.install_tasks
+
+require 'bundler/gem_tasks'
+require 'rspec/core/rake_task'
+
+Rspec::Core::RakeTask.new(:spec)
+task :default => :spec
@@ -0,0 +1,24 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "dbmanager/version"
+
+Gem::Specification.new do |s|
+ s.name = "dbmanager"
+ s.version = Dbmanager::VERSION
+ s.platform = Gem::Platform::RUBY
+ s.authors = ["TODO: Write your name"]
+ s.email = ["TODO: Write your email address"]
+ s.homepage = ""
+ s.summary = %q{TODO: Write a gem summary}
+ s.description = %q{TODO: Write a gem description}
+
+ s.rubyforge_project = "dbmanager"
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+
+ s.add_development_dependency 'rspec'
+ s.add_development_dependency 'rake'
+end
@@ -0,0 +1,3 @@
+module Dbmanager; end
+
+require 'dbmanager/yml_parser'
@@ -0,0 +1,3 @@
+module Dbmanager
+ VERSION = "0.0.1"
+end
@@ -0,0 +1,46 @@
+require 'erb'
+require 'yaml'
+require 'ostruct'
+
+module Dbmanager
+ module YmlParser
+ class Environment < OpenStruct; end
+
+ extend self
+
+ attr_writer :config
+
+ def config
+ @config ||= YAML.load db_config_file_parsed
+ end
+
+ def reload_config
+ @config = nil
+ config
+ end
+
+ def environments
+ @environments ||= begin
+ config.select do |key, value|
+ value.has_key?('adapter')
+ end.each_with_object({}) do |arr, hash|
+ hash[arr[0]] = Environment.new arr[1]
+ end
+ end
+ end
+
+ private
+
+ def db_config_file_parsed
+ ERB.new(File.read(db_config_file)).result
+ end
+
+ def db_config_file
+ if Rails.defined?
+ Rails.root.join 'config', 'database.yml'
+ else
+ raise 'no database.yml file found in the expected dir!'
+ end
+ end
+ end
+end
No changes.
@@ -0,0 +1,58 @@
+# This is the default login for the root user
+devLogin: &developmentLogin
+ username: root
+ password:
+
+prodLogin: &productionLogin
+ username: db_user
+ password: tango
+
+# This will include a file with user-defined login credentials (if it exists)
+<%=
+ file = File.join(RAILS_ROOT, 'config', 'dblogin.yml')
+ IO.read(file) if File.exist?(file)
+%>
+
+development:
+ adapter: mysql
+ database: demo_development
+ encoding: utf8
+ <<: *developmentLogin
+ # ale i tuoi settaggi specifici devi metterli in config/dbloginyml, non qui
+ # socket: /Applications/MAMP/tmp/mysql/mysql.sock
+
+beta:
+ adapter: mysql
+ database: demo_beta
+ username: beta_username
+ password: asdasd
+ adapter: mysql
+ port: 3306
+ host: 123.123.123.123
+ encoding: utf8
+
+production:
+ adapter: mysql
+ database: demo_production
+ username: production_username
+ password: asdasd
+ adapter: mysql
+ port: 3306
+ host: 123.123.123.123
+ encoding: utf8
+
+test:
+ adapter: mysql
+ database: demo_test
+ encoding: utf8
+ <<: *developmentLogin
+
+shop:
+ adapter: mysql
+ database: demo_shop
+ username: demo_username
+ password: asdasd
+ adapter: mysql
+ port: 3306
+ host: 123.123.123.123
+ encoding: utf8
@@ -0,0 +1,40 @@
+require 'spec_helper'
+
+module Dbmanager
+ describe YmlParser do
+ before do
+ ::RAILS_ROOT = fixture_path unless defined? RAILS_ROOT
+ YmlParser.config = nil
+ YmlParser.stub! :db_config_file => File.join(fixture_path, 'database.yml')
+ end
+
+ describe '#config' do
+ it 'should load a yml file with erb code inside' do
+ YmlParser.config.should be_a(Hash)
+ end
+
+ it 'should cache the result' do
+ YmlParser.config
+ YAML.should_not_receive(:load)
+ YmlParser.config
+ end
+ end
+
+ describe '#reload_config' do
+ it 'should reload the yml file' do
+ YmlParser.config
+ YAML.should_receive(:load)
+ YmlParser.reload_config
+ end
+ end
+
+ describe '#environments' do
+ it 'should be an hash of open structs' do
+ YmlParser.environments.should be_a(Hash)
+ YmlParser.environments.values.should be_all do |item|
+ item.is_a?(Environment)
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,9 @@
+require File.expand_path('../../lib/dbmanager', __FILE__)
+
+RSpec.configure do |config|
+ config.color_enabled = true
+end
+
+def fixture_path
+ File.expand_path('../fixtures', __FILE__)
+end

0 comments on commit 8a7d93f

Please sign in to comment.