persey 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a197cddff1bf86a95882b1e4b660567231d373f1
4
+ data.tar.gz: 59991233039e755972297e0f1a6236a07423c158
5
+ SHA512:
6
+ metadata.gz: 50ad4f17a8e692de2019aa7cd409892f7ee5d434643605dab9efca89c08b61f6ae76be84548143a163c0c0e5f18a247b5c228af7becae3ca6bd934b7ccffadb6
7
+ data.tar.gz: 31384243cb7765b28400fe5e96952143fd2d3d1a372e46a9ea268d4bd7eb92ae8ef79a8930a52882c870c1b92552fdb3350ebbd4b00abe97148a56723844cd82
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ gem 'minitest'
6
+ gem 'wrong', github: 'av-ast/wrong'
7
+ gem 'active_support'
8
+ gem 'configus', '0.0.5'
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Andrey Kumanyaev
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,95 @@
1
+ # Persey
2
+
3
+ ## Summary
4
+
5
+ Perseus help you easily manage the configuration, depending on the environment.
6
+ The main objective is to provide opportunities to reuse the
7
+ configuration provided by the project, as the default configuration.
8
+
9
+ ## Installing
10
+
11
+ Add this to your `Gemfile`:
12
+
13
+ gem "persey"
14
+
15
+ ## Examples
16
+
17
+ ### Definition
18
+
19
+ ``` ruby
20
+ redis_config = File.join(Rails.root, 'config', redis.yml.example)
21
+ project_config = File.join(Rails.root, 'config', project.yml.example)
22
+ awesome_gem_config = File.join(Rails.root, 'config', awesome_gem_config.yml)
23
+ my_secret_key_config = '/home/user/secret/keys.yml'
24
+
25
+ Persey.init :development do # set current environment
26
+ sourse :yaml, redis_config, :redis # set specific namespace for settings
27
+ project :yaml, project_config
28
+ project :yaml, awesome_gem_config
29
+ project :yaml, my_secret_key_config
30
+
31
+ env :production do
32
+ site_name 'Example'
33
+ web do
34
+ domain 'example.com'
35
+ protocol 'https'
36
+ port 80
37
+ uri -> { "#{protocol}://#{domain}:#{port}" }
38
+ end
39
+ site_uri -> { web.uri }
40
+ email do
41
+ pop do
42
+ address 'pop.example.com'
43
+ port 110
44
+ end
45
+ smtp do
46
+ address 'smtp.example.com'
47
+ port 25
48
+ end
49
+ end
50
+ end
51
+
52
+ env :development, :parent => :production do
53
+ web do
54
+ domain 'localhost'
55
+ protocol 'http'
56
+ port 9292
57
+ end
58
+ email do
59
+ smtp do
60
+ address 'smpt.text.example.com'
61
+ end
62
+ end
63
+ end
64
+ end
65
+ ```
66
+
67
+ ### Usage
68
+
69
+ config = Persey.config
70
+
71
+ config.site_name # => 'Example'
72
+ config.web.uri # => 'https://example.com:80'
73
+ config.site_uri # => 'https://example.com:80'
74
+ config.email.pop.port # => 110
75
+
76
+ ### Rails
77
+
78
+ define your config in `lib/config.rb`
79
+
80
+ Persey.init Rails.env do
81
+ # settings
82
+ end
83
+
84
+ reload
85
+
86
+ # config/environments/development.rb
87
+ ActionDispatch::Reloader.to_prepare do
88
+ load Rails.root.join('lib/config.rb')
89
+ end
90
+
91
+ ## Similar
92
+
93
+ * https://github.com/kaize/configus (this gem based on configus)
94
+ * https://github.com/markbates/configatron
95
+ * https://github.com/railsjedi/rails_config
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << "test"
6
+ t.test_files = FileList['test/**/*_test.rb']
7
+ t.verbose = true
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,21 @@
1
+ require "persey/version"
2
+ require "persey/builder"
3
+ require "persey/inspector"
4
+ require "persey/loader"
5
+ require "persey/adapters/yaml"
6
+
7
+ module Persey
8
+ class << self
9
+ def init(environment, &block)
10
+ configs = Inspector.analize(&block)
11
+ defaults = Loader.load(configs, environment)
12
+ @config = Builder.build(environment, defaults, &block)
13
+ end
14
+
15
+ def config
16
+ raise RuntimeError, "Please, init config before usage" if @config.nil?
17
+
18
+ @config
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,18 @@
1
+ module Persey
2
+ module Adapters
3
+ class Json
4
+
5
+ class << self
6
+ def load(file, env)
7
+ begin
8
+ raw_hash = YAML.load_file(file)
9
+ symbolize_keys(raw_hash)
10
+ rescue
11
+ binding.pry
12
+ end
13
+ end
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,29 @@
1
+ require 'yaml'
2
+
3
+ module Persey
4
+ module Adapters
5
+ class Yaml
6
+ class << self
7
+ def load(file, env)
8
+ begin
9
+ raw_hash = YAML.load_file(file)
10
+ symbolize_keys(raw_hash)
11
+ rescue
12
+ binding.pry
13
+ end
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
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,64 @@
1
+ module Persey
2
+ class Builder
3
+ class << self
4
+ def build(environment, defaults, &block)
5
+ b = new(environment, defaults, block)
6
+ Configus::Config.new(b.result)
7
+ end
8
+ end
9
+
10
+ def initialize(environment, defaults, block)
11
+ @current_env = environment.to_sym
12
+ @config = defaults
13
+ @envs = {}
14
+ instance_eval(&block)
15
+ end
16
+
17
+ def result(env = nil)
18
+ e = env || @current_env
19
+ edata = @envs[e]
20
+
21
+ raise ArgumentError, "Undefined environment '#{ env }" if edata.nil?
22
+
23
+ current_config = {}
24
+ if edata[:block]
25
+ current_config = expand(edata[:block])
26
+ end
27
+
28
+ parent = edata[:options][:parent]
29
+ if parent
30
+ parent_config = result(parent)
31
+ current_config = deep_merge(parent_config, current_config)
32
+ end
33
+
34
+ current_config = @config.merge(current_config)
35
+ end
36
+
37
+ private
38
+
39
+ def env(env, options = {}, &block)
40
+ env = env.to_sym
41
+
42
+ raise ArgumentError, "Double definition of environment '#{ env }'" if @envs.has_key?(env)
43
+
44
+ @envs[env] = { options: options }
45
+ @envs[env][:block] = block if block_given?
46
+ end
47
+
48
+ def deep_merge(target, source)
49
+ source.each_pair do |k,v|
50
+ tv = target[k]
51
+ target[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? deep_merge(tv, v) : v
52
+ end
53
+ target
54
+ end
55
+
56
+ def source(*args)
57
+ # Nothing. It's Inspector method
58
+ end
59
+
60
+ def expand(block)
61
+ Configus::Proxy.generate(block)
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,26 @@
1
+ require 'active_support/inflector'
2
+
3
+ module Persey
4
+ class Inspector
5
+ class << self
6
+ def analize(&block)
7
+ @sources = []
8
+ instance_eval(&block)
9
+ @sources
10
+ end
11
+
12
+ def source(source_type, config_file, namespace = nil)
13
+ begin
14
+ klass = "persey/adapters/#{source_type}".camelize.constantize
15
+ @sources << { class: klass, file: config_file, namespace: namespace } if File.exists?(config_file)
16
+ rescue
17
+ binding.pry
18
+ end
19
+ end
20
+
21
+ def env(*args)
22
+ # Nithing. I do not want call method_missing
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,35 @@
1
+ module Persey
2
+ class Loader
3
+ attr_accessor :configs
4
+
5
+ class << self
6
+ def load(configs, env)
7
+ l = new(configs)
8
+ l.load(env)
9
+ end
10
+ end
11
+
12
+ def initialize(configs)
13
+ @configs = configs
14
+ end
15
+
16
+ def load(env)
17
+ @defaults = {}
18
+
19
+ configs.each do |pdc|
20
+ klass = pdc[:class]
21
+ raw_config = klass.load(pdc[:file], env)
22
+ env_config = raw_config[env].nil? ? raw_config : raw_config[env]
23
+
24
+ n = pdc[:namespace]
25
+ if n.nil?
26
+ @defaults.merge!(env_config)
27
+ else
28
+ @defaults[n].is_a?(Hash) ? @defaults[n].merge!(env_config) : @defaults[n] = env_config
29
+ end
30
+ end
31
+
32
+ @defaults
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module Persey
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'persey/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "persey"
8
+ spec.version = Persey::VERSION
9
+ spec.authors = ["Andrey Kumanyaev"]
10
+ spec.email = ["me@zzet.org"]
11
+ spec.summary = %q{Helps you easily manage environment specific settings}
12
+ spec.description = %q{Persey helps you easily load configs from different files and use them from one variable. Based on Configus.}
13
+ spec.homepage = "https://github.com/zzet/persey"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "pry"
24
+ end
@@ -0,0 +1,5 @@
1
+ ---
2
+ option: option value
3
+ key: key value
4
+ first:
5
+ second: foo value
@@ -0,0 +1,17 @@
1
+ ---
2
+ defaults: &defaults
3
+ another_option: value
4
+ another_key: another key value
5
+ first:
6
+ second: value
7
+ second:
8
+ another_second: another_value
9
+
10
+ production:
11
+ <<: *defaults
12
+
13
+ development:
14
+ <<: *defaults
15
+
16
+ test:
17
+ <<: *defaults
@@ -0,0 +1,31 @@
1
+ require 'test_helper'
2
+
3
+ class PerseyTest < TestCase
4
+ def setup
5
+ plain_config = File.join(fixtures_path, 'yaml_config.yml')
6
+ env_config = File.join(fixtures_path, 'yaml_config_with_envs.yml')
7
+
8
+ Persey.init :production do
9
+ source :yaml, plain_config
10
+ source :yaml, env_config, :namespace
11
+
12
+ env :production do
13
+ option do
14
+ first "first value"
15
+ second "second value"
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ def test_load_config
22
+ assert { Persey.config }
23
+ end
24
+
25
+ def test_correct_config
26
+ @config = Persey.config
27
+ assert { @config.option.first == "first value" }
28
+ assert { @config.namespace.another_key == "another key value" }
29
+ assert { @config.key == "key value" }
30
+ end
31
+ end
@@ -0,0 +1,21 @@
1
+ require "rubygems"
2
+ require 'bundler/setup'
3
+ require 'pry'
4
+
5
+ Bundler.require
6
+
7
+ require 'wrong/adapters/minitest'
8
+
9
+ PROJECT_ROOT = File.join(Dir.pwd)
10
+
11
+ Wrong.config.color
12
+
13
+ Minitest.autorun
14
+
15
+ class TestCase < Minitest::Test
16
+ include Wrong
17
+
18
+ def fixtures_path
19
+ @path ||= File.expand_path(File.join(__FILE__, "../fixtures"))
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: persey
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Andrey Kumanyaev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Persey helps you easily load configs from different files and use them
56
+ from one variable. Based on Configus.
57
+ email:
58
+ - me@zzet.org
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - .gitignore
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/persey.rb
69
+ - lib/persey/adapters/json.rb
70
+ - lib/persey/adapters/yaml.rb
71
+ - lib/persey/builder.rb
72
+ - lib/persey/inspector.rb
73
+ - lib/persey/loader.rb
74
+ - lib/persey/version.rb
75
+ - persey.gemspec
76
+ - test/fixtures/yaml_config.yml
77
+ - test/fixtures/yaml_config_with_envs.yml
78
+ - test/lib/persey_test.rb
79
+ - test/test_helper.rb
80
+ homepage: https://github.com/zzet/persey
81
+ licenses:
82
+ - MIT
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.0.3
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Helps you easily manage environment specific settings
104
+ test_files:
105
+ - test/fixtures/yaml_config.yml
106
+ - test/fixtures/yaml_config_with_envs.yml
107
+ - test/lib/persey_test.rb
108
+ - test/test_helper.rb