anyway_config 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c681a4243d4852f2ef8383624c6d2a8f1578b3a0
4
- data.tar.gz: 4719dd86c4d8f21e55712861be1fd9c7e2691b57
3
+ metadata.gz: 40e4e0dd7f4d5e1fb19d83de0755be5f0054ac2f
4
+ data.tar.gz: 555132629ca863a88d84ba53ee9cfd6ce02a2fad
5
5
  SHA512:
6
- metadata.gz: 592cc0e754e5cd9568102b2bcd8e142c86c3b769bd262f0dfb3afc24ed853ca2ce70f854cf9e19fe7f7f980cb1bd761c68997b658f58cbb20c8f6794430b3777
7
- data.tar.gz: 26524cbd6a105d6d59a5775e06b8324f665d2428df3a3129531a63b91894c97c93362997154c60cbdf9c119467600600f9fdd2b21b41ec243a0ef79470a8c5eb
6
+ metadata.gz: 31e46f769ef2a2e550f195232c0fe76e127fc567d56de1c6a1d14e6c0f77e99a05c6f956746ca96a0647ad3edfd74e1d0f5e2301ab1c4f5af9dcc8ce119c939f
7
+ data.tar.gz: f19f0adfe0d2827c14c4ff83b718ae06f13a2614103dcc4e86cc5cfc5de91dbf4be5f36d885ea57f5cb1a862eca04a60f6e8eed8297eda368bfe2103a368eeb0
data/.travis.yml CHANGED
@@ -1,20 +1,31 @@
1
1
  language: ruby
2
2
  cache: bundler
3
3
  rvm:
4
- - 2.0.0
5
- - 2.1
4
+ - 2.2
6
5
 
7
- gemfile:
8
- - Gemfile
6
+ notifications:
7
+ email: false
9
8
 
10
9
  matrix:
10
+ allow_failures:
11
+ - rvm: :jruby
12
+
11
13
  include:
14
+ - rvm: 1.9.3
15
+ gemfile: gemfiles/rails32.gemfile
16
+
17
+ - rvm: jruby
18
+ gemfile: gemfiles/rails32.gemfile
19
+
12
20
  - rvm: 2.0.0
13
21
  gemfile: gemfiles/rails40.gemfile
14
22
 
15
23
  - rvm: 2.1
16
24
  gemfile: gemfiles/rails40.gemfile
17
25
 
26
+ - rvm: jruby
27
+ gemfile: gemfiles/rails41.gemfile
28
+
18
29
  - rvm: 2.1
19
30
  gemfile: gemfiles/rails41.gemfile
20
31
 
data/Gemfile CHANGED
@@ -1,4 +1,7 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in anyway_config.gemspec
4
+ gem 'rails', '~> 4.2'
5
+ gem 'pry-byebug'
6
+ gem 'sqlite3'
4
7
  gemspec
data/README.md CHANGED
@@ -2,11 +2,13 @@
2
2
 
3
3
  # Anyway Config
4
4
 
5
- Rails plugin configuration using any source: YAML, _secrets_, environment.
5
+ Rails plugin/application configuration using any source: YAML, _secrets_, environment.
6
6
 
7
- Requires Rails 4.
8
7
 
9
- ## Installation
8
+ Apps using Anyway Config:
9
+ - [influxer](https://github.com/palkan/influxer).
10
+
11
+ ## Using with Gem
10
12
 
11
13
  Configure your gemspec
12
14
 
@@ -26,13 +28,12 @@ Or install it yourself as:
26
28
 
27
29
  $ gem install anyway_config
28
30
 
29
- ## Usage
30
-
31
- ### Basic
31
+ ### Usage
32
32
 
33
33
  Create configuration class:
34
34
 
35
35
  ```ruby
36
+ require 'anyway'
36
37
  module MyCoolGem
37
38
  class Config < Anyway::Config
38
39
  attr_config user: 'root', password: 'root', host: 'localhost'
@@ -61,7 +62,44 @@ module MyCoolGem
61
62
  end
62
63
  ```
63
64
 
64
- ### How to set env vars
65
+ ### Config clear and reload
66
+
67
+ You can use `clear` and `reload` functions on your config (which do exactly what they state).
68
+
69
+
70
+ ## Using with Rails app
71
+
72
+ In your Gemfile
73
+
74
+ ```ruby
75
+ require 'anyway_config', "~>0.2"
76
+ ```
77
+
78
+ In your code
79
+
80
+ ```ruby
81
+
82
+ config = Anyway::Config.for(:my_app) # load data from config/my_app.yml, secrets.my_app, ENV["MYAPP_*"]
83
+
84
+ ```
85
+
86
+ ## `Rails.application.config_for` vs `Anyway::Config.for`
87
+
88
+ Rails 4.2 introduces new feature: `Rails.application.config_for`. It looks very similar to
89
+ `Anyway::Config.for`, but there are some differences:
90
+
91
+ | Feature | Rails | Anyway |
92
+ | ------------- |:-------------:| -----:|
93
+ | load data from `config/app.yml` | yes | yes |
94
+ | load data from `secrets` | no | yes |
95
+ | load data from environment | no | yes |
96
+ | return Hash with indifferent access | no | yes |
97
+ | support ERB within `config/app.yml` | yes | no |
98
+ | raise errors if file doesn't exist | yes | no |
99
+
100
+ But the main advantage of Anyway::Config is that it's supported in Rails >= 3.2, Ruby >= 1.9.3.
101
+
102
+ ## How to set env vars
65
103
 
66
104
  Environmental variables for your config should start with your module name (or config name if any), uppercased and underscore-free.
67
105
 
@@ -71,10 +109,6 @@ For example, if your module is called "MyCoolGem" then your env var "MYCOOLGEM_P
71
109
  For example, "MYCOOLGEM_OPTIONS__VERBOSE" is transformed to `config.options.verbose`.
72
110
 
73
111
 
74
- ### Config clear and reload
75
-
76
- You can use `clear` and `reload` functions on your config (which do exactly what they state).
77
-
78
112
  ## Contributing
79
113
 
80
114
  1. Fork it
@@ -9,18 +9,15 @@ Gem::Specification.new do |s|
9
9
  s.authors = ["Vlad Dem"]
10
10
  s.email = ["dementiev.vm@gmail.com"]
11
11
  s.homepage = "http://github.com/palkan/anyway_config"
12
- s.summary = "Configuration for Rails plugins"
13
- s.description = "Configuration for Rails plugins"
12
+ s.summary = "Configuration for Rails plugins and applications"
13
+ s.description = "Configuration for Rails plugins and applications"
14
14
  s.license = "MIT"
15
15
 
16
16
  s.files = `git ls-files`.split($/)
17
17
  s.require_paths = ["lib"]
18
-
19
- s.add_dependency 'rails', "~>4"
18
+ s.required_ruby_version = '>= 1.9.3'
20
19
 
21
- s.add_development_dependency 'sqlite3'
22
20
  s.add_development_dependency 'pry'
23
- s.add_development_dependency 'pry-byebug'
24
21
  s.add_development_dependency "simplecov", ">= 0.3.8"
25
22
  s.add_development_dependency "rspec", "~> 3.0.0"
26
23
  s.add_development_dependency "rspec-rails", "~> 3.0.0"
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'sqlite3', platform: :mri
4
+ gem 'activerecord-jdbcsqlite3-adapter', platform: :jruby
5
+ gem 'rails', '~> 3.2'
6
+
7
+ gemspec path: '..'
data/lib/anyway/config.rb CHANGED
@@ -3,7 +3,7 @@ module Anyway
3
3
  class << self
4
4
  attr_reader :defaults, :config_attributes
5
5
 
6
- def attr_config(*args,**hargs)
6
+ def attr_config(*args,hargs)
7
7
  @defaults = hargs.dup.with_indifferent_access
8
8
  @config_attributes = args+hargs.keys
9
9
  attr_accessor *@config_attributes
@@ -14,14 +14,25 @@ module Anyway
14
14
  @config_name ||= extract_name
15
15
  end
16
16
 
17
+ # Load config as Hash by any name
18
+ #
19
+ # Example:
20
+ #
21
+ # my_config = Anyway::Config.for(:my_app)
22
+ # # will load data from config/my_app.yml, secrets.my_app, ENV["MY_APP_*"]
23
+ def for(name)
24
+ self.new(name,false).load_from_sources
25
+ end
26
+
17
27
  private
18
28
  def extract_name
19
29
  self.name[/^(\w+)/].underscore
20
30
  end
21
31
  end
22
32
 
23
- def initialize
24
- load
33
+ def initialize(config_name=nil, do_load=true)
34
+ @config_name = config_name || self.class.config_name
35
+ load if do_load
25
36
  end
26
37
 
27
38
  def reload
@@ -38,27 +49,28 @@ module Anyway
38
49
  end
39
50
 
40
51
  def load
41
- # first, copy defaults
42
- config = self.class.defaults.deep_dup
43
- config_name = self.class.config_name
52
+ config = load_from_sources self.class.defaults.deep_dup
53
+ config.each do |key, val|
54
+ self.send("#{key}=",val)
55
+ end
56
+ end
44
57
 
58
+ def load_from_sources(config={}.with_indifferent_access)
45
59
  # then load from YAML if any
46
- config_path = Rails.root.join("config","#{config_name}.yml")
60
+ config_path = Rails.root.join("config","#{@config_name}.yml")
47
61
  if File.file? config_path
62
+ require 'yaml'
48
63
  config.deep_merge! (YAML.load_file(config_path)[Rails.env] || {})
49
64
  end
50
65
 
51
66
  # then load from Rails secrets
52
- unless Rails.application.try(:secrets).nil?
53
- config.deep_merge! (Rails.application.secrets.send(config_name)||{})
67
+ if Rails.application.respond_to?(:secrets)
68
+ config.deep_merge! (Rails.application.secrets.send(@config_name)||{})
54
69
  end
55
70
 
56
71
  # and then load from env
57
- config.deep_merge! (Anyway.env.send(config_name) || {})
58
-
59
- config.each do |key, val|
60
- self.send("#{key}=",val)
61
- end
72
+ config.deep_merge! (Anyway.env.send(@config_name) || {})
73
+ config
62
74
  end
63
75
  end
64
76
  end
@@ -1,3 +1,3 @@
1
1
  module Anyway
2
- VERSION = "0.1.0"
3
- end
2
+ VERSION = "0.2.0"
3
+ end
data/spec/config_spec.rb CHANGED
@@ -29,7 +29,7 @@ describe Anyway::Config do
29
29
  expect(conf.host).to eq "test.host"
30
30
  end
31
31
 
32
- unless Rails.application.try(:secrets).nil?
32
+ if Rails.application.respond_to?(:secrets)
33
33
  it "should load config from secrets" do
34
34
  expect(conf.user[:name]).to eq "test"
35
35
  expect(conf.user[:password]).to eq "test"
@@ -96,4 +96,19 @@ describe Anyway::Config do
96
96
  expect(test_conf.test).to eq "test"
97
97
  end
98
98
  end
99
+
100
+ describe "config for name" do
101
+ after(:each) { Anyway.env.clear }
102
+ it "should load data by config name" do
103
+ ENV['MYAPP_TEST'] = '1'
104
+ ENV['MYAPP_NAME'] = 'my_app'
105
+ Anyway.env.reload
106
+ data = Anyway::Config.for(:my_app)
107
+ expect(data[:test]).to eq '1'
108
+ expect(data[:name]).to eq 'my_app'
109
+ if Rails.application.respond_to?(:secrets)
110
+ expect(data[:secret]).to eq 'my_secret'
111
+ end
112
+ end
113
+ end
99
114
  end
@@ -13,7 +13,7 @@ Dummy::Application.configure do
13
13
  config.eager_load = false
14
14
 
15
15
  # Configure static asset server for tests with Cache-Control for performance.
16
- config.serve_static_assets = true
16
+ config.serve_static_files = true
17
17
  config.static_cache_control = 'public, max-age=3600'
18
18
 
19
19
  # Show full error reports and disable caching.
@@ -19,6 +19,9 @@ test:
19
19
  user:
20
20
  name: test
21
21
  password: test
22
+ my_app:
23
+ name: 'app'
24
+ secret: 'my_secret'
22
25
 
23
26
  # Do not keep production secrets in the repository,
24
27
  # instead read values from the environment.
metadata CHANGED
@@ -1,43 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: anyway_config
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vlad Dem
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-20 00:00:00.000000000 Z
11
+ date: 2015-01-23 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: rails
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '4'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '4'
27
- - !ruby/object:Gem::Dependency
28
- name: sqlite3
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
13
  - !ruby/object:Gem::Dependency
42
14
  name: pry
43
15
  requirement: !ruby/object:Gem::Requirement
@@ -52,20 +24,6 @@ dependencies:
52
24
  - - ">="
53
25
  - !ruby/object:Gem::Version
54
26
  version: '0'
55
- - !ruby/object:Gem::Dependency
56
- name: pry-byebug
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- version: '0'
69
27
  - !ruby/object:Gem::Dependency
70
28
  name: simplecov
71
29
  requirement: !ruby/object:Gem::Requirement
@@ -108,7 +66,7 @@ dependencies:
108
66
  - - "~>"
109
67
  - !ruby/object:Gem::Version
110
68
  version: 3.0.0
111
- description: Configuration for Rails plugins
69
+ description: Configuration for Rails plugins and applications
112
70
  email:
113
71
  - dementiev.vm@gmail.com
114
72
  executables: []
@@ -122,6 +80,7 @@ files:
122
80
  - README.md
123
81
  - Rakefile
124
82
  - anyway_config.gemspec
83
+ - gemfiles/rails32.gemfile
125
84
  - gemfiles/rails40.gemfile
126
85
  - gemfiles/rails41.gemfile
127
86
  - gemfiles/rails42.gemfile
@@ -189,7 +148,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
189
148
  requirements:
190
149
  - - ">="
191
150
  - !ruby/object:Gem::Version
192
- version: '0'
151
+ version: 1.9.3
193
152
  required_rubygems_version: !ruby/object:Gem::Requirement
194
153
  requirements:
195
154
  - - ">="
@@ -200,5 +159,5 @@ rubyforge_project:
200
159
  rubygems_version: 2.2.2
201
160
  signing_key:
202
161
  specification_version: 4
203
- summary: Configuration for Rails plugins
162
+ summary: Configuration for Rails plugins and applications
204
163
  test_files: []