settable 1.1 → 3.0.2
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 +7 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Readme.md +127 -64
- data/lib/settable/environment/env.rb +9 -0
- data/lib/settable/environment/rails.rb +10 -0
- data/lib/settable.rb +411 -74
- data/settable.gemspec +16 -16
- data/spec/settable_spec.rb +261 -0
- data/spec/spec_helper.rb +8 -0
- metadata +32 -16
- data/examples/example.rb +0 -43
- data/examples/rails.rb +0 -65
- data/test/settable_test.rb +0 -252
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9fc35a6d3c9c14cc86b200c1340fd2237cfd033a
|
4
|
+
data.tar.gz: da581e63af6e9d0367f8f0f4a159d4ad7a0d2c7a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f29356b13085637dba0e53224a340ddfa8c6c6c7dd87aa857348b631b9de510b1233b605da2241306c5ad59a47d6fce100e960001a2cd40c219e65caa0b862b9
|
7
|
+
data.tar.gz: b1d84ac501e7997de743f1de902e181543813e8e814e2ba1dde907eb49313fe60c8af9f4d3fe90f464e55c78570659f079e502e9915191b851f6656c687d6bab
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Readme.md
CHANGED
@@ -1,74 +1,137 @@
|
|
1
|
-
|
2
|
-
===
|
1
|
+
# Settable 3.0
|
3
2
|
|
4
|
-
|
3
|
+
[](https://travis-ci.org/robhurring/settable)
|
5
4
|
|
6
|
-
|
5
|
+
An alternative to using rails' environment files or YAML for application config. Settable was created out of the frustration of
|
6
|
+
missing a config setting in an environment file, or constantly duplicating YAML keys for different environments. Settable helps
|
7
|
+
make your config "safe" by always having a default value, and its built using Ruby so it is highly customizable and powerful.
|
7
8
|
|
8
|
-
|
9
|
-
include Settable
|
9
|
+
Check the Usage for some details on how it can be used.
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
end
|
14
|
-
end
|
11
|
+
**Note:** This is a complete re-write from settable v2.0 and not backwards compatible. The old code was clunky and confusing, so
|
12
|
+
it was refactored to be cleaner and a little more flexible.
|
15
13
|
|
16
|
-
|
14
|
+
## Installation
|
17
15
|
|
18
|
-
|
19
|
-
config = Configuration.new do
|
20
|
-
set :environment, 'development'
|
21
|
-
enable :debug
|
22
|
-
|
23
|
-
set :api_token do
|
24
|
-
return 'PRODTOKEN' if environment == 'production'
|
25
|
-
'DEVTOKEN'
|
26
|
-
end
|
16
|
+
Add this line to your application's Gemfile:
|
27
17
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
18
|
+
gem 'settable', git: 'https://github.com/robhurring/settable'
|
19
|
+
|
20
|
+
And then execute:
|
21
|
+
|
22
|
+
$ bundle
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
### Basic Usage
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
$config = Settable.configure do
|
30
|
+
# basic set, similar to capistrano and sinatra
|
31
|
+
set :username, 'user'
|
32
|
+
set :password, 's3kr1t'
|
33
|
+
|
34
|
+
# namespace support to keep config clean
|
35
|
+
namespace :tracking do
|
36
|
+
set :enabled, true
|
37
|
+
end
|
38
|
+
|
39
|
+
set :block do
|
40
|
+
'blocks are allowed too!'
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
if params[:user] == $config.username && params[:password] == $config.password
|
45
|
+
...
|
46
|
+
end
|
47
|
+
|
48
|
+
# all settings have a "presence" method, just add a "?" to check if it has been set
|
49
|
+
if $config.tracking.enabled?
|
50
|
+
...
|
51
|
+
end
|
52
|
+
```
|
53
|
+
|
54
|
+
### Rails Integration
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
# config/initializers/app_config.rb
|
58
|
+
$config = Settable.configure do
|
59
|
+
# this enables the +environment+ helpers below, so we can set values in specific
|
60
|
+
# environments only. (environment testers can be swapped out - see the advanced example)
|
61
|
+
use_environment :rails
|
62
|
+
|
63
|
+
set :username do
|
64
|
+
# checks Rails.env and will return 'superadmin' when in production
|
65
|
+
environment :production, 'superadmin'
|
66
|
+
|
67
|
+
# defaults back to 'devuser' if environment doesn't match
|
68
|
+
'devuser'
|
69
|
+
end
|
70
|
+
|
71
|
+
set :password do
|
72
|
+
environment :production, 's3kr1t'
|
73
|
+
|
74
|
+
'defaultpassword'
|
75
|
+
end
|
76
|
+
|
77
|
+
set :tracking do
|
78
|
+
# check if we're in production _or_ staging
|
79
|
+
environment [:production, :staging], true
|
80
|
+
false
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
# some_controller.rb
|
85
|
+
http_basic_authenticate_with name: $config.username, password: $config.password
|
86
|
+
```
|
87
|
+
|
88
|
+
### Advanced Integration
|
89
|
+
|
90
|
+
To use a custom class/namespace for your configuration you can do the following:
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
class MyApp
|
94
|
+
# include the Settable DSL
|
95
|
+
include Settable
|
96
|
+
|
97
|
+
# create a custom environment tester, any object that respond to #call(value) can be used
|
98
|
+
# as an environment tester. This is in the core code when using "use_environment :env"
|
99
|
+
module EnvironmentTester
|
100
|
+
def self.call(environment)
|
101
|
+
::ENV.has_key?(environment.to_s.upcase)
|
43
102
|
end
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
enable :tracking, :caching
|
56
|
-
else
|
57
|
-
disable :tracking, :caching
|
58
|
-
end
|
59
|
-
|
60
|
-
set :api_token do
|
61
|
-
in_production { return 'PRODTOKEN' }
|
62
|
-
in_development{ return 'DEVTOKEN' }
|
63
|
-
'OTHERTOKEN'
|
64
|
-
end
|
65
|
-
|
66
|
-
set :api_endpoint do
|
67
|
-
in_environments(:development, :test){ return "http://sandbox.example.com/api/#{api_token}" }
|
68
|
-
"http://example.com/api/#{api_token}"
|
103
|
+
end
|
104
|
+
|
105
|
+
# creates a class and instance method +config+ that holds all settings
|
106
|
+
settable :config do
|
107
|
+
# use our custom env tester for all +environment+ calls
|
108
|
+
use_environment EnvironmentTester
|
109
|
+
|
110
|
+
set :redis_uri do
|
111
|
+
# check our ENV for the REDIS_TO_GO_URL key
|
112
|
+
environment :REDIS_TO_GO_URL do
|
113
|
+
ENV['REDIS_TO_GO_URL']
|
69
114
|
end
|
115
|
+
|
116
|
+
# default to localhost if not found
|
117
|
+
'localhost:6379'
|
70
118
|
end
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
119
|
+
end
|
120
|
+
|
121
|
+
def redis
|
122
|
+
Redis.new(config.redis_uri)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
$app = MyApp.new
|
127
|
+
$redis = $app.redis
|
128
|
+
# you can also reach the redis_uri at MyApp.config.redis_uri
|
129
|
+
```
|
130
|
+
|
131
|
+
## Contributing
|
132
|
+
|
133
|
+
1. Fork it
|
134
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
135
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
136
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
137
|
+
5. Create new Pull Request
|