settable 1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/Rakefile +2 -0
- data/Readme.md +74 -0
- data/examples/example.rb +36 -0
- data/examples/rails.rb +51 -0
- data/lib/settable.rb +64 -0
- data/settable.gemspec +21 -0
- metadata +64 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
data/Readme.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
About
|
2
|
+
===
|
3
|
+
|
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.
|
5
|
+
|
6
|
+
To create the configuration wrapper, its as simple as:
|
7
|
+
|
8
|
+
class Configuration
|
9
|
+
include Settable
|
10
|
+
|
11
|
+
def initialize(&block)
|
12
|
+
instance_eval(&block) if block_given?
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
to use this in your initializer, script, etc. just open it up and use set/enable/etc
|
17
|
+
|
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
|
27
|
+
|
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
|
43
|
+
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}"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
puts config.debug?
|
73
|
+
puts config.caching?
|
74
|
+
puts config.api_endpoint
|
data/examples/example.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
$:.push File.expand_path("../../lib", __FILE__)
|
2
|
+
require 'settable'
|
3
|
+
|
4
|
+
class Configuration
|
5
|
+
include Settable
|
6
|
+
|
7
|
+
def initialize(&block)
|
8
|
+
instance_eval(&block) if block_given?
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
config = Configuration.new do
|
13
|
+
set :environment, 'development'
|
14
|
+
|
15
|
+
set :api_token do
|
16
|
+
return 'PRODTOKEN' if environment == 'production'
|
17
|
+
'DEVTOKEN'
|
18
|
+
end
|
19
|
+
|
20
|
+
set :api_endpoint, "http://example.com/api/#{api_token}"
|
21
|
+
set :environment, 'production'
|
22
|
+
end
|
23
|
+
|
24
|
+
# external stuffs
|
25
|
+
config.set :something, 1
|
26
|
+
config.enable :debug
|
27
|
+
|
28
|
+
puts config.inspect
|
29
|
+
|
30
|
+
puts '-' * 80
|
31
|
+
|
32
|
+
puts config.environment
|
33
|
+
puts config.api_token
|
34
|
+
puts config.api_endpoint
|
35
|
+
puts config.something
|
36
|
+
puts config.debug?
|
data/examples/rails.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
$:.push File.expand_path("../../lib", __FILE__)
|
2
|
+
require 'settable'
|
3
|
+
|
4
|
+
# stub out our rails
|
5
|
+
module Rails
|
6
|
+
def self.env; :blah end
|
7
|
+
end
|
8
|
+
|
9
|
+
class Configuration
|
10
|
+
include Settable
|
11
|
+
include Settable::Rails
|
12
|
+
|
13
|
+
def initialize(&block)
|
14
|
+
instance_eval(&block) if block_given?
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
@config = Configuration.new do
|
19
|
+
define_environments :blah, :qa
|
20
|
+
|
21
|
+
set :something, in_blah?
|
22
|
+
set :debug, in_environments?(:development, :test)
|
23
|
+
|
24
|
+
if in_production?
|
25
|
+
enable :tracking, :caching
|
26
|
+
else
|
27
|
+
disable :tracking, :caching
|
28
|
+
end
|
29
|
+
|
30
|
+
set :api_token do
|
31
|
+
in_production { return 'PRODTOKEN' }
|
32
|
+
in_development{ return 'DEVTOKEN' }
|
33
|
+
'OTHERTOKEN'
|
34
|
+
end
|
35
|
+
|
36
|
+
set :api_endpoint do
|
37
|
+
in_environments(:development, :test){ return "http://sandbox.example.com/api/#{api_token}" }
|
38
|
+
"http://example.com/api/#{api_token}"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# external stuffs
|
43
|
+
puts @config.inspect
|
44
|
+
|
45
|
+
puts '-' * 80
|
46
|
+
|
47
|
+
puts @config.tracking?
|
48
|
+
puts @config.caching?
|
49
|
+
puts @config.api_token
|
50
|
+
puts @config.api_endpoint
|
51
|
+
puts @config.debug?
|
data/lib/settable.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
module Settable
|
2
|
+
VERSION = "1.0"
|
3
|
+
|
4
|
+
module Rails
|
5
|
+
DEFAULT_ENVIRONMENTS = [:development, :production, :test]
|
6
|
+
|
7
|
+
# allow us to add custom environment helpers
|
8
|
+
def define_environments(*envs)
|
9
|
+
envs.each do |env|
|
10
|
+
define_metaclass_method(:"in_#{env}"){ |&block| in_environment(env.to_sym, &block) }
|
11
|
+
define_metaclass_method(:"in_#{env}?"){ in_environment?(env.to_sym) }
|
12
|
+
end
|
13
|
+
end
|
14
|
+
alias_method :define_environment, :define_environments
|
15
|
+
|
16
|
+
# create our default environments
|
17
|
+
DEFAULT_ENVIRONMENTS.each do |env|
|
18
|
+
define_method(:"in_#{env}"){ |&block| in_environment(env.to_sym, &block) }
|
19
|
+
define_method(:"in_#{env}?"){ in_environment?(env.to_sym) }
|
20
|
+
end
|
21
|
+
|
22
|
+
# helper method that will call the block if the Rails.env matches the given environments
|
23
|
+
def in_environments(*envs, &block)
|
24
|
+
block.call if envs.include?(::Rails.env.to_sym)
|
25
|
+
end
|
26
|
+
alias_method :in_environment, :in_environments
|
27
|
+
|
28
|
+
# tests if we're in the given environment(s)
|
29
|
+
def in_environments?(*envs)
|
30
|
+
envs.include?(::Rails.env.to_sym)
|
31
|
+
end
|
32
|
+
alias_method :in_environment?, :in_environments?
|
33
|
+
end
|
34
|
+
|
35
|
+
def define_metaclass_method(method, &block)
|
36
|
+
(class << self; self; end).send :define_method, method, &block
|
37
|
+
end
|
38
|
+
|
39
|
+
# list
|
40
|
+
def __settables__
|
41
|
+
@__settables__ ||= []
|
42
|
+
end
|
43
|
+
|
44
|
+
# modified from sinatra
|
45
|
+
def set(key, value=nil, &block)
|
46
|
+
raise ArgumentError, "You must specify either a block or value" if block_given? && !value.nil?
|
47
|
+
value = block if block_given?
|
48
|
+
if value.is_a?(Proc)
|
49
|
+
__settables__ << key
|
50
|
+
define_metaclass_method key, &value
|
51
|
+
define_metaclass_method(:"#{key}?"){ !!__send__(key) }
|
52
|
+
else
|
53
|
+
set key, Proc.new{value}
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def enable(*keys)
|
58
|
+
keys.each{ |key| set key, true }
|
59
|
+
end
|
60
|
+
|
61
|
+
def disable(*keys)
|
62
|
+
keys.each{ |key| set key, false }
|
63
|
+
end
|
64
|
+
end
|
data/settable.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "settable"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "settable"
|
7
|
+
s.version = Settable::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Rob Hurring"]
|
10
|
+
s.email = ["robhurring@gmail.com"]
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{Small include to make config files better}
|
13
|
+
s.description = %q{Small include to make config files better}
|
14
|
+
|
15
|
+
s.rubyforge_project = "settable"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: settable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: "1.0"
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Rob Hurring
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-05-09 00:00:00 -04:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: Small include to make config files better
|
18
|
+
email:
|
19
|
+
- robhurring@gmail.com
|
20
|
+
executables: []
|
21
|
+
|
22
|
+
extensions: []
|
23
|
+
|
24
|
+
extra_rdoc_files: []
|
25
|
+
|
26
|
+
files:
|
27
|
+
- .gitignore
|
28
|
+
- Gemfile
|
29
|
+
- Rakefile
|
30
|
+
- Readme.md
|
31
|
+
- examples/example.rb
|
32
|
+
- examples/rails.rb
|
33
|
+
- lib/settable.rb
|
34
|
+
- settable.gemspec
|
35
|
+
has_rdoc: true
|
36
|
+
homepage: ""
|
37
|
+
licenses: []
|
38
|
+
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "0"
|
56
|
+
requirements: []
|
57
|
+
|
58
|
+
rubyforge_project: settable
|
59
|
+
rubygems_version: 1.5.0
|
60
|
+
signing_key:
|
61
|
+
specification_version: 3
|
62
|
+
summary: Small include to make config files better
|
63
|
+
test_files: []
|
64
|
+
|