Skip to content

Commit b8c64b6

Browse files
committed
Fix: README and new place for main config
1 parent 4ab4073 commit b8c64b6

File tree

3 files changed

+119
-7
lines changed

3 files changed

+119
-7
lines changed

README.md

Lines changed: 117 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# ConfigGenerator
22

3-
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/config_generator`. To experiment with that code, run `bin/console` for an interactive prompt.
4-
5-
TODO: Delete this and the text above, and describe your gem
3+
Generate Rails config files from one application.yml
64

75
## Installation
86

@@ -22,7 +20,121 @@ Or install it yourself as:
2220

2321
## Usage
2422

25-
TODO: Write usage instructions here
23+
Example of application.yml
24+
25+
```yaml
26+
# Necessary environments. It's required key.
27+
#
28+
environments:
29+
- development
30+
- test
31+
32+
database_names: &database_names
33+
development: streamline_development
34+
test: streamline_test
35+
36+
###############################################################################
37+
38+
database.yml:
39+
# MySQL supports a reconnect flag in its connections - if set to true, then the client will try
40+
# reconnecting to the server before giving up in case of a lost connection.
41+
# You can now set reconnect = true for your MySQL connections in database.yml to get this
42+
# behavior from a Rails application.
43+
#
44+
# reconnect: true
45+
46+
# List of databases for each environment. It is required key.
47+
#
48+
database_names:
49+
<<: *database_names
50+
51+
# Active Record database connections are managed by ActiveRecord::ConnectionAdapters::ConnectionPool
52+
# which ensures that a connection pool synchronizes the amount of thread access to a limited number
53+
# of database connections. This limit defaults to 5 and can be configured in database.yml.
54+
#
55+
# pool: 5
56+
57+
# Database credentials
58+
#
59+
username: root
60+
password:
61+
62+
# Connection Preference
63+
#
64+
# socket: /tmp/mysql.sock
65+
# host: localhost
66+
# port: 3306
67+
68+
###############################################################################
69+
70+
mongoid.yml:
71+
...
72+
```
73+
74+
Example of database.yml generator
75+
76+
```ruby
77+
module ConfigGenerator
78+
class DatabaseYml < Base
79+
80+
protected
81+
82+
def required_keys
83+
%w(database_names username)
84+
end
85+
86+
def default_options
87+
{
88+
'adapter' => 'mysql2',
89+
'encoding' => 'utf8'
90+
}
91+
end
92+
93+
def config_file
94+
'database.yml'
95+
end
96+
97+
def database_names
98+
@database_names ||= config_section.delete('database_names') || {}
99+
end
100+
101+
def error_messages
102+
super.merge(
103+
missing_db_for_environments: ->(value) { "Provide DB name for environments: #{value.map { |k| "'#{k}'" }.join(', ')}." }
104+
)
105+
end
106+
107+
def validate_config!
108+
super
109+
110+
return if database_names.empty?
111+
112+
missing_db_for_environments = []
113+
environments.each do |environment|
114+
next if database_names[environment].present?
115+
missing_db_for_environments << environment
116+
end
117+
118+
if missing_db_for_environments.present?
119+
@errors[:missing_db_for_environments] = missing_db_for_environments
120+
end
121+
end
122+
123+
def file_contents
124+
environments.each_with_object({}) do |environment, result|
125+
options = config_section.merge('database' => database_names[environment])
126+
result[environment] = default_options.merge(options)
127+
end.to_yaml
128+
end
129+
end
130+
end
131+
```
132+
133+
and just execute after
134+
135+
```ruby
136+
ConfigGenerator::DatabaseYml.new(Rails.root.to_s).generate_file
137+
```
26138

27139
## Development
28140

@@ -32,7 +144,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32144

33145
## Contributing
34146

35-
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/config_generator. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
147+
Bug reports and pull requests are welcome on GitHub at https://github.com/dimianstudio/config_generator. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
36148

37149

38150
## License

config_generator.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
99
spec.authors = ["Dmytro Myrhorodskyi"]
1010
spec.email = ["dimianstudio@gmail.com"]
1111

12-
spec.summary = %q{Simple generator for config files}
12+
spec.summary = %q{Simple generator for Rails config files}
1313
spec.license = "MIT"
1414

1515
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or

lib/config_generator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module ConfigGenerator
2626
class Base
2727
def initialize(project_path)
2828
@project_path = project_path
29-
@app_config = YAML.load(File.read("#{@project_path}/app_config.yml"))
29+
@app_config = YAML.load(File.read("#{@project_path}/config/application.yml"))
3030
@errors = {}
3131
end
3232

0 commit comments

Comments
 (0)