localconfig 0.2.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.
data/.yardopts ADDED
@@ -0,0 +1 @@
1
+ --markup markdown
data/README.md ADDED
@@ -0,0 +1,94 @@
1
+ []: {{{1
2
+
3
+ File : README.md
4
+ Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
+ Date : 2013-09-01
6
+
7
+ Copyright : Copyright (C) 2013 Felix C. Stegerman
8
+ Version : v0.2.0
9
+
10
+ []: }}}1
11
+
12
+ ## Description
13
+ []: {{{1
14
+
15
+ [rb-]localconfig - local configuration for ruby (web) apps
16
+
17
+ localconfig makes it easy to load (additional) configuration files
18
+ from `~/.apps/<name>`, where name is determined by the current
19
+ directory. You can easily require ruby files and load json and yaml
20
+ files from this directory.
21
+
22
+ Additionally, it allows rails applications to easily define
23
+ admin:exists and admin:create rake tasks to make it easier to
24
+ automate application setup.
25
+
26
+ Just about everything is configurable: see the docs.
27
+
28
+ For an example w/ rails, see
29
+ https://github.com/obfusk/localconfig-rails-example.
30
+
31
+ []: }}}1
32
+
33
+ ## Examples
34
+ []: {{{1
35
+
36
+ Gemfile
37
+
38
+ ```
39
+ gem 'localconfig', require: 'localconfig/rails'
40
+ ```
41
+
42
+ config/localconfig.rb
43
+
44
+ ```ruby
45
+ LocalConfig['rails'].configure do |c|
46
+ puts "env: #{c.env}, #{Rails.env}"
47
+ c.require 'init.rb'
48
+ c.load_json 'pg.json'
49
+ c.on_admin_exists do |username|
50
+ # ...
51
+ end
52
+ c.on_admin_create do |username, password, email|
53
+ # ...
54
+ end
55
+ end
56
+ ```
57
+
58
+ []: }}}1
59
+
60
+ ## Specs & Docs
61
+ []: {{{1
62
+
63
+ $ rake spec
64
+ $ rake docs
65
+
66
+ []: }}}1
67
+
68
+ ## TODO
69
+ []: {{{1
70
+
71
+ * more specs/docs?
72
+ * ...
73
+
74
+ []: }}}1
75
+
76
+ ## License
77
+ []: {{{1
78
+
79
+ GPLv2 [1] or EPLv1 [2].
80
+
81
+ []: }}}1
82
+
83
+ ## References
84
+ []: {{{1
85
+
86
+ [1] GNU General Public License, version 2
87
+ --- http://www.opensource.org/licenses/GPL-2.0
88
+
89
+ [2] Eclipse Public License, version 1
90
+ --- http://www.opensource.org/licenses/EPL-1.0
91
+
92
+ []: }}}1
93
+
94
+ []: ! ( vim: set tw=70 sw=2 sts=2 et fdm=marker : )
data/Rakefile ADDED
@@ -0,0 +1,59 @@
1
+ desc 'Run specs'
2
+ task :spec do
3
+ sh 'rspec -c'
4
+ end
5
+
6
+ desc 'Run specs verbosely'
7
+ task 'spec:verbose' do
8
+ sh 'rspec -cfd'
9
+ end
10
+
11
+ desc 'Run specs verbosely, view w/ less'
12
+ task 'spec:less' do
13
+ sh 'rspec -cfd --tty | less -R'
14
+ end
15
+
16
+ desc 'Check for warnings'
17
+ task :warn do
18
+ reqs = %w{ config }.map { |x| "-r localconfig/#{x}" } * ' '
19
+ sh "ruby -w -I lib #{reqs} -e ''"
20
+ end
21
+
22
+ desc 'Check for warnings in specs'
23
+ task 'warn:spec' do
24
+ reqs = Dir['spec/**/*.rb'].sort.map { |x| "-r ./#{x}" } * ' '
25
+ sh "ruby -w -I lib -r rspec #{reqs} -e ''"
26
+ end
27
+
28
+ desc 'Check for warnings in specs (but not void context)'
29
+ task 'warn:spec:novoid' do
30
+ sh 'rake warn:spec 2>&1 | grep -v "void context"'
31
+ end
32
+
33
+ desc 'Generate docs'
34
+ task :docs do
35
+ sh 'yardoc | cat'
36
+ end
37
+
38
+ desc 'List undocumented objects'
39
+ task 'docs:undoc' do
40
+ sh 'yard stats --list-undoc'
41
+ end
42
+
43
+ desc 'Cleanup'
44
+ task :clean do
45
+ sh 'rm -rf .yardoc/ doc/ *.gem'
46
+ end
47
+
48
+ desc 'Build SNAPSHOT gem'
49
+ task :snapshot do
50
+ v = Time.new.strftime '%Y%m%d%H%M%S'
51
+ f = 'lib/localconfig/version.rb'
52
+ sh "sed -ri~ 's!(SNAPSHOT)!\\1.#{v}!' #{f}"
53
+ sh 'gem build localconfig.gemspec'
54
+ end
55
+
56
+ desc 'Undo SNAPSHOT gem'
57
+ task 'snapshot:undo' do
58
+ sh 'git checkout -- lib/localconfig/version.rb'
59
+ end
@@ -0,0 +1,56 @@
1
+ # -- ; {{{1
2
+ #
3
+ # File : localconfig/admin.rb
4
+ # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
+ # Date : 2013-09-01
6
+ #
7
+ # Copyright : Copyright (C) 2013 Felix C. Stegerman
8
+ # Licence : GPLv2 or EPLv1
9
+ #
10
+ # -- ; }}}1
11
+
12
+ require 'localconfig/config'
13
+
14
+ # namespace
15
+ module LocalConfig
16
+
17
+ class Config # {{{1
18
+
19
+ # set to true to disable rake tasks in railtie
20
+ attr_accessor :no_rake
21
+
22
+ # block will be run on admin_exists
23
+ def on_admin_exists(&b)
24
+ @admin_exists = b
25
+ end
26
+
27
+ # block will be run on admin_create
28
+ def on_admin_create(&b)
29
+ @admin_create = b
30
+ end
31
+
32
+ # run block set w/ on_admin_exists
33
+ def admin_exists(username)
34
+ @admin_exists[username]
35
+ end
36
+
37
+ # run block set w/ on_admin_create
38
+ def admin_create(username, password, email)
39
+ @admin_create[username, password, email]
40
+ end
41
+
42
+ # run admin_exists w/ $USERNAME
43
+ def admin_exists_from_env
44
+ admin_exists ENV['USERNAME']
45
+ end
46
+
47
+ # run admin_create w/ $USERNAME, $PASSWORD, $EMAIL
48
+ def admin_create_from_env
49
+ admin_create ENV['USERNAME'], ENV['PASSWORD'], ENV['EMAIL']
50
+ end
51
+
52
+ end # }}}1
53
+
54
+ end
55
+
56
+ # vim: set tw=70 sw=2 sts=2 et fdm=marker :
@@ -0,0 +1,148 @@
1
+ # -- ; {{{1
2
+ #
3
+ # File : localconfig/config.rb
4
+ # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
+ # Date : 2013-09-01
6
+ #
7
+ # Copyright : Copyright (C) 2013 Felix C. Stegerman
8
+ # Licence : GPLv2 or EPLv1
9
+ #
10
+ # -- ; }}}1
11
+
12
+ require 'hashie/mash'
13
+ require 'json'
14
+ require 'yaml'
15
+
16
+ # namespace
17
+ module LocalConfig
18
+
19
+ CONFIGS = {}
20
+
21
+ # --
22
+
23
+ # configuration
24
+ class Config # {{{1
25
+
26
+ # dir
27
+ attr_accessor :dir
28
+
29
+ # name
30
+ attr_accessor :name
31
+
32
+ # set dir to ~/.apps, derive name
33
+ def initialize(opts = {})
34
+ @config = {}
35
+ @dir = opts[:dir] || "#{Dir.home}/.apps"
36
+ @name = opts[:name] || derive_name
37
+ end
38
+
39
+ # configure; self is passed to the block
40
+ # @return self
41
+ def configure(&b)
42
+ b[self]; self
43
+ end
44
+
45
+ # derive name
46
+ # @option opts [String] :dir (Dir.pwd)
47
+ # @option opts [<String>] :up (%w{ app })
48
+ # @option opts [String] :env (ENV['LOCALCONFIG_NAME'])
49
+ # @return [String] env (if not empty) or basename of dir; if
50
+ # basename of dir in up, uses one dir up
51
+ def derive_name(opts = {}) # {{{2
52
+ dir = opts[:dir] || Dir.pwd; up = opts[:up] || %w{ app }
53
+ env = opts.fetch(:env) { ENV['LOCALCONFIG_NAME'] }
54
+ if env && !env.empty?
55
+ env
56
+ else
57
+ d, b = File.split dir; up.include?(b) ? File.basename(d) : b
58
+ end
59
+ end # }}}2
60
+
61
+ # `<dir>/<name>/...`
62
+ def path(*paths)
63
+ ([dir,name] + paths)*'/'
64
+ end
65
+
66
+ # glob in path
67
+ def glob(*args, &b)
68
+ Dir.chdir(path) { Dir.glob(*args, &b) }
69
+ end
70
+
71
+ # require relative to path
72
+ def require(*files)
73
+ _files(files).map { |f| Kernel.require f }
74
+ end
75
+
76
+ # load json file relative to path and store as Hashie::Mash in
77
+ # self.<basename>
78
+ def load_json(*files)
79
+ _load '.json', (-> x { JSON.parse x }), files
80
+ end
81
+
82
+ # load yaml file relative to path and store as Hashie::Mash in
83
+ # self.<basename>
84
+ def load_yaml(*files)
85
+ _load '.yaml', (-> x { YAML.load x }), files
86
+ end
87
+
88
+ # --
89
+
90
+ # LocalConfig.branch
91
+ def branch; LocalConfig.branch; end
92
+
93
+ # LocalConfig.env
94
+ def env; LocalConfig.env; end
95
+
96
+ # --
97
+
98
+ # files relative to path
99
+ def _files(files)
100
+ files.map { |f| "#{path}/#{f}" }
101
+ end
102
+
103
+ # load file relative to path and store as Hashie::Mash in
104
+ # self.<basename>
105
+ def _load(ext, parse, files) # {{{2
106
+ _files(files).each do |f|
107
+ b = File.basename f, ext
108
+ raise "@config[#{b}] already defined" if @config[b]
109
+ @config[b] = Hashie::Mash.new parse[File.read(f)]
110
+ define_singleton_method(b) { @config[b] }
111
+ end
112
+ end # }}}2
113
+
114
+ end # }}}1
115
+
116
+ # --
117
+
118
+ # get (new) Config by name
119
+ def self.[](name)
120
+ CONFIGS[name] ||= Config.new
121
+ end
122
+
123
+ # either the current git branch, '(HEAD)' for a detached head, or
124
+ # nil if not in a git repo; (cached);
125
+ #
126
+ # to use this in a Gemfile, you will need to have the localconfig
127
+ # gem installed locally before running bundle
128
+ def self.branch
129
+ @branch ||= _branch
130
+ end
131
+
132
+ # like Rails.env; (cached)
133
+ def self.env
134
+ @env ||= ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
135
+ end
136
+
137
+ # --
138
+
139
+ # current git branch
140
+ def self._branch
141
+ c = 'git symbolic-ref -q HEAD 2>/dev/null'
142
+ b = %x[#{c}].chomp.sub %r{^refs/heads/}, ''
143
+ [0,1].include?($?.exitstatus) ? (b.empty? ? '(HEAD)' : b) : nil
144
+ end
145
+
146
+ end
147
+
148
+ # vim: set tw=70 sw=2 sts=2 et fdm=marker :
@@ -0,0 +1,29 @@
1
+ # -- ; {{{1
2
+ #
3
+ # File : localconfig/rails.rb
4
+ # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
+ # Date : 2013-09-01
6
+ #
7
+ # Copyright : Copyright (C) 2013 Felix C. Stegerman
8
+ # Licence : GPLv2 or EPLv1
9
+ #
10
+ # -- ; }}}1
11
+
12
+ require 'rails'
13
+
14
+ require 'localconfig'
15
+
16
+ # namespace
17
+ module LocalConfig
18
+
19
+ # railtie that sets config.local to LocalConfig['rails'] and adds
20
+ # the rake tasks
21
+ class Railtie < Rails::Railtie
22
+ config.local = LocalConfig['rails']
23
+ rake_tasks { LocalConfig::Rake.define_tasks } \
24
+ unless LocalConfig['rails'].no_rake
25
+ end
26
+
27
+ end
28
+
29
+ # vim: set tw=70 sw=2 sts=2 et fdm=marker :
@@ -0,0 +1,47 @@
1
+ # -- ; {{{1
2
+ #
3
+ # File : localconfig/rake.rb
4
+ # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
+ # Date : 2013-09-01
6
+ #
7
+ # Copyright : Copyright (C) 2013 Felix C. Stegerman
8
+ # Licence : GPLv2 or EPLv1
9
+ #
10
+ # -- ; }}}1
11
+
12
+ require 'rake/dsl_definition'
13
+
14
+ require 'localconfig/admin'
15
+
16
+ # namespace
17
+ module LocalConfig
18
+
19
+ # rake tasks
20
+ module Rake
21
+ extend ::Rake::DSL
22
+
23
+ # define rake tasks `<namespace>:{exists,create}` that use
24
+ # `LocalConfig[name].admin_{exists,create}_from_env`
25
+ #
26
+ # @option opts [String] :name ('rails')
27
+ # @option opts [String] :namespace ('admin')
28
+ def self.define_tasks(opts = {})
29
+ name = opts[:name] || 'rails'
30
+ ns = opts[:namespace] || 'admin'
31
+
32
+ desc 'exit w/ status 2 if the admin user does not exist'
33
+ task "#{ns}:exists" => :environment do
34
+ exit 2 unless LocalConfig[name].admin_exists_from_env
35
+ end
36
+
37
+ desc 'create the admin user'
38
+ task "#{ns}:create" => :environment do
39
+ LocalConfig[name].admin_create_from_env
40
+ end
41
+ end
42
+
43
+ end
44
+
45
+ end
46
+
47
+ # vim: set tw=70 sw=2 sts=2 et fdm=marker :
@@ -0,0 +1,4 @@
1
+ module LocalConfig
2
+ VERSION = '0.2.0'
3
+ DATE = '2013-09-01'
4
+ end
@@ -0,0 +1,15 @@
1
+ # -- ; {{{1
2
+ #
3
+ # File : localconfig.rb
4
+ # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
+ # Date : 2013-09-01
6
+ #
7
+ # Copyright : Copyright (C) 2013 Felix C. Stegerman
8
+ # Licence : GPLv2 or EPLv1
9
+ #
10
+ # -- ; }}}1
11
+
12
+ require 'localconfig/rake'
13
+ require './config/localconfig'
14
+
15
+ # vim: set tw=70 sw=2 sts=2 et fdm=marker :
@@ -0,0 +1,32 @@
1
+ require File.expand_path('../lib/localconfig/version', __FILE__)
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'localconfig'
5
+ s.homepage = 'https://github.com/obfusk/rb-localconfig'
6
+ s.summary = 'local configuration for ruby (web) apps'
7
+
8
+ s.description = <<-END.gsub(/^ {4}/, '')
9
+ local configuration for ruby (web) apps
10
+
11
+ ...
12
+ END
13
+
14
+ s.version = LocalConfig::VERSION
15
+ s.date = LocalConfig::DATE
16
+
17
+ s.authors = [ 'Felix C. Stegerman' ]
18
+ s.email = %w{ flx@obfusk.net }
19
+
20
+ s.licenses = %w{ GPLv2 EPLv1 }
21
+
22
+ s.files = %w{ .yardopts README.md Rakefile } \
23
+ + %w{ localconfig.gemspec } \
24
+ + Dir['{lib,spec}/**/*.rb']
25
+
26
+ s.add_runtime_dependency 'hashie'
27
+ s.add_runtime_dependency 'rake'
28
+
29
+ s.add_development_dependency 'rspec'
30
+
31
+ s.required_ruby_version = '>= 1.9.1'
32
+ end
@@ -0,0 +1,98 @@
1
+ # -- ; {{{1
2
+ #
3
+ # File : localconfig/config_spec.rb
4
+ # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
+ # Date : 2013-09-01
6
+ #
7
+ # Copyright : Copyright (C) 2013 Felix C. Stegerman
8
+ # Licence : GPLv2 or EPLv1
9
+ #
10
+ # -- ; }}}1
11
+
12
+ require 'localconfig/config'
13
+
14
+ lcc = LocalConfig::Config
15
+
16
+ describe lcc do
17
+
18
+ apps = "#{Dir.pwd}/test/apps"
19
+ d1 = "#{apps}/test1"
20
+ d2 = "#{apps}/test2"
21
+ d3 = "#{apps}/test3"
22
+ test1 = Dir.chdir('test/test1') { lcc.new dir: apps }
23
+ test2 = Dir.chdir('test/test2/app') { lcc.new dir: apps }
24
+ test3 = Dir.chdir('test/test1') do
25
+ old = ENV['LOCALCONFIG_NAME']; ENV['LOCALCONFIG_NAME'] = 'test3'
26
+ x = lcc.new dir: apps; ENV['LOCALCONFIG_NAME'] = old; x
27
+ end
28
+
29
+ context 'dir, name, path' do # {{{1
30
+ it 'test1 is OK' do
31
+ expect(test1.dir).to eq apps
32
+ expect(test1.name).to eq 'test1'
33
+ expect(test1.path).to eq d1
34
+ end
35
+ it 'test2 is OK' do
36
+ expect(test2.dir).to eq apps
37
+ expect(test2.name).to eq 'test2'
38
+ expect(test2.path).to eq d2
39
+ end
40
+ it 'test3 is OK' do
41
+ expect(test3.dir).to eq apps
42
+ expect(test3.name).to eq 'test3'
43
+ expect(test3.path).to eq d3
44
+ end
45
+ end # }}}1
46
+
47
+ context 'configure' do # {{{1
48
+ it 'is passed self and returns self' do
49
+ x = nil
50
+ y = test1.configure { |c| x = c }
51
+ expect(x).to be test1
52
+ expect(y).to be test1
53
+ end
54
+ end # }}}1
55
+
56
+ context 'path' do # {{{1
57
+ it 'joins' do
58
+ expect(test1.path 'foo', 'bar').to eq "#{d1}/foo/bar"
59
+ end
60
+ end # }}}1
61
+
62
+ context 'glob' do # {{{1
63
+ it '* -> init.rb pg.json x.yaml' do
64
+ expect(test1.glob('*').sort).to eq \
65
+ %w{ init.rb pg.json x.yaml }
66
+ end
67
+ it '*.json -> pg.json' do
68
+ expect(test1.glob('*.json').sort).to eq %w{ pg.json }
69
+ end
70
+ end # }}}1
71
+
72
+ context 'require' do # {{{1
73
+ it 'requires init.rb' do
74
+ old = $stdout; $stdout = out = StringIO.open('', 'w')
75
+ begin test1.require 'init.rb' rescue $stdout = old end
76
+ expect(out.string).to eq "init!\n"
77
+ end
78
+ end # }}}1
79
+
80
+ context 'load_json' do # {{{1
81
+ it 'loads pg.json' do
82
+ x = test1.dup; x.load_json 'pg.json'
83
+ expect(x.pg.to_hash).to eq \
84
+ ({ 'database' => 'foo', 'username' => 'foo',
85
+ 'password' => 'bar' })
86
+ end
87
+ end # }}}1
88
+
89
+ context 'load_yaml' do # {{{1
90
+ it 'loads x.yaml' do
91
+ x = test1.dup; x.load_yaml 'x.yaml'
92
+ expect(x.x.to_hash).to eq({ 'foo' => 99, 'bar' => 'hi!' })
93
+ end
94
+ end # }}}1
95
+
96
+ end
97
+
98
+ # vim: set tw=70 sw=2 sts=2 et fdm=marker :
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: localconfig
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Felix C. Stegerman
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-09-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: hashie
16
+ requirement: &19213920 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *19213920
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &19212440 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *19212440
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &19209980 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *19209980
47
+ description: ! 'local configuration for ruby (web) apps
48
+
49
+
50
+ ...
51
+
52
+ '
53
+ email:
54
+ - flx@obfusk.net
55
+ executables: []
56
+ extensions: []
57
+ extra_rdoc_files: []
58
+ files:
59
+ - .yardopts
60
+ - README.md
61
+ - Rakefile
62
+ - localconfig.gemspec
63
+ - lib/localconfig/rake.rb
64
+ - lib/localconfig/version.rb
65
+ - lib/localconfig/rails.rb
66
+ - lib/localconfig/config.rb
67
+ - lib/localconfig/admin.rb
68
+ - lib/localconfig.rb
69
+ - spec/localconfig/config_spec.rb
70
+ homepage: https://github.com/obfusk/rb-localconfig
71
+ licenses:
72
+ - GPLv2
73
+ - EPLv1
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: 1.9.1
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 1.8.11
93
+ signing_key:
94
+ specification_version: 3
95
+ summary: local configuration for ruby (web) apps
96
+ test_files: []
97
+ has_rdoc: