setty 1.0.0 → 1.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a7197876a7fd153864e854a68461bce2a3cbc14d
4
+ data.tar.gz: a0aa78b034d6bb80d830b583d1bb2d3a1845c155
5
+ SHA512:
6
+ metadata.gz: 913a5c1bb8bc607e19b90bb747454b4cddfd0a0208e06afa8ae3db01ef53c05f4efc4f30d77da8f45426a94d1e389995aa2f918252938f77a1d267ec59eddffb
7
+ data.tar.gz: 22ac1895547bf190df26458b7c969d87aa0a23448ffe542ea37b8c99200edb1b7cacb3af657346077ff2a3755c72c196c9ea1a63e5fcdbc5db21fea7e8439d96
@@ -1,5 +1,25 @@
1
1
  # Changelog
2
2
 
3
+ ## Version 1.1 (unreleased)
4
+
5
+ * Added reload method to Settings.
6
+
7
+ * Raise error when settings file exists, but is not readable.
8
+
9
+ * Added `settings_environment` option. It defaults to `Rails.env`.
10
+
11
+ Example:
12
+
13
+ # config/application.rb
14
+ config.setty.settings_environment = 'custom'
15
+
16
+ # config/settings.yml
17
+ custom:
18
+ key: "value"
19
+
20
+ development:
21
+ key: "other value"
22
+
3
23
  ## Version 1.0
4
24
 
5
- * Initial release
25
+ * Initial release.
data/README.md CHANGED
@@ -1,3 +1,4 @@
1
+ [![Gem Version](https://badge.fury.io/rb/setty.png)](http://badge.fury.io/rb/setty)
1
2
  [![Code Climate](https://codeclimate.com/github/RStankov/setty.png)](https://codeclimate.com/github/RStankov/setty)
2
3
  [![Build Status](https://secure.travis-ci.org/RStankov/setty.png)](http://travis-ci.org/RStankov/setty)
3
4
  [![Code coverage](https://coveralls.io/repos/RStankov/setty/badge.png?branch=master)](https://coveralls.io/r/RStankov/setty)
@@ -55,7 +56,7 @@ Settings.secret_token #=> "blablablabla"
55
56
  Settings.secret_token? #=> true
56
57
  Settings.ssl_only #=> false
57
58
  Settings.ssl_only? #=> false
58
- Settings.projects.minumum_photos_count #= 1
59
+ Settings.products.minumum_photos_count #=> 1
59
60
  ```
60
61
 
61
62
  You can find example of most important features and plugins - [here](https://github.com/RStankov/setty/tree/master/example).
@@ -66,6 +67,7 @@ You can find example of most important features and plugins - [here](https://git
66
67
  * Environment dependent
67
68
  * Interpolation
68
69
  * Nested settings
70
+ * Reloading settings
69
71
 
70
72
  ### Configurable
71
73
 
@@ -74,8 +76,9 @@ You can find example of most important features and plugins - [here](https://git
74
76
  module MyApp
75
77
  class Application < Rails::Application
76
78
  # ... code ... code ...
77
- config.setty.settings_object_name = 'Settings' # => Settings will be loaded in `Settings`
78
- config.setty.settings_path = 'settings' # => extracts Settings from `config/settings/*` and `config/settings.yml`
79
+ config.setty.settings_object_name = 'Settings' # => settings will be loaded in `Settings`
80
+ config.setty.settings_path = 'settings' # => extracts settings from `config/settings/*` and `config/settings.yml`
81
+ config.setty.settings_environment = Rails.env # => enviroment name (defaults to Rails.env)
79
82
  end
80
83
  end
81
84
  ```
@@ -98,12 +101,12 @@ Depending on your Rails environment:
98
101
 
99
102
  ```Ruby
100
103
  # in development
101
- Settings::Validations.require_user_to_belong_to_account #=> true
102
- Settings::Validations.require_user_to_belong_to_account? #=> true
104
+ Settings.validations.require_user_to_belong_to_account #=> true
105
+ Settings.validations.require_user_to_belong_to_account? #=> true
103
106
 
104
107
  # in test
105
- Settings::Validations.require_user_to_belong_to_account #=> false
106
- Settings::Validations.require_user_to_belong_to_account? #=> false
108
+ Settings.validations.require_user_to_belong_to_account #=> false
109
+ Settings.validations.require_user_to_belong_to_account? #=> false
107
110
  ```
108
111
 
109
112
  ### Interpolation
@@ -127,8 +130,8 @@ production:
127
130
 
128
131
  ```Ruby
129
132
  # in production
130
- Settings::S3.access_key_id #=> S3_ACCESS_KEY from `.env.production`
131
- Settings::S3.secret_access_key #=> S3_SECRET_KEY from `.env.production`
133
+ Settings.s3.access_key_id #=> S3_ACCESS_KEY from `.env.production`
134
+ Settings.s3.secret_access_key #=> S3_SECRET_KEY from `.env.production`
132
135
  ```
133
136
 
134
137
  ### Nested Settings
@@ -151,6 +154,14 @@ Settings.validations.product
151
154
  Settings.validations.category
152
155
  ```
153
156
 
157
+ ### Reloading
158
+
159
+ You can reload the current settings by calling:
160
+
161
+ ```ruby
162
+ Settings.reload
163
+ ```
164
+
154
165
  ## Contributing
155
166
 
156
167
  1. Fork it
@@ -1,4 +1,10 @@
1
1
  class ExamplesController < ApplicationController
2
2
  def index
3
3
  end
4
+
5
+ def reload
6
+ AppSettings.reload
7
+
8
+ redirect_to :back, notice: 'Settings reloaded'
9
+ end
4
10
  end
@@ -1,4 +1,7 @@
1
1
  #example
2
+ - if flash[:notice]
3
+ .alert.alert-success = flash[:notice]
4
+
2
5
  h1 Settings values
3
6
 
4
7
  table.table.table-striped.table-bordered
@@ -25,3 +28,7 @@
25
28
  td AppSettings.uploads.s3_active?
26
29
  td = AppSettings.uploads.s3_active?
27
30
  td Presence checker
31
+
32
+
33
+ p
34
+ = link_to 'Reload settings', reload_path
@@ -1,3 +1,5 @@
1
1
  Example::Application.routes.draw do
2
+ get :reload, to: 'examples#reload'
3
+
2
4
  root to: 'examples#index'
3
5
  end
@@ -1,10 +1,11 @@
1
1
  require 'setty/version'
2
2
  require 'setty/options'
3
3
  require 'setty/loader'
4
+ require 'setty/settings'
4
5
  require 'setty/railtie' if defined? Rails
5
6
 
6
7
  module Setty
7
8
  def self.load(path, enviroment)
8
- Loader.new(path, enviroment).options
9
+ Settings.new Loader.new(path, enviroment)
9
10
  end
10
11
  end
@@ -1,33 +1,35 @@
1
1
  require 'erb'
2
2
  require 'yaml'
3
+ require 'pathname'
3
4
  require 'active_support/core_ext/hash/keys.rb'
4
5
  require 'active_support/core_ext/string/inflections.rb'
5
6
 
6
7
  module Setty
7
8
  MissingEnviromentError = Class.new(RuntimeError)
9
+ NotReadableFileError = Class.new(RuntimeError)
8
10
 
9
11
  class Loader
10
12
  def initialize(path, enviroment)
11
- @base_path = path
12
- @enviroment = enviroment
13
+ @base_path = Pathname(path)
14
+ @enviroment = enviroment.to_s
13
15
  end
14
16
 
15
- def options
16
- load_options @base_path
17
+ def load_options
18
+ load_option_and_sub_options @base_path
17
19
  end
18
20
 
19
21
  private
20
22
 
21
- def load_options(path)
22
- options = load_options_from_file "#{path}.yml"
23
+ def load_option_and_sub_options(path)
24
+ options = load_options_from_file path.sub_ext('.yml')
23
25
  find_nested_options(path).each do |sub_path|
24
- options[:"#{File.basename(sub_path)}"] = load_options(sub_path)
26
+ options[sub_path.basename.to_s.to_sym] = load_option_and_sub_options sub_path
25
27
  end
26
28
  options
27
29
  end
28
30
 
29
31
  def find_nested_options(path)
30
- Dir.glob(File.join(path, '/*')).map { |file_path| "#{path}/#{File.basename(file_path, '.yml')}" }.uniq
32
+ Pathname.glob(path.join('*')).map { |file_path| path.join file_path.basename('.yml') }.uniq
31
33
  end
32
34
 
33
35
  def load_options_from_file(path)
@@ -38,13 +40,15 @@ module Setty
38
40
  Options[enviroment_options.symbolize_keys]
39
41
  end
40
42
 
41
- def load_enviroment_options_from_file(path)
42
- return {} unless File.readable? path
43
+ def load_enviroment_options_from_file(file)
44
+ return {} unless file.exist?
43
45
 
44
- yaml_content = ERB.new(File.read(path)).result
46
+ raise NotReadableFileError, %Q(Settings file "#{file}" is not readable) unless file.readable?
47
+
48
+ yaml_content = ERB.new(file.read).result
45
49
  options = YAML.load yaml_content
46
50
 
47
- options[@enviroment.to_s]
51
+ options[@enviroment]
48
52
  end
49
53
  end
50
54
  end
@@ -3,12 +3,14 @@ module Setty
3
3
  config.setty = ActiveSupport::OrderedOptions.new
4
4
  config.setty[:settings_path] = 'settings'
5
5
  config.setty[:settings_object_name] = 'Settings'
6
+ config.setty[:settings_environment] = Rails.env
6
7
 
7
8
  initializer 'setty.load' do |app|
8
9
  path = app.root.join 'config', app.config.setty[:settings_path]
9
10
  object_name = app.config.setty[:settings_object_name]
11
+ environment = app.config.setty[:settings_environment]
10
12
 
11
- Object.const_set object_name, ::Setty.load(path, Rails.env)
13
+ Object.const_set object_name, ::Setty.load(path, environment)
12
14
  end
13
15
  end
14
16
  end
@@ -0,0 +1,16 @@
1
+ module Setty
2
+ class Settings < Delegator
3
+ def initialize(loader)
4
+ @loader = loader
5
+ @options = loader.load_options
6
+ end
7
+
8
+ def __getobj__
9
+ @options
10
+ end
11
+
12
+ def reload
13
+ @options = @loader.load_options
14
+ end
15
+ end
16
+ end
@@ -1,3 +1,3 @@
1
1
  module Setty
2
- VERSION = "1.0.0"
2
+ VERSION = '1.1.0'
3
3
  end
@@ -22,8 +22,8 @@ Gem::Specification.new do |spec|
22
22
 
23
23
  spec.add_development_dependency 'bundler', '~> 1.3'
24
24
  spec.add_development_dependency 'rake'
25
- spec.add_development_dependency 'rspec', '~> 2.14'
26
- spec.add_development_dependency 'rspec-mocks', '>= 2.12.3'
25
+ spec.add_development_dependency 'rspec', '3.0'
26
+ spec.add_development_dependency 'rspec-mocks', '3.0'
27
27
  spec.add_development_dependency 'coveralls'
28
28
  end
29
29
 
@@ -0,0 +1,2 @@
1
+ test:
2
+ content: 'should not be available'
@@ -6,11 +6,11 @@ describe Setty do
6
6
  end
7
7
 
8
8
  describe "#load" do
9
- it "returns loaded options" do
10
- loader = double options: 'options'
11
- allow(Setty::Loader).to receive(:new).with('path', 'enviroment').and_return loader
9
+ it "returns settings" do
10
+ allow(Setty::Loader).to receive(:new).with('path', 'enviroment').and_return 'loader'
11
+ allow(Setty::Settings).to receive(:new).with('loader').and_return 'settings'
12
12
 
13
- expect(Setty.load('path', 'enviroment')).to eq 'options'
13
+ expect(Setty.load('path', 'enviroment')).to eq 'settings'
14
14
  end
15
15
  end
16
16
  end
@@ -1,5 +1,4 @@
1
1
  require 'spec_helper'
2
- require 'tempfile'
3
2
 
4
3
  module Setty
5
4
  describe Loader do
@@ -10,7 +9,7 @@ module Setty
10
9
  it "loads yaml depending on environment variable" do
11
10
  options = load 'settings'
12
11
  expect(options.enviroment).to eq 'test'
13
- expect(options.active?).to be_true
12
+ expect(options.active?).to be_truthy
14
13
  end
15
14
 
16
15
  it "does not complain about missing yaml file" do
@@ -39,8 +38,18 @@ module Setty
39
38
  expect { load 'settings', 'bongus enviroment' }.to raise_error MissingEnviromentError
40
39
  end
41
40
 
41
+ it "raises error on not readable settings file" do
42
+ begin
43
+ File.chmod(0200, fixture('forbidden.yml'))
44
+
45
+ expect { load 'forbidden' }.to raise_error NotReadableFileError
46
+ ensure
47
+ File.chmod(0777, fixture('forbidden.yml'))
48
+ end
49
+ end
50
+
42
51
  def load(name, env = 'test')
43
- Loader.new(fixture(name), env).options
52
+ Loader.new(fixture(name), env).load_options
44
53
  end
45
54
  end
46
55
  end
@@ -19,9 +19,9 @@ module Setty
19
19
  it "has attribute checker" do
20
20
  options = Setty::Options[active: true, disabled: false]
21
21
 
22
- expect(options.active?).to be_true
23
- expect(options.disabled?).to be_false
24
- expect(options.missing?).to be_false
22
+ expect(options.active?).to be_truthy
23
+ expect(options.disabled?).to be_falsey
24
+ expect(options.missing?).to be_falsey
25
25
  end
26
26
  end
27
27
  end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ module Setty
4
+ describe Settings do
5
+ let(:options) { 'options' }
6
+ let(:loader) { double 'Loader', load_options: options }
7
+
8
+ it "delegates to loader's loaded options" do
9
+ config = Settings.new(loader)
10
+
11
+ expect(config).to eq options
12
+ end
13
+
14
+ it "caches the loaded options" do
15
+ allow(loader).to receive(:load_options).and_return options
16
+
17
+ config = Settings.new(loader)
18
+
19
+ 2.times { config.size }
20
+
21
+ expect(loader).to have_received(:load_options).once
22
+ end
23
+
24
+ describe "#reload" do
25
+ it "loads options again from the loader" do
26
+ config = Settings.new(loader)
27
+
28
+ allow(loader).to receive(:load_options).and_return 'new options'
29
+
30
+ config.reload
31
+
32
+ expect(config).to eq 'new options'
33
+ end
34
+ end
35
+ end
36
+ end
metadata CHANGED
@@ -1,110 +1,97 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: setty
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
5
- prerelease:
4
+ version: 1.1.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Radoslav Stankov
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2014-02-15 00:00:00.000000000 Z
11
+ date: 2014-06-11 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: activesupport
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
19
  version: '3.2'
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - ">="
28
25
  - !ruby/object:Gem::Version
29
26
  version: '3.2'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: bundler
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ~>
31
+ - - "~>"
36
32
  - !ruby/object:Gem::Version
37
33
  version: '1.3'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ~>
38
+ - - "~>"
44
39
  - !ruby/object:Gem::Version
45
40
  version: '1.3'
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: rake
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ! '>='
45
+ - - ">="
52
46
  - !ruby/object:Gem::Version
53
47
  version: '0'
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ! '>='
52
+ - - ">="
60
53
  - !ruby/object:Gem::Version
61
54
  version: '0'
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: rspec
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
- - - ~>
59
+ - - '='
68
60
  - !ruby/object:Gem::Version
69
- version: '2.14'
61
+ version: '3.0'
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
- - - ~>
66
+ - - '='
76
67
  - !ruby/object:Gem::Version
77
- version: '2.14'
68
+ version: '3.0'
78
69
  - !ruby/object:Gem::Dependency
79
70
  name: rspec-mocks
80
71
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
72
  requirements:
83
- - - ! '>='
73
+ - - '='
84
74
  - !ruby/object:Gem::Version
85
- version: 2.12.3
75
+ version: '3.0'
86
76
  type: :development
87
77
  prerelease: false
88
78
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
79
  requirements:
91
- - - ! '>='
80
+ - - '='
92
81
  - !ruby/object:Gem::Version
93
- version: 2.12.3
82
+ version: '3.0'
94
83
  - !ruby/object:Gem::Dependency
95
84
  name: coveralls
96
85
  requirement: !ruby/object:Gem::Requirement
97
- none: false
98
86
  requirements:
99
- - - ! '>='
87
+ - - ">="
100
88
  - !ruby/object:Gem::Version
101
89
  version: '0'
102
90
  type: :development
103
91
  prerelease: false
104
92
  version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
93
  requirements:
107
- - - ! '>='
94
+ - - ">="
108
95
  - !ruby/object:Gem::Version
109
96
  version: '0'
110
97
  description: Mini application configuration for Rails projects.
@@ -114,9 +101,9 @@ executables: []
114
101
  extensions: []
115
102
  extra_rdoc_files: []
116
103
  files:
117
- - .gitignore
118
- - .rspec
119
- - .travis.yml
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
120
107
  - CHANGELOG.md
121
108
  - Gemfile
122
109
  - LICENSE.txt
@@ -160,8 +147,10 @@ files:
160
147
  - lib/setty/loader.rb
161
148
  - lib/setty/options.rb
162
149
  - lib/setty/railtie.rb
150
+ - lib/setty/settings.rb
163
151
  - lib/setty/version.rb
164
152
  - setty.gemspec
153
+ - spec/fixtures/forbidden.yml
165
154
  - spec/fixtures/settings.yml
166
155
  - spec/fixtures/settings/inner_0.yml
167
156
  - spec/fixtures/settings/inner_1.yml
@@ -170,39 +159,34 @@ files:
170
159
  - spec/setty/base_spec.rb
171
160
  - spec/setty/loader_spec.rb
172
161
  - spec/setty/options_spec.rb
162
+ - spec/setty/settings_spec.rb
173
163
  - spec/spec_helper.rb
174
164
  homepage: https://github.com/rstankov/setty
175
165
  licenses:
176
166
  - MIT
167
+ metadata: {}
177
168
  post_install_message:
178
169
  rdoc_options: []
179
170
  require_paths:
180
171
  - lib
181
172
  required_ruby_version: !ruby/object:Gem::Requirement
182
- none: false
183
173
  requirements:
184
- - - ! '>='
174
+ - - ">="
185
175
  - !ruby/object:Gem::Version
186
176
  version: '0'
187
- segments:
188
- - 0
189
- hash: -655774490321426843
190
177
  required_rubygems_version: !ruby/object:Gem::Requirement
191
- none: false
192
178
  requirements:
193
- - - ! '>='
179
+ - - ">="
194
180
  - !ruby/object:Gem::Version
195
181
  version: '0'
196
- segments:
197
- - 0
198
- hash: -655774490321426843
199
182
  requirements: []
200
183
  rubyforge_project:
201
- rubygems_version: 1.8.25
184
+ rubygems_version: 2.2.2
202
185
  signing_key:
203
- specification_version: 3
186
+ specification_version: 4
204
187
  summary: Geneartes object hierarchy based on YAML files
205
188
  test_files:
189
+ - spec/fixtures/forbidden.yml
206
190
  - spec/fixtures/settings.yml
207
191
  - spec/fixtures/settings/inner_0.yml
208
192
  - spec/fixtures/settings/inner_1.yml
@@ -211,4 +195,6 @@ test_files:
211
195
  - spec/setty/base_spec.rb
212
196
  - spec/setty/loader_spec.rb
213
197
  - spec/setty/options_spec.rb
198
+ - spec/setty/settings_spec.rb
214
199
  - spec/spec_helper.rb
200
+ has_rdoc: