localconfig 0.2.0 → 0.3.0

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: 69720f49e2aaf298ba101c6cf4b62d19896ccabc
4
+ data.tar.gz: 08cdc94023f7e7c30c61d0b876c109c625806786
5
+ SHA512:
6
+ metadata.gz: 899ffc8ed6c89e921ba45511ef76a3f93e3bb166b819916f19e5ce71afb05502c9dd2d4eeda10da6797008bba7f1bd1907cae4cfef854a097bf436ed4f2e7dda
7
+ data.tar.gz: 5056aaf7eb5d76e79a57619a989ca3d47dc040a64e2959f35ce34972473f7e958dacb287a26ec1d8b81ae34eb938957bbc6d860df614762eca57f31743e1b9e9
data/README.md CHANGED
@@ -2,13 +2,15 @@
2
2
 
3
3
  File : README.md
4
4
  Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
- Date : 2013-09-01
5
+ Date : 2014-10-21
6
6
 
7
- Copyright : Copyright (C) 2013 Felix C. Stegerman
8
- Version : v0.2.0
7
+ Copyright : Copyright (C) 2014 Felix C. Stegerman
8
+ Version : v0.3.0
9
9
 
10
10
  []: }}}1
11
11
 
12
+ [![Gem Version](https://badge.fury.io/rb/localconfig.png)](https://rubygems.org/gems/localconfig)
13
+
12
14
  ## Description
13
15
  []: {{{1
14
16
 
@@ -20,7 +22,7 @@
20
22
  files from this directory.
21
23
 
22
24
  Additionally, it allows rails applications to easily define
23
- admin:exists and admin:create rake tasks to make it easier to
25
+ `admin:exists` and `admin:create` rake tasks to make it easier to
24
26
  automate application setup.
25
27
 
26
28
  Just about everything is configurable: see the docs.
@@ -33,19 +35,24 @@
33
35
  ## Examples
34
36
  []: {{{1
35
37
 
36
- Gemfile
37
-
38
- ```
38
+ ```ruby
39
+ # Gemfile
39
40
  gem 'localconfig', require: 'localconfig/rails'
40
41
  ```
41
42
 
42
- config/localconfig.rb
43
-
44
43
  ```ruby
44
+ # config/localconfig.rb
45
45
  LocalConfig['rails'].configure do |c|
46
46
  puts "env: #{c.env}, #{Rails.env}"
47
+
47
48
  c.require 'init.rb'
49
+
48
50
  c.load_json 'pg.json'
51
+ c.load_yaml 'git.yml' # repo:, branch:
52
+ c.git_repo 'more', c.git.repo, branch: c.git.branch
53
+ c.load_dir 'more' # more/foo.yml, more/bar.json
54
+ puts "more: #{c.more.foo.key1} #{c.more.bar.key2}"
55
+
49
56
  c.on_admin_exists do |username|
50
57
  # ...
51
58
  end
@@ -58,37 +65,24 @@ end
58
65
  []: }}}1
59
66
 
60
67
  ## Specs & Docs
61
- []: {{{1
62
68
 
63
- $ rake spec
64
- $ rake docs
65
-
66
- []: }}}1
69
+ ```bash
70
+ rake spec
71
+ rake docs
72
+ ```
67
73
 
68
74
  ## TODO
69
- []: {{{1
70
75
 
71
76
  * more specs/docs?
72
77
  * ...
73
78
 
74
- []: }}}1
75
-
76
79
  ## License
77
- []: {{{1
78
80
 
79
- GPLv2 [1] or EPLv1 [2].
80
-
81
- []: }}}1
81
+ LGPLv3+ [1].
82
82
 
83
83
  ## References
84
- []: {{{1
85
-
86
- [1] GNU General Public License, version 2
87
- --- http://www.opensource.org/licenses/GPL-2.0
88
84
 
89
- [2] Eclipse Public License, version 1
90
- --- http://www.opensource.org/licenses/EPL-1.0
91
-
92
- []: }}}1
85
+ [1] GNU Lesser General Public License, version 3
86
+ --- http://www.gnu.org/licenses/lgpl-3.0.html
93
87
 
94
88
  []: ! ( vim: set tw=70 sw=2 sts=2 et fdm=marker : )
data/lib/localconfig.rb CHANGED
@@ -2,10 +2,10 @@
2
2
  #
3
3
  # File : localconfig.rb
4
4
  # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
- # Date : 2013-09-01
5
+ # Date : 2014-10-21
6
6
  #
7
- # Copyright : Copyright (C) 2013 Felix C. Stegerman
8
- # Licence : GPLv2 or EPLv1
7
+ # Copyright : Copyright (C) 2014 Felix C. Stegerman
8
+ # Licence : LGPLv3+
9
9
  #
10
10
  # -- ; }}}1
11
11
 
@@ -2,10 +2,10 @@
2
2
  #
3
3
  # File : localconfig/admin.rb
4
4
  # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
- # Date : 2013-09-01
5
+ # Date : 2014-10-21
6
6
  #
7
- # Copyright : Copyright (C) 2013 Felix C. Stegerman
8
- # Licence : GPLv2 or EPLv1
7
+ # Copyright : Copyright (C) 2014 Felix C. Stegerman
8
+ # Licence : LGPLv3+
9
9
  #
10
10
  # -- ; }}}1
11
11
 
@@ -2,10 +2,10 @@
2
2
  #
3
3
  # File : localconfig/config.rb
4
4
  # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
- # Date : 2013-09-01
5
+ # Date : 2014-10-21
6
6
  #
7
- # Copyright : Copyright (C) 2013 Felix C. Stegerman
8
- # Licence : GPLv2 or EPLv1
7
+ # Copyright : Copyright (C) 2014 Felix C. Stegerman
8
+ # Licence : LGPLv3+
9
9
  #
10
10
  # -- ; }}}1
11
11
 
@@ -23,6 +23,9 @@ module LocalConfig
23
23
  # configuration
24
24
  class Config # {{{1
25
25
 
26
+ # something went wrong calling `system`
27
+ class RunError < StandardError; end
28
+
26
29
  # dir
27
30
  attr_accessor :dir
28
31
 
@@ -36,6 +39,11 @@ module LocalConfig
36
39
  @name = opts[:name] || derive_name
37
40
  end
38
41
 
42
+ # access setting by key
43
+ def [](k)
44
+ @config[k.to_s]
45
+ end
46
+
39
47
  # configure; self is passed to the block
40
48
  # @return self
41
49
  def configure(&b)
@@ -58,7 +66,7 @@ module LocalConfig
58
66
  end
59
67
  end # }}}2
60
68
 
61
- # `<dir>/<name>/...`
69
+ # `dir/name/...`
62
70
  def path(*paths)
63
71
  ([dir,name] + paths)*'/'
64
72
  end
@@ -70,21 +78,94 @@ module LocalConfig
70
78
 
71
79
  # require relative to path
72
80
  def require(*files)
73
- _files(files).map { |f| Kernel.require f }
81
+ _files(files).each { |f| Kernel.require f[:path] }
82
+ nil
74
83
  end
75
84
 
76
85
  # load json file relative to path and store as Hashie::Mash in
77
- # self.<basename>
86
+ # self; for example:
87
+ #
88
+ # ```
89
+ # lc.load_json 'foo.json'
90
+ # lc.foo.key1
91
+ # lc.load_json 'bar/baz.json'
92
+ # lc.bar.baz.key2
93
+ # ```
78
94
  def load_json(*files)
79
- _load '.json', (-> x { JSON.parse x }), files
95
+ _load %w{ .json }, (-> x { JSON.parse x }), files
80
96
  end
81
97
 
82
98
  # load yaml file relative to path and store as Hashie::Mash in
83
- # self.<basename>
99
+ # self; for example:
100
+ #
101
+ # ```
102
+ # lc.load_yaml 'foo.yaml'
103
+ # lc.foo.key1
104
+ # lc.load_yaml 'bar/baz.yml'
105
+ # lc.bar.baz.key2
106
+ # ```
84
107
  def load_yaml(*files)
85
- _load '.yaml', (-> x { YAML.load x }), files
108
+ _load %w{ .yml .yaml }, (-> x { YAML.load x }), files
109
+ end
110
+
111
+ # `load_{json,yaml}` `*.json`, `*.y{a,}ml` in dir; for example:
112
+ #
113
+ # ```
114
+ # lc.load_dir 'more' # more/foo.json, more/bar.yml
115
+ # lc.more.foo.key1
116
+ # lc.more.bar.key2
117
+ # ```
118
+ def load_dir(*dirs)
119
+ dirs.flat_map do |d|
120
+ j = glob "#{d}/*.json"; y = glob "#{d}/*.y{a,}ml"
121
+ load_json *j; load_yaml *y; j + y
122
+ end
86
123
  end
87
124
 
125
+ # <!-- {{{1 -->
126
+ #
127
+ # clone/fetch git repo in dir (to load more config files from
128
+ # elsewhere); for example:
129
+ #
130
+ # ```
131
+ # lc.load_yaml 'git.yml' # repo:, branch:
132
+ # lc.git_repo 'more', c.git.repo, branch: c.git.branch
133
+ # lc.load_dir 'more' # more/foo.yml, more/bar.json
134
+ # ```
135
+ #
136
+ # You can't use more than one of `:rev`, `:tag`, `:branch`; if you
137
+ # specify none, the default is `branch: 'master'`.
138
+ #
139
+ # @param [String] path subpath to clone to
140
+ # @param [String] url url to clone from
141
+ #
142
+ # @param [Hash] opts options
143
+ # @option opts [Bool] :quiet (true) whether to be quiet
144
+ # @option opts [String] :rev specific revision (SHA1)
145
+ # @option opts [String] :tag specific tag
146
+ # @option opts [String] :branch specific branch
147
+ #
148
+ # <!-- }}}1 -->
149
+ def git_repo(path, url, opts = {}) # {{{1
150
+ q = opts.fetch(:quiet, true) ? %w{ --quiet } : []
151
+ b = opts[:branch]; b = "origin/#{b}" if b && !b['/']
152
+ rev = opts[:rev] || opts[:tag]
153
+ ref = rev || b || 'origin/master'
154
+ dest = path path
155
+ if File.exist? dest
156
+ Dir.chdir(dest) do
157
+ _sys *(%w{ git fetch --force --tags } + q) \
158
+ unless rev && %x[ git rev-parse HEAD ] ==
159
+ %x[ git rev-parse --revs-only #{rev}^0 -- ]
160
+ end
161
+ else
162
+ _sys *(%w{ git clone } + q + [url, dest])
163
+ end
164
+ Dir.chdir(dest) do
165
+ _sys *(%w{ git reset --hard } + q + [ref] + %w{ -- })
166
+ end
167
+ end # }}}1
168
+
88
169
  # --
89
170
 
90
171
  # LocalConfig.branch
@@ -97,20 +178,31 @@ module LocalConfig
97
178
 
98
179
  # files relative to path
99
180
  def _files(files)
100
- files.map { |f| "#{path}/#{f}" }
181
+ files.map { |f| { f: f, path: "#{path}/#{f}" } }
101
182
  end
102
183
 
103
- # load file relative to path and store as Hashie::Mash in
104
- # self.<basename>
105
- def _load(ext, parse, files) # {{{2
184
+ # load file relative to path and store as Hashie::Mash in self
185
+ def _load(exts, parse, files) # {{{2
106
186
  _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] }
187
+ *pre, b = all = Pathname.new(f[:f]).each_filename.to_a
188
+ ext = exts.find { |x| b.end_with? x } || ''
189
+ k = File.basename b, ext ; c_k = (pre+[k]).first
190
+ o = @config
191
+ pre.each { |x| o = o[x] ||= Hashie::Mash.new }
192
+ raise "self.#{(pre+[k])*'.'} already set" if o[k]
193
+ o[k] = Hashie::Mash.new parse[File.read(f[:path])]
194
+ define_singleton_method(c_k) { @config[c_k] } \
195
+ unless self.respond_to? c_k
111
196
  end
197
+ nil
112
198
  end # }}}2
113
199
 
200
+ # run!
201
+ def _sys(cmd, *args)
202
+ system([cmd, cmd], *args) or \
203
+ raise RunError, "failed to run command #{[cmd]+args} (#$?)"
204
+ end
205
+
114
206
  end # }}}1
115
207
 
116
208
  # --
@@ -2,10 +2,10 @@
2
2
  #
3
3
  # File : localconfig/rails.rb
4
4
  # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
- # Date : 2013-09-01
5
+ # Date : 2014-10-21
6
6
  #
7
- # Copyright : Copyright (C) 2013 Felix C. Stegerman
8
- # Licence : GPLv2 or EPLv1
7
+ # Copyright : Copyright (C) 2014 Felix C. Stegerman
8
+ # Licence : LGPLv3+
9
9
  #
10
10
  # -- ; }}}1
11
11
 
@@ -2,10 +2,10 @@
2
2
  #
3
3
  # File : localconfig/rake.rb
4
4
  # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
- # Date : 2013-09-01
5
+ # Date : 2014-10-21
6
6
  #
7
- # Copyright : Copyright (C) 2013 Felix C. Stegerman
8
- # Licence : GPLv2 or EPLv1
7
+ # Copyright : Copyright (C) 2014 Felix C. Stegerman
8
+ # Licence : LGPLv3+
9
9
  #
10
10
  # -- ; }}}1
11
11
 
@@ -1,4 +1,4 @@
1
1
  module LocalConfig
2
- VERSION = '0.2.0'
3
- DATE = '2013-09-01'
2
+ VERSION = '0.3.0'
3
+ DATE = '2014-10-21'
4
4
  end
data/localconfig.gemspec CHANGED
@@ -17,7 +17,7 @@ Gem::Specification.new do |s|
17
17
  s.authors = [ 'Felix C. Stegerman' ]
18
18
  s.email = %w{ flx@obfusk.net }
19
19
 
20
- s.licenses = %w{ GPLv2 EPLv1 }
20
+ s.licenses = %w{ LGPLv3+ }
21
21
 
22
22
  s.files = %w{ .yardopts README.md Rakefile } \
23
23
  + %w{ localconfig.gemspec } \
@@ -2,10 +2,10 @@
2
2
  #
3
3
  # File : localconfig/config_spec.rb
4
4
  # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
- # Date : 2013-09-01
5
+ # Date : 2014-10-21
6
6
  #
7
- # Copyright : Copyright (C) 2013 Felix C. Stegerman
8
- # Licence : GPLv2 or EPLv1
7
+ # Copyright : Copyright (C) 2014 Felix C. Stegerman
8
+ # Licence : LGPLv3+
9
9
  #
10
10
  # -- ; }}}1
11
11
 
metadata CHANGED
@@ -1,97 +1,101 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: localconfig
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
5
- prerelease:
4
+ version: 0.3.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Felix C. Stegerman
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-09-01 00:00:00.000000000 Z
11
+ date: 2014-10-21 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: hashie
16
- requirement: &19213920 !ruby/object:Gem::Requirement
17
- none: false
15
+ requirement: !ruby/object:Gem::Requirement
18
16
  requirements:
19
- - - ! '>='
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
22
20
  type: :runtime
23
21
  prerelease: false
24
- version_requirements: *19213920
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
25
27
  - !ruby/object:Gem::Dependency
26
28
  name: rake
27
- requirement: &19212440 !ruby/object:Gem::Requirement
28
- none: false
29
+ requirement: !ruby/object:Gem::Requirement
29
30
  requirements:
30
- - - ! '>='
31
+ - - ">="
31
32
  - !ruby/object:Gem::Version
32
33
  version: '0'
33
34
  type: :runtime
34
35
  prerelease: false
35
- version_requirements: *19212440
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
36
41
  - !ruby/object:Gem::Dependency
37
42
  name: rspec
38
- requirement: &19209980 !ruby/object:Gem::Requirement
39
- none: false
43
+ requirement: !ruby/object:Gem::Requirement
40
44
  requirements:
41
- - - ! '>='
45
+ - - ">="
42
46
  - !ruby/object:Gem::Version
43
47
  version: '0'
44
48
  type: :development
45
49
  prerelease: false
46
- version_requirements: *19209980
47
- description: ! 'local configuration for ruby (web) apps
48
-
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: |
56
+ local configuration for ruby (web) apps
49
57
 
50
58
  ...
51
-
52
- '
53
59
  email:
54
60
  - flx@obfusk.net
55
61
  executables: []
56
62
  extensions: []
57
63
  extra_rdoc_files: []
58
64
  files:
59
- - .yardopts
65
+ - ".yardopts"
60
66
  - README.md
61
67
  - Rakefile
62
- - localconfig.gemspec
68
+ - lib/localconfig.rb
69
+ - lib/localconfig/admin.rb
70
+ - lib/localconfig/config.rb
71
+ - lib/localconfig/rails.rb
63
72
  - lib/localconfig/rake.rb
64
73
  - lib/localconfig/version.rb
65
- - lib/localconfig/rails.rb
66
- - lib/localconfig/config.rb
67
- - lib/localconfig/admin.rb
68
- - lib/localconfig.rb
74
+ - localconfig.gemspec
69
75
  - spec/localconfig/config_spec.rb
70
76
  homepage: https://github.com/obfusk/rb-localconfig
71
77
  licenses:
72
- - GPLv2
73
- - EPLv1
78
+ - LGPLv3+
79
+ metadata: {}
74
80
  post_install_message:
75
81
  rdoc_options: []
76
82
  require_paths:
77
83
  - lib
78
84
  required_ruby_version: !ruby/object:Gem::Requirement
79
- none: false
80
85
  requirements:
81
- - - ! '>='
86
+ - - ">="
82
87
  - !ruby/object:Gem::Version
83
88
  version: 1.9.1
84
89
  required_rubygems_version: !ruby/object:Gem::Requirement
85
- none: false
86
90
  requirements:
87
- - - ! '>='
91
+ - - ">="
88
92
  - !ruby/object:Gem::Version
89
93
  version: '0'
90
94
  requirements: []
91
95
  rubyforge_project:
92
- rubygems_version: 1.8.11
96
+ rubygems_version: 2.2.2
93
97
  signing_key:
94
- specification_version: 3
98
+ specification_version: 4
95
99
  summary: local configuration for ruby (web) apps
96
100
  test_files: []
97
101
  has_rdoc: