persey 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1105cc74f8880197636a93ea4c1990547c8cc85a
4
- data.tar.gz: 9dbd7aded17e89ebf3b4c0e36f0ab5773eed5925
3
+ metadata.gz: 64a5c154b43d81e9f986339f73a9f85cc861b059
4
+ data.tar.gz: 0c5d8a0d96946015c87777d12efd69fa49aa9921
5
5
  SHA512:
6
- metadata.gz: 32c3ee1921e3a4559b4ea28c41cd833bdc0f49d4ed6c0cc40f2b44d644764c1362d059fb1d710d18278e867eb53a281cd8bc65d09c24155b17669aca95d88d99
7
- data.tar.gz: 9aaab88972ebc844865da7426f2407a66bdfd562bca82ae07e615bff8f044d276320dbfbad06244dcac29c6c3dc45252b872c6ad337a0a0aa1ea72416497d55b
6
+ metadata.gz: eb9d2a412b8f013c8982a6984b53175b97c034b9081dbef94ff7da76b31ec8724783f1ddde0078d4e97253d7d08e15d402775fba6ad8ee406f554869d211d27e
7
+ data.tar.gz: 636a151f73fd8710e75b19eafb2afcec3ce83096e0c7071649c3b4760c517c31d43e556e9c744efa9eeb645242845748e2cd188ff0ee8a6db929d63ec2b85d21
@@ -1,6 +1,6 @@
1
1
  language: ruby
2
2
  rvm:
3
+ - 1.9.3
3
4
  - 2.0.0
4
5
  - 2.1.0
5
- - jruby
6
6
  script: "bundle exec rake test"
data/README.md CHANGED
@@ -7,37 +7,92 @@ Persey help you easily manage the configuration, depending on the environment.
7
7
  The main objective is to provide opportunities to reuse the
8
8
  configuration provided by the project, as the default configuration.
9
9
 
10
+ ## Problem
11
+
12
+ For the occurrence of the gem was a few prerequisites.
13
+
14
+ * Work on opensource projects that support the relevance of problem configurations, changing the appearance of new versions and functionality.
15
+ * Use in the project configuration, diversity in different files, and the inability to unite all in one configuration
16
+ * Desire to use sensitive data as easily as those that can be safely stored in the repository.
17
+ * Sometimes configuration happens in a variety of formats: yaml, json, ini
18
+
19
+ I do not want to engage in writing parsers, I want to work fine :)
20
+
21
+ This solution allows to **accumulate** different configs in one, with the **possibility of reusability** of configuration options and **simple override**. It uses an **intuitive DSL**.
22
+
10
23
  ## Installing
11
24
 
12
25
  Add this to your `Gemfile`:
13
26
 
14
- gem "persey"
27
+ ``` ruby
28
+ gem "persey", '>= 0.0.6'
29
+ ```
30
+
31
+ Generate default config file
32
+
33
+ ``` bash
34
+ $ rails g persey:install
35
+ ```
15
36
 
16
37
  ## Examples
17
38
 
18
39
  ### Definition
19
40
 
20
41
  ``` ruby
21
- redis_config = File.join(Rails.root, 'config', redis.yml.example)
22
- project_config = File.join(Rails.root, 'config', project.yml.example)
23
- awesome_gem_config = File.join(Rails.root, 'config', awesome_gem_config.yml)
42
+ # Rails.root are not initialized here
43
+ app_path = File.expand_path('../../', __FILE__)
44
+
45
+ # your redis config
46
+ redis_config = File.join(app_path, 'config', 'redis.yml.example')
47
+
48
+ # your project config
49
+ # as example - it's default options from opensource
50
+ # you don't want redeclare then with copy default file
51
+ project_config = File.join(app_path, 'config', 'project.yml.example')
52
+
53
+ # some different environment specifed configuration options in separate config
54
+ project_env_config = File.join(app_path, 'config', "project.#{Rails.env}.yml")
55
+
56
+ # config for awesome gem
57
+ awesome_gem_config = File.join(app_path, 'config', 'awesome_gem_config.yml')
58
+
59
+ # config with secret keys
60
+ # you don't want store this config in repository and copy to secret folder on host machine
24
61
  my_secret_key_config = '/home/user/secret/keys.yml'
25
62
 
26
- Persey.init :development do # set current environment
27
- sourse :yaml, redis_config, :redis # set specific namespace for settings
28
- project :yaml, project_config
29
- project :yaml, awesome_gem_config
30
- project :yaml, my_secret_key_config
63
+ # We support not only YAML
64
+ # Also JSON
65
+ my_node_js_config = '/rest/u/apps/node/config.json'
66
+ # And TOML
67
+ my_berkshelf_config = File.join(app_path, 'provisioning', '.berkshelf')
68
+ # And INI
69
+ my_ini_config = File.join(app_path, 'provisioning', 'php.ini') # lol
70
+
71
+ # Persey.init ENV["environment"] do # set current environment
72
+ Persey.init Rails.env do # set current environment
73
+ sourse :yaml, redis_config, :redis # set specific namespace for settings (mount config in :redis key)
74
+ source :yaml, project_config # if project config and project_env_config have some options keys
75
+ source :yaml, project_env_config # last declared keys overwite before declared
76
+ source :yaml, awesome_gem_config, :awesome_gem # it's good to mount unknown configs to special :namespace
77
+ source :yaml, my_secret_key_config, :secret # no comments. It's secret!
78
+ source :json, my_node_js_config, :node_js_namespace
79
+ source :toml, my_berkshelf_config, :berkshelf_namespace
80
+ source :ini, my_ini_config, :ini_namespace
31
81
 
32
82
  env :production do
33
83
  site_name 'Example'
34
84
  web do
35
- domain 'example.com'
36
- protocol 'https'
37
- port 80
85
+ # domain 'example.com' # domain described in project_env_config
86
+ # you can use them, or overwirite here
87
+ protocol 'https' # we overwrite prolocol option here
88
+ # by default was 'http', but we need some little security
89
+ port 12345 # more, more security!
90
+ # and now we use configs for our options, which are not declared in any config
38
91
  uri -> { "#{protocol}://#{domain}:#{port}" }
39
92
  end
40
- site_uri -> { web.uri }
93
+
94
+ site_uri -> { web.uri } # we can re-re-use different options
95
+
41
96
  email do
42
97
  pop do
43
98
  address 'pop.example.com'
@@ -63,34 +118,84 @@ Persey.init :development do # set current environment
63
118
  end
64
119
  end
65
120
  end
121
+ ```
122
+ If you generate Persey config with run `rails g persey:install` in your `config/application.rb` were added strings to run config initialization. If you do not run installer, you can specify of run config manually. For example:
123
+
124
+ in your `config/application.rb`
125
+
126
+ ``` ruby
127
+ #...
128
+ # We require gem here
129
+ require "persey"
130
+ require File.expand_path('../config', __FILE__)
131
+
132
+ module AppName
133
+ # If you don't want use configs with call Persey.config
134
+ # you can do something like it:
135
+ def self.config
136
+ Persey.config
137
+ end
138
+
139
+ class Application < Rails::Application
140
+ # ...
141
+ end
142
+ end
143
+
66
144
  ```
67
145
 
68
146
  ### Usage
69
147
 
70
- config = Persey.config
148
+ ``` ruby
149
+ config = Persey.config
150
+
151
+ config.site_name # => 'Example'
152
+ config.web.uri # => 'https://example.com:80'
153
+ config.site_uri # => 'https://example.com:80'
154
+ config.email.pop.port # => 110
71
155
 
72
- config.site_name # => 'Example'
73
- config.web.uri # => 'https://example.com:80'
74
- config.site_uri # => 'https://example.com:80'
75
- config.email.pop.port # => 110
156
+ AppName.config.site_name # => 'Example'
157
+ ```
76
158
 
77
159
  ### Rails
78
160
 
79
- define your config in `lib/config.rb`
161
+ define your config in `config/config.rb`
80
162
 
81
- Persey.init Rails.env do
82
- # settings
83
- end
163
+ ``` ruby
164
+ Persey.init Rails.env do
165
+ # settings
166
+ end
167
+ ```
84
168
 
85
169
  reload
86
170
 
87
- # config/environments/development.rb
88
- ActionDispatch::Reloader.to_prepare do
89
- load Rails.root.join('lib/config.rb')
90
- end
171
+ ``` ruby
172
+ # config/environments/development.rb
173
+ ActionDispatch::Reloader.to_prepare do
174
+ load Rails.root.join('config/config.rb')
175
+ end
176
+ ```
177
+
178
+ ## Supported config formats
179
+
180
+ * YAML
181
+ * JSON
182
+ * TOML
183
+ * INI
91
184
 
92
185
  ## Similar
93
186
 
94
187
  * https://github.com/kaize/configus (this gem based on configus)
95
188
  * https://github.com/markbates/configatron
96
189
  * https://github.com/railsjedi/rails_config
190
+
191
+ ## Contributing
192
+
193
+ 1. Fork it
194
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
195
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
196
+ 6. Push to the branch (`git push origin my-new-feature`)
197
+ 7. Create new Pull Request
198
+
199
+ ## Another help
200
+
201
+ You can give me feedback with issue.
@@ -0,0 +1,34 @@
1
+ module Persey
2
+ module Generators
3
+ class InstallGenerator < ::Rails::Generators::Base
4
+ source_root File.expand_path("../templates", __FILE__)
5
+
6
+ def create_configuration
7
+ copy_file("config.rb", "config/config.rb")
8
+ inject_into_file 'config/application.rb', before: "module #{Rails.application.class.parent_name}" do
9
+ <<-'RUBY'
10
+ require "persey"
11
+ require File.expand_path('../config', __FILE__)
12
+
13
+ RUBY
14
+ end
15
+ end
16
+
17
+ def ignore_configuration
18
+ if File.exists?("config/config.rb")
19
+ append_to_file("config/config.rb") do
20
+ <<-EOF.strip_heredoc
21
+
22
+ # Define method in #{Rails.application.class.parent_name}
23
+ module #{Rails.application.class.parent_name}
24
+ def self.config
25
+ Persey.config
26
+ end
27
+ end
28
+ EOF
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,31 @@
1
+ # Rails.root are not initialized here
2
+ app_path = File.expand_path('../../', __FILE__)
3
+
4
+ # config_path = File.join(app_path, 'config', 'awesome_config.yml.example')
5
+
6
+ Persey.init Rails.env do # set current environment
7
+ # source :yaml, config_path, :mount_namespace
8
+ # source :json, config_path, :mount_namespace
9
+ # source :toml, config_path, :mount_namespace
10
+ # source :ini, config_path, :mount_namespace
11
+
12
+ env :default do
13
+ # declare here some config option for all environments
14
+ end
15
+
16
+ env :production, parent: :default do
17
+ # redeclare here some specific keys for production environment
18
+ end
19
+
20
+ env :development, parent: :production do
21
+ # redeclare here some specific keys for development environment
22
+ end
23
+
24
+ env :staging, parent: :production do
25
+ # redeclare here some specific keys for staging environment
26
+ end
27
+
28
+ env :test, parent: :development do
29
+ # redeclare here some specific keys for test environment
30
+ end
31
+ end
@@ -2,7 +2,11 @@ require "persey/version"
2
2
  require "persey/builder"
3
3
  require "persey/inspector"
4
4
  require "persey/loader"
5
+ require "persey/adapters/base"
5
6
  require "persey/adapters/yaml"
7
+ require "persey/adapters/json"
8
+ require "persey/adapters/toml"
9
+ require "persey/adapters/ini"
6
10
  require "persey/configus_patch"
7
11
 
8
12
  module Persey
@@ -0,0 +1,20 @@
1
+ module Persey
2
+ module Adapters
3
+ class Base
4
+ class << self
5
+ def load(file, env)
6
+ raise NotImplementedError
7
+ end
8
+
9
+ def symbolize_keys(hash)
10
+ hash.inject({}){|res, (k, v)|
11
+ n_k = k.is_a?(String) ? k.to_sym : k
12
+ n_v = v.is_a?(Hash) ? symbolize_keys(v) : v
13
+ res[n_k] = n_v
14
+ res
15
+ }
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,18 @@
1
+ require 'inifile'
2
+
3
+ module Persey
4
+ module Adapters
5
+ class Ini < Persey::Adapters::Base
6
+ class << self
7
+ def load(file, env)
8
+ begin
9
+ raw_hash = IniFile.load(file).to_h
10
+ symbolize_keys(raw_hash)
11
+ rescue
12
+ puts "FATAL: Error while process config from file '#{file}'"
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -1,18 +1,20 @@
1
+ require 'yajl'
2
+
1
3
  module Persey
2
4
  module Adapters
3
- class Json
4
-
5
+ class Json < Persey::Adapters::Base
5
6
  class << self
6
7
  def load(file, env)
7
8
  begin
8
- raw_hash = YAML.load_file(file)
9
+ json = File.new(file, 'r')
10
+ parser = Yajl::Parser.new
11
+ raw_hash = parser.parse(json)
9
12
  symbolize_keys(raw_hash)
10
13
  rescue
11
- binding.pry
14
+ puts "FATAL: Error while process config from file '#{file}'"
12
15
  end
13
16
  end
14
17
  end
15
-
16
18
  end
17
19
  end
18
20
  end
@@ -0,0 +1,18 @@
1
+ require 'toml'
2
+
3
+ module Persey
4
+ module Adapters
5
+ class Toml < Persey::Adapters::Base
6
+ class << self
7
+ def load(file, env)
8
+ begin
9
+ raw_hash = TOML.load_file(file)
10
+ symbolize_keys(raw_hash)
11
+ rescue
12
+ puts "FATAL: Error while process config from file '#{file}'"
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -2,27 +2,16 @@ require 'yaml'
2
2
 
3
3
  module Persey
4
4
  module Adapters
5
- class Yaml
5
+ class Yaml < Persey::Adapters::Base
6
6
  class << self
7
7
  def load(file, env)
8
8
  begin
9
9
  raw_hash = YAML.load_file(file)
10
10
  symbolize_keys(raw_hash)
11
11
  rescue
12
- binding.pry
12
+ puts "FATAL: Error while process config from file '#{file}'"
13
13
  end
14
14
  end
15
-
16
- private
17
-
18
- def symbolize_keys(hash)
19
- hash.inject({}){|res, (k, v)|
20
- n_k = k.is_a?(String) ? k.to_sym : k
21
- n_v = v.is_a?(Hash) ? symbolize_keys(v) : v
22
- res[n_k] = n_v
23
- res
24
- }
25
- end
26
15
  end
27
16
  end
28
17
  end
@@ -1,3 +1,3 @@
1
1
  module Persey
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
@@ -20,4 +20,7 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_runtime_dependency "activesupport"
22
22
  spec.add_runtime_dependency 'configus', '>= 0.0.5'
23
+ spec.add_runtime_dependency 'yajl-ruby'
24
+ spec.add_runtime_dependency "toml", "~> 0.1.0"
25
+ spec.add_runtime_dependency "inifile"
23
26
  end
@@ -0,0 +1,11 @@
1
+ [section1]
2
+ ; some comment on section1
3
+ var1 = foo
4
+ var2 = doodle
5
+ var3 = multiline values \
6
+ are also possible
7
+
8
+ [section2]
9
+ # another comment
10
+ var1 = baz
11
+ var2 = shoodle
@@ -0,0 +1,26 @@
1
+ {
2
+ "header":{
3
+ "modified":"1 April 2001",
4
+ "dc:author": "John Doe"
5
+ },
6
+ "logger_root":{
7
+ "level":"NOTSET",
8
+ "handlers":"hand01"
9
+ },
10
+ "logger_parser":{
11
+ "level":"DEBUG",
12
+ "handlers":"hand01",
13
+ "propagate":"1",
14
+ "qualname":"compiler.parser"
15
+ },
16
+ "owner":{
17
+ "name":"John Doe",
18
+ "organization":"Acme Widgets Inc."
19
+ },
20
+ "database":{
21
+ "server":"192.0.2.62",
22
+ "_comment_server":"use IP address in case network name resolution is not working",
23
+ "port":"143",
24
+ "file":"payroll.dat"
25
+ }
26
+ }
@@ -0,0 +1,35 @@
1
+ # This is a TOML document. Boom.
2
+
3
+ title = "TOML Example"
4
+
5
+ [owner]
6
+ name = "Tom Preston-Werner"
7
+ organization = "GitHub"
8
+ bio = "GitHub Cofounder & CEO\nLikes tater tots and beer."
9
+ dob = 1979-05-27T07:32:00Z # First class dates? Why not?
10
+
11
+ [database]
12
+ server = "192.168.1.1"
13
+ ports = [ 8001, 8001, 8002 ]
14
+ connection_max = 5000
15
+ enabled = true
16
+
17
+ [servers]
18
+
19
+ # You can indent as you please. Tabs or spaces. TOML don't care.
20
+ [servers.alpha]
21
+ ip = "10.0.0.1"
22
+ dc = "eqdc10"
23
+
24
+ [servers.beta]
25
+ ip = "10.0.0.2"
26
+ dc = "eqdc10"
27
+
28
+ [clients]
29
+ data = [ ["gamma", "delta"], [1, 2] ]
30
+
31
+ # Line breaks are OK when inside arrays
32
+ hosts = [
33
+ "alpha",
34
+ "omega"
35
+ ]
@@ -4,10 +4,16 @@ class PerseyTest < TestCase
4
4
  def setup
5
5
  plain_config = File.join(fixtures_path, 'yaml_config.yml')
6
6
  env_config = File.join(fixtures_path, 'yaml_config_with_envs.yml')
7
+ plain_json_config = File.join(fixtures_path, 'json_config.json')
8
+ plain_toml_config = File.join(fixtures_path, 'toml_config.toml')
9
+ plain_ini_config = File.join(fixtures_path, 'ini_config.ini')
7
10
 
8
11
  Persey.init :production do
9
12
  source :yaml, plain_config
10
- source :yaml, env_config, :namespace
13
+ source :yaml, env_config, :namespace
14
+ source :json, plain_json_config, :json_config
15
+ source :toml, plain_toml_config, :toml_config
16
+ source :ini, plain_ini_config, :ini_config
11
17
 
12
18
  env :production do
13
19
  option do
@@ -27,5 +33,8 @@ class PerseyTest < TestCase
27
33
  assert { @config.option.first == "first value" }
28
34
  assert { @config.namespace.another_key == "another key value" }
29
35
  assert { @config.key == "key value" }
36
+ assert { @config.json_config.owner.name == "John Doe" }
37
+ assert { @config.toml_config.owner.name == "Tom Preston-Werner" }
38
+ assert { @config.ini_config.section1.var1 == "foo" }
30
39
  end
31
40
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: persey
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrey Kumanyaev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-26 00:00:00.000000000 Z
11
+ date: 2014-01-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -38,6 +38,48 @@ dependencies:
38
38
  - - '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: 0.0.5
41
+ - !ruby/object:Gem::Dependency
42
+ name: yajl-ruby
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: toml
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.1.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 0.1.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: inifile
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
41
83
  description: Persey helps you easily load configs from different files and use them
42
84
  from one variable. Based on Configus.
43
85
  email:
@@ -52,8 +94,13 @@ files:
52
94
  - LICENSE.txt
53
95
  - README.md
54
96
  - Rakefile
97
+ - lib/generators/persey/install/install_generator.rb
98
+ - lib/generators/persey/install/templates/config.rb
55
99
  - lib/persey.rb
100
+ - lib/persey/adapters/base.rb
101
+ - lib/persey/adapters/ini.rb
56
102
  - lib/persey/adapters/json.rb
103
+ - lib/persey/adapters/toml.rb
57
104
  - lib/persey/adapters/yaml.rb
58
105
  - lib/persey/builder.rb
59
106
  - lib/persey/configus_patch.rb
@@ -61,6 +108,9 @@ files:
61
108
  - lib/persey/loader.rb
62
109
  - lib/persey/version.rb
63
110
  - persey.gemspec
111
+ - test/fixtures/ini_config.ini
112
+ - test/fixtures/json_config.json
113
+ - test/fixtures/toml_config.toml
64
114
  - test/fixtures/yaml_config.yml
65
115
  - test/fixtures/yaml_config_with_envs.yml
66
116
  - test/lib/persey_test.rb
@@ -90,6 +140,9 @@ signing_key:
90
140
  specification_version: 4
91
141
  summary: Helps you easily manage environment specific settings
92
142
  test_files:
143
+ - test/fixtures/ini_config.ini
144
+ - test/fixtures/json_config.json
145
+ - test/fixtures/toml_config.toml
93
146
  - test/fixtures/yaml_config.yml
94
147
  - test/fixtures/yaml_config_with_envs.yml
95
148
  - test/lib/persey_test.rb