settable 1.1 → 3.0.2

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format=documentation
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 1.9.3
5
+ script: bundle exec rspec spec
data/Readme.md CHANGED
@@ -1,74 +1,137 @@
1
- About
2
- ===
1
+ # Settable 3.0
3
2
 
4
- Simple library to make configuration files dead simple. It uses "set, enable, disable" just like capistrano and sinatra. It also has some rails helpers to change settings based off environments.
3
+ [![Build Status](https://travis-ci.org/robhurring/settable.png?branch=master)](https://travis-ci.org/robhurring/settable)
5
4
 
6
- To create the configuration wrapper, its as simple as:
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
- class Configuration
9
- include Settable
9
+ Check the Usage for some details on how it can be used.
10
10
 
11
- def initialize(&block)
12
- instance_eval(&block) if block_given?
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
- to use this in your initializer, script, etc. just open it up and use set/enable/etc
14
+ ## Installation
17
15
 
18
- # using it without the rails helpers
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
- set :api_endpoint, "http://example.com/api/#{api_token}"
29
- end
30
-
31
- puts config.debug?
32
- puts config.api_endpoint
33
-
34
- or if you wanted to use it with rails
35
-
36
- class Configuration
37
- include Settable
38
- include Settable::Rails
39
-
40
- def initialize(&block)
41
- instance_eval(&block) if block_given?
42
- end
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
- and use the environment helpers
46
-
47
- config = Configuration.new do
48
- # add some custom environments from our app
49
- define_environments :blah, :qa
50
-
51
- set :something, in_blah?
52
- set :debug, in_environments?(:development, :test)
53
-
54
- if in_production?
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
- puts config.debug?
73
- puts config.caching?
74
- puts config.api_endpoint
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
@@ -0,0 +1,9 @@
1
+ module Settable
2
+ module Environment
3
+ module Env
4
+ def self.call(environment)
5
+ ::ENV.has_key?(environment.to_s)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ module Settable
2
+ module Environment
3
+ module Rails
4
+ def self.call(environment)
5
+ return false unless defined?(::Rails)
6
+ ::Rails.env.to_s == environment.to_s
7
+ end
8
+ end
9
+ end
10
+ end