Class: LocalConfig::Config
- Inherits:
-
Object
- Object
- LocalConfig::Config
- Defined in:
- lib/localconfig/admin.rb,
lib/localconfig/config.rb
Overview
configuration
Defined Under Namespace
Classes: Error
Instance Attribute Summary collapse
-
#dir ⇒ Object
dir.
-
#name ⇒ Object
name.
-
#no_rake ⇒ Object
set to true to disable rake tasks in railtie.
Instance Method Summary collapse
-
#[](k) ⇒ Object
access setting by key.
- #_dup ⇒ Object
-
#_files(files) ⇒ Object
files relative to path.
-
#_git_dir_check ⇒ Object
check for .git/.
-
#_git_rev(rev) ⇒ Object
get hash for revision or tag.
-
#_load(exts, parse, files) ⇒ Object
load file relative to path and store as Hashie::Mash in self.
-
#_sys(cmd, *args) ⇒ Object
run!.
-
#admin_create(username, password, email) ⇒ Object
run block set w/ on_admin_create.
-
#admin_create_from_env ⇒ Object
run admin_create w/ $USERNAME, $PASSWORD, $EMAIL.
-
#admin_exists(username) ⇒ Object
run block set w/ on_admin_exists.
-
#admin_exists_from_env ⇒ Object
run admin_exists w/ $USERNAME.
-
#branch ⇒ Object
LocalConfig.branch.
-
#configure(&b) ⇒ Object
configure; self is passed to the block.
-
#derive_name(opts = {}) ⇒ String
derive name.
-
#dup ⇒ Object
deep duplicate.
-
#env ⇒ Object
LocalConfig.env.
-
#git_repo(path, url, opts = {}) ⇒ Object
<!-- {{{2 -->.
-
#glob(*args, &b) ⇒ Object
glob in path.
-
#initialize(opts = {}) ⇒ Config
constructor
set dir to ~/.apps, derive name.
-
#load_dir(*dirs) ⇒ Object
load_{json,yaml}
*.json
,*.y{a,}ml
in dir; for example:. -
#load_json(*files) ⇒ Object
load json file relative to path and store as Hashie::Mash in self; for example:.
-
#load_yaml(*files) ⇒ Object
load yaml file relative to path and store as Hashie::Mash in self; for example:.
-
#on_admin_create(&b) ⇒ Object
block will be run on admin_create.
-
#on_admin_exists(&b) ⇒ Object
block will be run on admin_exists.
-
#path(*paths) ⇒ Object
dir/name/...
. -
#require(*files) ⇒ Object
require relative to path.
Constructor Details
#initialize(opts = {}) ⇒ Config
set dir to ~/.apps, derive name
36 37 38 39 40 41 42 43 |
# File 'lib/localconfig/config.rb', line 36 def initialize(opts = {}) # use Mash: has deep .dup @config = Hashie::Mash.new(opts[:config] || {}) @dir = opts[:dir] || "#{Dir.home}/.apps" @name = opts[:name] || derive_name @getters = @config.keys.select { |x| !respond_to?(x) } @getters.each { |x| define_singleton_method(x) { @config[x] } } end |
Instance Attribute Details
#dir ⇒ Object
dir
30 31 32 |
# File 'lib/localconfig/config.rb', line 30 def dir @dir end |
#name ⇒ Object
name
33 34 35 |
# File 'lib/localconfig/config.rb', line 33 def name @name end |
#no_rake ⇒ Object
set to true to disable rake tasks in railtie
20 21 22 |
# File 'lib/localconfig/admin.rb', line 20 def no_rake @no_rake end |
Instance Method Details
#[](k) ⇒ Object
access setting by key
53 54 55 |
# File 'lib/localconfig/config.rb', line 53 def [](k) @config[k.to_s] end |
#_dup ⇒ Object
45 |
# File 'lib/localconfig/config.rb', line 45 alias_method :_dup, :dup |
#_files(files) ⇒ Object
files relative to path
197 198 199 |
# File 'lib/localconfig/config.rb', line 197 def _files(files) files.map { |f| { f: f, path: "#{path}/#{f}" } } end |
#_git_dir_check ⇒ Object
check for .git/
220 221 222 |
# File 'lib/localconfig/config.rb', line 220 def _git_dir_check File.exist? '.git/' or raise Error, 'not a git dir' end |
#_git_rev(rev) ⇒ Object
get hash for revision or tag
231 232 233 234 |
# File 'lib/localconfig/config.rb', line 231 def _git_rev(rev) hash = %x[ git rev-parse --revs-only #{rev}^0 -- 2>/dev/null ] $?.success? ? hash.chomp : nil end |
#_load(exts, parse, files) ⇒ Object
load file relative to path and store as Hashie::Mash in self
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/localconfig/config.rb', line 202 def _load(exts, parse, files) # {{{2 _files(files).each do |f| *pre, b = all = Pathname.new(f[:f]).each_filename.to_a ext = exts.find { |x| b.end_with? x } || '' k = File.basename b, ext ; c_k = (pre+[k]).first o = @config pre.each { |x| o[x] ||= Hashie::Mash.new; o = o[x] } raise "self.#{(pre+[k])*'.'} already set" if o[k] o[k] = Hashie::Mash.new parse[File.read(f[:path])] unless respond_to? c_k define_singleton_method(c_k) { @config[c_k] } @getters << c_k end end nil end |
#_sys(cmd, *args) ⇒ Object
run!
225 226 227 228 |
# File 'lib/localconfig/config.rb', line 225 def _sys(cmd, *args) system([cmd, cmd], *args) or \ raise Error, "failed to run command #{[cmd]+args} (#$?)" end |
#admin_create(username, password, email) ⇒ Object
run block set w/ on_admin_create
38 39 40 |
# File 'lib/localconfig/admin.rb', line 38 def admin_create(username, password, email) @admin_create[username, password, email] end |
#admin_create_from_env ⇒ Object
run admin_create w/ $USERNAME, $PASSWORD, $EMAIL
48 49 50 |
# File 'lib/localconfig/admin.rb', line 48 def admin_create_from_env admin_create ENV['USERNAME'], ENV['PASSWORD'], ENV['EMAIL'] end |
#admin_exists(username) ⇒ Object
run block set w/ on_admin_exists
33 34 35 |
# File 'lib/localconfig/admin.rb', line 33 def admin_exists(username) @admin_exists[username] end |
#admin_exists_from_env ⇒ Object
run admin_exists w/ $USERNAME
43 44 45 |
# File 'lib/localconfig/admin.rb', line 43 def admin_exists_from_env admin_exists ENV['USERNAME'] end |
#branch ⇒ Object
LocalConfig.branch
189 |
# File 'lib/localconfig/config.rb', line 189 def branch; LocalConfig.branch; end |
#configure(&b) ⇒ Object
configure; self is passed to the block
59 60 61 |
# File 'lib/localconfig/config.rb', line 59 def configure(&b) b[self]; self end |
#derive_name(opts = {}) ⇒ String
derive name
69 70 71 72 73 74 75 76 77 |
# File 'lib/localconfig/config.rb', line 69 def derive_name(opts = {}) # {{{2 dir = opts[:dir] || Dir.pwd; up = opts[:up] || %w{ app } env = opts.fetch(:env) { ENV['LOCALCONFIG_NAME'] } if env && !env.empty? env else d, b = File.split dir; up.include?(b) ? File.basename(d) : b end end |
#dup ⇒ Object
deep duplicate
48 49 50 |
# File 'lib/localconfig/config.rb', line 48 def dup self.class.new config: @config, dir: @dir.dup, name: @name.dup end |
#env ⇒ Object
LocalConfig.env
192 |
# File 'lib/localconfig/config.rb', line 192 def env; LocalConfig.env; end |
#git_repo(path, url, opts = {}) ⇒ Object
clone/fetch git repo in dir (to load more config files from elsewhere); for example:
lc.load_yaml 'git.yml' # repo:, branch:
lc.git_repo 'more', c.git.repo, branch: c.git.branch
lc.load_dir 'more' # more/foo.yml, more/bar.json
You can't use more than one of :rev
, :tag
, :branch
; if you
specify none, the default is branch: 'master'
.
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/localconfig/config.rb', line 159 def git_repo(path, url, opts = {}) # {{{2 %w{ branch rev tag }.count { |x| opts.has_key? x.to_sym } <= 1 \ or raise ArgumentError, "You can't use more than one of :rev, :tag, :branch" q = opts.fetch(:quiet, true) ? %w{ --quiet } : [] s = opts.fetch(:quiet, true) ? -> *a { _sys *a } : -> *a { puts "$ #{a*' '}"; _sys *a } b = opts[:branch]; b = "origin/#{b}" if b && !b['/'] rev = opts[:rev] || opts[:tag] ref = rev || b || 'origin/master' dest = path path if File.exist? dest Dir.chdir(dest) do _git_dir_check s[*(%w{ git fetch --force --tags } + q)] \ unless rev && _git_rev('HEAD') == _git_rev(rev) end else s[*(%w{ git clone } + q + [url, dest])] end Dir.chdir(dest) do _git_dir_check s[*(%w{ git reset --hard } + q + [ref] + %w{ -- })] end end |
#glob(*args, &b) ⇒ Object
glob in path
85 86 87 |
# File 'lib/localconfig/config.rb', line 85 def glob(*args, &b) Dir.chdir(path) { Dir.glob(*args, &b) } end |
#load_dir(*dirs) ⇒ Object
load_{json,yaml}
*.json
, *.y{a,}ml
in dir; for example:
lc.load_dir 'more' # more/foo.json, more/bar.yml
lc.more.foo.key1
lc.more..key2
128 129 130 131 132 133 |
# File 'lib/localconfig/config.rb', line 128 def load_dir(*dirs) dirs.flat_map do |d| j = glob "#{d}/*.json"; y = glob "#{d}/*.y{a,}ml" load_json *j; load_yaml *y; j + y end end |
#load_json(*files) ⇒ Object
load json file relative to path and store as Hashie::Mash in self; for example:
lc.load_json 'foo.json'
lc.foo.key1
lc.load_json 'bar/baz.json'
lc..baz.key2
104 105 106 |
# File 'lib/localconfig/config.rb', line 104 def load_json(*files) _load %w{ .json }, (-> x { JSON.parse x }), files end |
#load_yaml(*files) ⇒ Object
load yaml file relative to path and store as Hashie::Mash in self; for example:
lc.load_yaml 'foo.yaml'
lc.foo.key1
lc.load_yaml 'bar/baz.yml'
lc..baz.key2
117 118 119 |
# File 'lib/localconfig/config.rb', line 117 def load_yaml(*files) _load %w{ .yml .yaml }, (-> x { YAML.load x }), files end |
#on_admin_create(&b) ⇒ Object
block will be run on admin_create
28 29 30 |
# File 'lib/localconfig/admin.rb', line 28 def on_admin_create(&b) @admin_create = b end |
#on_admin_exists(&b) ⇒ Object
block will be run on admin_exists
23 24 25 |
# File 'lib/localconfig/admin.rb', line 23 def on_admin_exists(&b) @admin_exists = b end |
#path(*paths) ⇒ Object
dir/name/...
80 81 82 |
# File 'lib/localconfig/config.rb', line 80 def path(*paths) ([dir,name] + paths)*'/' end |
#require(*files) ⇒ Object
require relative to path
90 91 92 93 |
# File 'lib/localconfig/config.rb', line 90 def require(*files) _files(files).each { |f| Kernel.require f[:path] } nil end |