config_yml 0.0.2 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ OTFhOTViODNiZTBlYTIzMTkzZGExMzBjZDIxMzc1ZjQzYjk3OWNmYQ==
5
+ data.tar.gz: !binary |-
6
+ ZmI0YjUxNDI3OWI5ZTgxZjc1MGZkOGRkNDdkMTJkMzExMmMyZTc5ZA==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ ZDgzNmRhZDk1YTE4NGY0MjA1MjgwOGU4NjE1MzM0NTg5YzgwMzM4MTJjM2Mx
10
+ YWQ5OTdmODE5MmYzZGEzODRlMzUyZTEzNmIzOTM4Y2ViYjIzZDg5YzU4ODdi
11
+ NWY4NGQ1ZTliZDFhYWE1Mzk3NzA0MjY5NmM5MTJkYmIzOWNlOTA=
12
+ data.tar.gz: !binary |-
13
+ NDY3ZDBiYjVjNjBiMjk3NzUwNWZmODc4YzE4NjUxYTFkMzg3NTUzMDZlMTY4
14
+ MjM1ZWFlOTI0ZmZlOWEyMjc3ZTczM2IyMjQ2NjRhMWQ5M2IzZDlmZjgxYzFm
15
+ OTU1NWNjOWRjM2M2Y2MwOGQ5ZmY4ZTc0YjBkMGM2ZTI1ZGI4OGI=
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ test/tmp
15
15
  test/version_tmp
16
16
  tmp
17
17
  .DS_Store
18
+ config/
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- config_yml (0.0.1)
4
+ config_yml (0.0.3)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -12,21 +12,25 @@ Installation
12
12
  ------------
13
13
 
14
14
  If you are using Bundler, add the following code to your Gemfile:
15
+
15
16
  ```ruby
16
17
  gem "config_yml", :require => "configuration"
17
18
  ```
18
19
 
19
20
  and run:
21
+
20
22
  ```console
21
23
  $ bundle install
22
24
  ```
23
25
 
24
26
  If you are not using Bundler:
27
+
25
28
  ```console
26
29
  $ gem install config_yml
27
30
  ```
28
31
 
29
32
  then in your Ruby code:
33
+
30
34
  ```ruby
31
35
  require "configuration"
32
36
  ```
@@ -39,7 +43,7 @@ Examples
39
43
  password: foo
40
44
  host: localhost
41
45
  port: 6379
42
- database: bar
46
+ database: 1
43
47
  ```
44
48
 
45
49
  ```ruby
@@ -48,10 +52,11 @@ if redis = Configuration.redis
48
52
  ENV["REDIS_URL"] = %(redis://#{password}#{redis[:host]}:#{redis[:port]}/#{redis[:database]})
49
53
  end
50
54
 
51
- ENV["REDIS_URL"] # => "redis://:foo@localhost:6379/bar"
55
+ ENV["REDIS_URL"] # => "redis://:foo@localhost:6379/1"
52
56
  ```
53
57
 
54
58
  You can also use the shorthand:
59
+
55
60
  ```ruby
56
61
  redis = Conf.redis
57
62
  ```
@@ -78,12 +83,14 @@ test:
78
83
  ```
79
84
 
80
85
  Rack
86
+
81
87
  ```ruby
82
88
  ENV["RACK_ENV"] # => "development"
83
89
  Configuration.mysql[:database] # => "myapp_development"
84
90
  ```
85
91
 
86
92
  Rails
93
+
87
94
  ```ruby
88
95
  Rails.env # => "test"
89
96
  Configuration.mysql[:database] # => "myapp_test"
data/Rakefile CHANGED
@@ -1 +1,4 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), 'lib'))
2
+
1
3
  require "bundler/gem_tasks"
4
+ require "configuration/tasks"
@@ -0,0 +1,7 @@
1
+ module Configuration
2
+ class Railtie < Rails::Railtie
3
+ rake_tasks do
4
+ load "configuration/tasks.rb"
5
+ end
6
+ end
7
+ end
@@ -1,5 +1,15 @@
1
1
  module Configuration
2
2
  module Syntax
3
+ # Provides a shorthand for access config files
4
+ #
5
+ # For example:
6
+ #
7
+ # # The following code
8
+ # Conf.foo
9
+ #
10
+ # # is the same as:
11
+ # Configuration.foo
12
+ #
3
13
  module Conf
4
14
  def self.method_missing(method, *args)
5
15
  Configuration.method_missing(method, args)
@@ -0,0 +1,85 @@
1
+ module Configuration
2
+ module Tasks
3
+ # Provides a method to generate yaml files from sample or example files.
4
+ # This module and methods are intended to use with a rake task and are not
5
+ # available for the rest of application.
6
+ #
7
+ # Examples:
8
+ #
9
+ # # The following files
10
+ # config/application.yml.sample
11
+ # config/database.yml.sample
12
+ # config/resque.yml.example
13
+ #
14
+ # # will generate:
15
+ # config/application.yml
16
+ # config/database.yml
17
+ # config/resque.yml
18
+ #
19
+ # If the .yml file already exists and has the same content of the related
20
+ # .sample file, the .yml will remain untouched. Otherwise, user will be
21
+ # asked whether the content will be replaced or not.
22
+ #
23
+ module Generate
24
+ class << self
25
+ def generate_ymls
26
+ grouped_files.each do |group, files|
27
+ sample = files.find { |file| file =~ /\.yml\.(s|ex)ample$/ }
28
+ destination = files.find { |file| file =~ /\.yml$/ }
29
+
30
+ begin
31
+ case files.count
32
+ when 1
33
+ generate(sample)
34
+ when 2
35
+ replace(sample, destination)
36
+ else
37
+ raise
38
+ end
39
+ rescue
40
+ handle_error(group)
41
+ end
42
+ end
43
+ end
44
+
45
+ def grouped_files
46
+ @grouped_files ||= Dir.glob("config/*.yml*").group_by do |file|
47
+ file.match(/((config\/)([^.]*))/i)[3]
48
+ end
49
+ end
50
+
51
+ private
52
+
53
+ def generate(sample)
54
+ if sample =~ /\.(s|ex)ample$/
55
+ destination = sample.sub(/\.(s|ex)ample/, '')
56
+ FileUtils.cp(sample, destination)
57
+ puts "#{destination} generated from #{sample}"
58
+ end
59
+ end
60
+
61
+ def replace(sample, destination)
62
+ if read_content(sample) != read_content(destination)
63
+ ask "Do you want to replace existing #{destination}?" do
64
+ FileUtils.cp(sample, destination)
65
+ puts "#{destination} content have been replaced by #{sample} content"
66
+ end
67
+ end
68
+ end
69
+
70
+ def read_content(file)
71
+ File.open(file, 'r').readlines
72
+ end
73
+
74
+ def handle_error(group)
75
+ puts "An error ocurred when trying to copy config/#{group}.* files."
76
+ end
77
+
78
+ def ask(message, &block)
79
+ print "#{message} (yes/no): "
80
+ yield if %w{y yes}.include? STDIN.gets.strip
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,8 @@
1
+ require "configuration/tasks/generate"
2
+
3
+ namespace :config_yml do
4
+ desc "Generate .yml files copying from .yml.sample or .yml.example files."
5
+ task :generate do
6
+ Configuration::Tasks::Generate.generate_ymls
7
+ end
8
+ end
@@ -1,3 +1,3 @@
1
1
  module Configuration
2
- VERSION = "0.0.2".freeze
2
+ VERSION = "0.0.4".freeze
3
3
  end
data/lib/configuration.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require "yaml"
2
2
 
3
3
  require "configuration/syntax"
4
+ require "configuration/railtie" if defined?(Rails)
4
5
  require "configuration/version"
5
6
 
6
7
  module Configuration
@@ -0,0 +1,97 @@
1
+ require "spec_helper"
2
+
3
+ describe Configuration::Tasks::Generate do
4
+ before do
5
+ Dir.stub(:glob).and_return(files)
6
+ subject.instance_variable_set(:@grouped_files, nil)
7
+ end
8
+
9
+ describe ".generate_ymls" do
10
+ before { subject.stub(:puts) }
11
+ after { files = nil }
12
+
13
+ context "when there is only a sample for a foo file" do
14
+ let(:files) { [sample_file] }
15
+ let(:sample_file) { "config/foo.yml.sample" }
16
+ let(:destination_file) { "config/foo.yml" }
17
+ let(:friendly_message) { "#{destination_file} generated from #{sample_file}" }
18
+
19
+ it "generates an yaml file from sample" do
20
+ FileUtils.should_receive(:cp).with(sample_file, destination_file)
21
+ subject.generate_ymls
22
+ end
23
+
24
+ it "outputs a friendly message" do
25
+ subject.should_receive(:puts).with(friendly_message)
26
+ subject.generate_ymls
27
+ end
28
+ end
29
+
30
+ context "when there is an yaml and a sample for a foo file" do
31
+ let(:files) { [destination_file, sample_file] }
32
+ let(:sample_file) { "config/foo.yml.sample" }
33
+ let(:destination_file) { "config/foo.yml" }
34
+ let(:friendly_message) { "#{destination_file} content have been replaced by #{sample_file} content" }
35
+
36
+ context "when destination_file content is distinct from sample_file" do
37
+ before do
38
+ subject.should_receive(:read_content).with(sample_file).and_return("bar")
39
+ subject.should_receive(:read_content).with(destination_file).and_return("baz")
40
+ subject.should_receive(:ask).and_yield
41
+ end
42
+
43
+ it "replaces yaml file with sample" do
44
+ FileUtils.should_receive(:cp).with(sample_file, destination_file)
45
+ subject.generate_ymls
46
+ end
47
+
48
+ it "outputs a friendly message" do
49
+ subject.should_receive(:puts).with(friendly_message)
50
+ subject.generate_ymls
51
+ end
52
+ end
53
+
54
+ context "when destination_file content is the same of sample_file" do
55
+ before { subject.stub(:read_content).and_return("bar") }
56
+
57
+ it "doesn't replace yaml file with sample" do
58
+ FileUtils.should_not_receive(:cp)
59
+ subject.generate_ymls
60
+ end
61
+
62
+ it "doesn't output a friendly message" do
63
+ subject.should_not_receive(:puts)
64
+ subject.generate_ymls
65
+ end
66
+ end
67
+ end
68
+
69
+ context "when there is another kind for a foo file" do
70
+ let(:files) { [sample_file, other_file] }
71
+ let(:sample_file) { "config/foo.yml.sample" }
72
+ let(:other_file) { "config/foo.yml.other" }
73
+ let(:error_message) { "An error ocurred when trying to copy config/foo.* files." }
74
+
75
+ it "doesn't generate an yaml file from sample" do
76
+ FileUtils.should_not_receive(:cp)
77
+ subject.generate_ymls
78
+ end
79
+
80
+ it "outputs an error message" do
81
+ subject.should_receive(:puts).with(error_message)
82
+ subject.generate_ymls
83
+ end
84
+ end
85
+ end
86
+
87
+ describe ".grouped_files" do
88
+ let(:files) { files_a + files_b }
89
+ let(:files_a) { ["config/file_a.yml", "config/file_a.yml.sample"] }
90
+ let(:files_b) { ["config/file_b.yml", "config/file_b.yml.sample"] }
91
+
92
+ it "returns a list of files grouped by file name" do
93
+ subject.grouped_files["file_a"].should be_eql files_a
94
+ subject.grouped_files["file_b"].should be_eql files_b
95
+ end
96
+ end
97
+ end
@@ -4,7 +4,7 @@ describe Configuration do
4
4
  let(:files) { ["config/file_a.yml", "config/file_b.yml"] }
5
5
 
6
6
  before do
7
- Configuration.instance_variable_set(:@hash, nil)
7
+ subject.instance_variable_set(:@hash, nil)
8
8
  Dir.stub(:glob).and_return(files)
9
9
  YAML.stub(:load_file).and_return(file_a, file_b)
10
10
  end
@@ -14,7 +14,7 @@ describe Configuration do
14
14
  let(:file_b) { double }
15
15
 
16
16
  it "loads all config files" do
17
- Configuration.files.should be_eql files
17
+ subject.files.should be_eql files
18
18
  end
19
19
  end
20
20
 
@@ -24,14 +24,14 @@ describe Configuration do
24
24
  let(:rack_env) { "rack_test" }
25
25
  let(:rails_env) { "rails_test" }
26
26
 
27
- before { Configuration.instance_variable_set(:@env, nil) }
27
+ before { subject.instance_variable_set(:@env, nil) }
28
28
 
29
29
  context "when Rack application" do
30
30
  before { ENV["RACK_ENV"] = rack_env }
31
31
  after { ENV["RACK_ENV"] = nil }
32
32
 
33
33
  it "returns the environment as symbol" do
34
- Configuration.env.should be_eql rack_env.to_sym
34
+ subject.env.should be_eql rack_env.to_sym
35
35
  end
36
36
  end
37
37
 
@@ -44,13 +44,13 @@ describe Configuration do
44
44
  after { Object.send(:remove_const, :Rails) }
45
45
 
46
46
  it "returns the environment as symbol" do
47
- Configuration.env.should be_eql rails_env.to_sym
47
+ subject.env.should be_eql rails_env.to_sym
48
48
  end
49
49
  end
50
50
 
51
51
  context "when environment isn't defined" do
52
52
  it "returns nil" do
53
- Configuration.env.should be_nil
53
+ subject.env.should be_nil
54
54
  end
55
55
  end
56
56
  end
@@ -61,8 +61,8 @@ describe Configuration do
61
61
  let(:file_b) { { key_b: "value_b" } }
62
62
 
63
63
  it "responds to keys split in different files" do
64
- Configuration.file_a[:key_a].should be_eql "value_a"
65
- Configuration.file_b[:key_b].should be_eql "value_b"
64
+ subject.file_a[:key_a].should be_eql "value_a"
65
+ subject.file_b[:key_b].should be_eql "value_b"
66
66
  end
67
67
  end
68
68
 
@@ -71,7 +71,7 @@ describe Configuration do
71
71
  let(:file_b) { double }
72
72
 
73
73
  it "responds to nested key values" do
74
- Configuration.file_a[:nested][:key].should be_eql "value"
74
+ subject.file_a[:nested][:key].should be_eql "value"
75
75
  end
76
76
  end
77
77
 
@@ -86,11 +86,11 @@ describe Configuration do
86
86
  after { ENV["RACK_ENV"] = nil }
87
87
 
88
88
  it "returns hash of environment key" do
89
- Configuration.file_a[:key].should be_eql "value_test"
89
+ subject.file_a[:key].should be_eql "value_test"
90
90
  end
91
91
 
92
92
  it "doesn't load hash of other environments" do
93
- Configuration.file_a[:key].should_not be_eql "value_dev"
93
+ subject.file_a[:key].should_not be_eql "value_dev"
94
94
  end
95
95
  end
96
96
 
@@ -99,7 +99,7 @@ describe Configuration do
99
99
  let(:file_b) { double }
100
100
 
101
101
  it "returns nil" do
102
- Configuration.foo.should be_nil
102
+ subject.foo.should be_nil
103
103
  end
104
104
  end
105
105
  end
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "configuration"
2
+ require "configuration/tasks/generate"
2
3
 
3
4
  RSpec.configure do |config|
4
5
  config.color_enabled = true
metadata CHANGED
@@ -1,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: config_yml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
5
- prerelease:
4
+ version: 0.0.4
6
5
  platform: ruby
7
6
  authors:
8
7
  - Vitor Kiyoshi Arimitsu
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-04-30 00:00:00.000000000 Z
11
+ date: 2013-05-10 00:00:00.000000000 Z
13
12
  dependencies: []
14
13
  description: A very simple way of configure your ruby applications through yaml files.
15
14
  email: to@vitork.com
@@ -24,38 +23,42 @@ files:
24
23
  - Rakefile
25
24
  - config_yml.gemspec
26
25
  - lib/configuration.rb
26
+ - lib/configuration/railtie.rb
27
27
  - lib/configuration/syntax.rb
28
28
  - lib/configuration/syntax/conf.rb
29
+ - lib/configuration/tasks.rb
30
+ - lib/configuration/tasks/generate.rb
29
31
  - lib/configuration/version.rb
30
- - spec/lib/configuration/syntax_spec.rb
32
+ - spec/lib/configuration/syntax/conf_spec.rb
33
+ - spec/lib/configuration/tasks/generate_spec.rb
31
34
  - spec/lib/configuration_spec.rb
32
35
  - spec/spec_helper.rb
33
36
  homepage: https://github.com/vitork/config_yml
34
37
  licenses:
35
38
  - MIT
39
+ metadata: {}
36
40
  post_install_message:
37
41
  rdoc_options: []
38
42
  require_paths:
39
43
  - lib
40
44
  required_ruby_version: !ruby/object:Gem::Requirement
41
- none: false
42
45
  requirements:
43
46
  - - ! '>='
44
47
  - !ruby/object:Gem::Version
45
48
  version: '0'
46
49
  required_rubygems_version: !ruby/object:Gem::Requirement
47
- none: false
48
50
  requirements:
49
51
  - - ! '>='
50
52
  - !ruby/object:Gem::Version
51
53
  version: '0'
52
54
  requirements: []
53
55
  rubyforge_project:
54
- rubygems_version: 1.8.24
56
+ rubygems_version: 2.0.3
55
57
  signing_key:
56
- specification_version: 3
58
+ specification_version: 4
57
59
  summary: Simplify your app configuration.
58
60
  test_files:
59
- - spec/lib/configuration/syntax_spec.rb
61
+ - spec/lib/configuration/syntax/conf_spec.rb
62
+ - spec/lib/configuration/tasks/generate_spec.rb
60
63
  - spec/lib/configuration_spec.rb
61
64
  - spec/spec_helper.rb