endeco 0.0.1
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.
- data/.gitignore +4 -0
- data/.rspec +2 -0
- data/Gemfile +6 -0
- data/Guardfile +14 -0
- data/History.txt +3 -0
- data/README.md +85 -0
- data/Rakefile +1 -0
- data/endeco.gemspec +24 -0
- data/lib/endeco/cache.rb +30 -0
- data/lib/endeco/config.rb +21 -0
- data/lib/endeco/rails.rb +13 -0
- data/lib/endeco/version.rb +3 -0
- data/lib/endeco.rb +45 -0
- data/spec/lib/endeco_spec.rb +109 -0
- data/spec/spec_helper.rb +8 -0
- metadata +73 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
guard 'rspec', :version => 2 do
|
5
|
+
watch(%r{^spec/.+_spec\.rb$})
|
6
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
7
|
+
watch('spec/spec_helper.rb') { "spec" }
|
8
|
+
|
9
|
+
watch(%r{^spec/.+_spec\.rb$})
|
10
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
11
|
+
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
|
12
|
+
watch('spec/spec_helper.rb') { "spec" }
|
13
|
+
end
|
14
|
+
|
data/History.txt
ADDED
data/README.md
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
# endeco
|
2
|
+
|
3
|
+
https://github.com/morimori/endeco
|
4
|
+
|
5
|
+
## DESCRIPTION
|
6
|
+
|
7
|
+
Managing the configuration of the environment-dependent.
|
8
|
+
|
9
|
+
Like "envdir", contained in djb's daemontools.
|
10
|
+
see http://cr.yp.to/daemontools/envdir.html
|
11
|
+
|
12
|
+
## FEATURES
|
13
|
+
|
14
|
+
* File base configuration
|
15
|
+
* Configuration changes on running apps (if cache disabled)
|
16
|
+
* Rails integration
|
17
|
+
|
18
|
+
## SYNOPSIS
|
19
|
+
|
20
|
+
$ cat sample.rb
|
21
|
+
require 'rubygems'
|
22
|
+
require 'endeco'
|
23
|
+
Endeco::Config.path = 'env'
|
24
|
+
Endeco::Config.env = 'development'
|
25
|
+
print Endeco.hoge
|
26
|
+
print Endeco.fuga
|
27
|
+
|
28
|
+
$ mkdir -p env/development
|
29
|
+
$ echo 'foo' > env/development/hoge
|
30
|
+
$ echo 'bar' > env/development/fuga
|
31
|
+
$ ruby sample.rb
|
32
|
+
foo
|
33
|
+
bar
|
34
|
+
|
35
|
+
### Rails integration
|
36
|
+
#### setup configuration files
|
37
|
+
|
38
|
+
$ cd $RAILS_ROOT
|
39
|
+
$ mkdir -p env/{development,production}
|
40
|
+
$ echo 'foo' > env/development/hoge
|
41
|
+
$ echo 'bar' > env/production/hoge`
|
42
|
+
|
43
|
+
#### Gemfile
|
44
|
+
|
45
|
+
...
|
46
|
+
gem 'endeco', :require => 'endeco/rails'
|
47
|
+
...
|
48
|
+
|
49
|
+
#### result
|
50
|
+
|
51
|
+
$ bundle exec rails runner -e development 'Endeco.hoge'
|
52
|
+
foo
|
53
|
+
$ bundle exec rails runner -e production 'Endeco.hoge'
|
54
|
+
bar
|
55
|
+
|
56
|
+
## REQUIREMENTS:
|
57
|
+
|
58
|
+
ruby 1.8.7 or higher / ruby 1.9.2 or higher
|
59
|
+
|
60
|
+
## INSTALL:
|
61
|
+
|
62
|
+
$ gem install endeco
|
63
|
+
|
64
|
+
## LICENSE:
|
65
|
+
|
66
|
+
(The MIT License)
|
67
|
+
|
68
|
+
Copyright (c) 2011 Takatoshi -morimori- MORIYAMA
|
69
|
+
|
70
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
71
|
+
a copy of this software and associated documentation files (the
|
72
|
+
'Software'), to deal in the Software without restriction, including
|
73
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
74
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
75
|
+
permit persons to whom the Software is furnished to do so, subject to
|
76
|
+
the following conditions:
|
77
|
+
|
78
|
+
The above copyright notice and this permission notice shall be
|
79
|
+
included in all copies or substantial portions of the Software.
|
80
|
+
|
81
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
82
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
83
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
84
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
85
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/endeco.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "endeco/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "endeco"
|
7
|
+
s.version = Endeco::VERSION
|
8
|
+
s.authors = ["Takatoshi MORIYAMA"]
|
9
|
+
s.email = ["hawk@at-exit.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Environment Dependent Configuration}
|
12
|
+
s.description = %q{Environment Dependent Configuration}
|
13
|
+
|
14
|
+
s.rubyforge_project = "endeco"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
s.add_development_dependency "rspec"
|
23
|
+
# s.add_runtime_dependency "rest-client"
|
24
|
+
end
|
data/lib/endeco/cache.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
module Endeco
|
2
|
+
module Cache
|
3
|
+
@@cache = {}
|
4
|
+
@@enable = false
|
5
|
+
|
6
|
+
def self.[]=(key, value)
|
7
|
+
@@cache[key] = value
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.[](key)
|
11
|
+
@@cache[key]
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.key?(key)
|
15
|
+
@@cache.key? key
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.clear
|
19
|
+
@@cache.clear
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.enable
|
23
|
+
@@enable
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.enable=(value)
|
27
|
+
@@enable = value
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Endeco
|
2
|
+
module Config
|
3
|
+
@@path = nil
|
4
|
+
@@env = nil
|
5
|
+
def self.path
|
6
|
+
@@path
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.path=(new_path)
|
10
|
+
@@path = new_path
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.env
|
14
|
+
@@env
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.env=(new_env)
|
18
|
+
@@env = new_env
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/endeco/rails.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'endeco'
|
2
|
+
|
3
|
+
module Endeco
|
4
|
+
class Railtie < Rails::Railtie
|
5
|
+
config.endeco = ActiveSupport::OrderedOptions.new
|
6
|
+
|
7
|
+
config.before_initialize do
|
8
|
+
Endeco::Config.path = config.endeco.path || "#{Rails.root}/env"
|
9
|
+
Endeco::Config.env = config.endeco.env || Rails.env.to_s
|
10
|
+
Endeco::Cache.enable = config.endeco.enable_cache || Rails.env.production?
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/lib/endeco.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'endeco/version'
|
2
|
+
require 'endeco/config'
|
3
|
+
require 'endeco/cache'
|
4
|
+
|
5
|
+
module Endeco
|
6
|
+
def self.method_missing(symbol, *args)
|
7
|
+
safe_name = symbol.to_s.sub /!$/, ''
|
8
|
+
def_methods safe_name
|
9
|
+
__send__ symbol, *args
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.[](key, options = {})
|
13
|
+
safe_key = key.sub(/!$/, '')
|
14
|
+
return Cache[safe_key] if Cache.enable and !options[:force] and Cache.key?(safe_key)
|
15
|
+
fullpath = expand_path safe_key
|
16
|
+
if File.exists? fullpath
|
17
|
+
Cache[safe_key] = File.read fullpath
|
18
|
+
else
|
19
|
+
if key == safe_key
|
20
|
+
nil
|
21
|
+
else
|
22
|
+
raise Errno::ENOENT, "Errno::ENOENT: No such file or directory - #{fullpath}"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
def self.expand_path(name)
|
29
|
+
File.expand_path File.join([Config.path, Config.env, name].compact)
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.def_methods(name)
|
33
|
+
module_eval %[
|
34
|
+
def self.#{name}(options = {})
|
35
|
+
self[__method__.to_s, options]
|
36
|
+
end
|
37
|
+
], __FILE__, __LINE__
|
38
|
+
|
39
|
+
module_eval %[
|
40
|
+
def self.#{name}!(options = {})
|
41
|
+
self[__method__.to_s, options]
|
42
|
+
end
|
43
|
+
], __FILE__, __LINE__
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Endeco do
|
4
|
+
before do
|
5
|
+
Endeco::Cache.clear
|
6
|
+
Endeco::Config.path = "#{File.dirname(__FILE__)}/../../env"
|
7
|
+
Endeco::Config.env = 'test'
|
8
|
+
FileUtils.mkdir_p File.expand_path(File.join(Endeco::Config.path, Endeco::Config.env))
|
9
|
+
end
|
10
|
+
|
11
|
+
after do
|
12
|
+
FileUtils.rm_rf File.expand_path(File.join(Endeco::Config.path))
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'file exists' do
|
16
|
+
before do
|
17
|
+
open File.join(Endeco::Config.path, Endeco::Config.env, 'test_var'), 'w' do |f|
|
18
|
+
f << 'hoge'
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
after do
|
23
|
+
File.unlink File.join(Endeco::Config.path, Endeco::Config.env, 'test_var')
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'can read file contents through filename method' do
|
27
|
+
Endeco.test_var.should == 'hoge'
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'can read file contents through filename method with bang' do
|
31
|
+
Endeco.test_var!.should == 'hoge'
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'can read file contents through brace with filename key' do
|
35
|
+
Endeco['test_var'].should == 'hoge'
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'can read file contents through brace with filename key with bang' do
|
39
|
+
Endeco['test_var!'].should == 'hoge'
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context 'file not exists' do
|
44
|
+
it 'return nil through filename method' do
|
45
|
+
Endeco.test_var.should be_nil
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'raise Errno::ENOENT through filename method with bang' do
|
49
|
+
expect{ Endeco.test_var! }.should raise_error(Errno::ENOENT)
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'return nil through brace with filename key' do
|
53
|
+
Endeco['test_var'].should be_nil
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'raise Errno::ENOENT through brace with filename key with bang' do
|
57
|
+
expect{ Endeco['test_var!'] }.should raise_error(Errno::ENOENT)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context 'cache disable' do
|
62
|
+
before do
|
63
|
+
open File.join(Endeco::Config.path, Endeco::Config.env, 'test_var'), 'w' do |f|
|
64
|
+
f << 'hoge'
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
after do
|
69
|
+
File.unlink File.join(Endeco::Config.path, Endeco::Config.env, 'test_var')
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'read variable is not cached' do
|
73
|
+
Endeco.test_var!.should == 'hoge'
|
74
|
+
open File.join(Endeco::Config.path, Endeco::Config.env, 'test_var'), 'w' do |f|
|
75
|
+
f << 'fuga'
|
76
|
+
end
|
77
|
+
Endeco.test_var!.should == 'fuga'
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
context 'cache enable' do
|
82
|
+
before do
|
83
|
+
Endeco::Cache.enable = true
|
84
|
+
open File.join(Endeco::Config.path, Endeco::Config.env, 'test_var'), 'w' do |f|
|
85
|
+
f << 'hoge'
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
after do
|
90
|
+
File.unlink File.join(Endeco::Config.path, Endeco::Config.env, 'test_var')
|
91
|
+
end
|
92
|
+
|
93
|
+
it 'read variable is cached' do
|
94
|
+
Endeco.test_var!.should == 'hoge'
|
95
|
+
open File.join(Endeco::Config.path, Endeco::Config.env, 'test_var'), 'w' do |f|
|
96
|
+
f << 'fuga'
|
97
|
+
end
|
98
|
+
Endeco.test_var!.should == 'hoge'
|
99
|
+
end
|
100
|
+
|
101
|
+
it 'force read cached variable with :force option' do
|
102
|
+
Endeco.test_var!.should == 'hoge'
|
103
|
+
open File.join(Endeco::Config.path, Endeco::Config.env, 'test_var'), 'w' do |f|
|
104
|
+
f << 'fuga'
|
105
|
+
end
|
106
|
+
Endeco.test_var!(:force => true).should == 'fuga'
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: endeco
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Takatoshi MORIYAMA
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-11-08 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &70195411594520 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70195411594520
|
25
|
+
description: Environment Dependent Configuration
|
26
|
+
email:
|
27
|
+
- hawk@at-exit.com
|
28
|
+
executables: []
|
29
|
+
extensions: []
|
30
|
+
extra_rdoc_files: []
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- .rspec
|
34
|
+
- Gemfile
|
35
|
+
- Guardfile
|
36
|
+
- History.txt
|
37
|
+
- README.md
|
38
|
+
- Rakefile
|
39
|
+
- endeco.gemspec
|
40
|
+
- lib/endeco.rb
|
41
|
+
- lib/endeco/cache.rb
|
42
|
+
- lib/endeco/config.rb
|
43
|
+
- lib/endeco/rails.rb
|
44
|
+
- lib/endeco/version.rb
|
45
|
+
- spec/lib/endeco_spec.rb
|
46
|
+
- spec/spec_helper.rb
|
47
|
+
homepage: ''
|
48
|
+
licenses: []
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ! '>='
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ! '>='
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
requirements: []
|
66
|
+
rubyforge_project: endeco
|
67
|
+
rubygems_version: 1.8.11
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: Environment Dependent Configuration
|
71
|
+
test_files:
|
72
|
+
- spec/lib/endeco_spec.rb
|
73
|
+
- spec/spec_helper.rb
|