Class: LocalConfig::Config
- Inherits:
-
Object
- Object
- LocalConfig::Config
- Defined in:
- lib/localconfig/admin.rb,
lib/localconfig/config.rb
Overview
configuration
Defined Under Namespace
Classes: RunError
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.
-
#_files(files) ⇒ Object
files relative to path.
-
#_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.
-
#env ⇒ Object
LocalConfig.env.
-
#git_repo(path, url, opts = {}) ⇒ Object
<!-- {{{1 -->.
-
#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 |
# File 'lib/localconfig/config.rb', line 36 def initialize(opts = {}) @config = {} @dir = opts[:dir] || "#{Dir.home}/.apps" @name = opts[:name] || derive_name 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
43 44 45 |
# File 'lib/localconfig/config.rb', line 43 def [](k) @config[k.to_s] end |
#_files(files) ⇒ Object
files relative to path
180 181 182 |
# File 'lib/localconfig/config.rb', line 180 def _files(files) files.map { |f| { f: f, path: "#{path}/#{f}" } } end |
#_load(exts, parse, files) ⇒ Object
load file relative to path and store as Hashie::Mash in self
185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/localconfig/config.rb', line 185 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 = o[x] ||= Hashie::Mash.new } raise "self.#{(pre+[k])*'.'} already set" if o[k] o[k] = Hashie::Mash.new parse[File.read(f[:path])] define_singleton_method(c_k) { @config[c_k] } \ unless self.respond_to? c_k end nil end |
#_sys(cmd, *args) ⇒ Object
run!
201 202 203 204 |
# File 'lib/localconfig/config.rb', line 201 def _sys(cmd, *args) system([cmd, cmd], *args) or \ raise RunError, "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
172 |
# File 'lib/localconfig/config.rb', line 172 def branch; LocalConfig.branch; end |
#configure(&b) ⇒ Object
configure; self is passed to the block
49 50 51 |
# File 'lib/localconfig/config.rb', line 49 def configure(&b) b[self]; self end |
#derive_name(opts = {}) ⇒ String
derive name
59 60 61 62 63 64 65 66 67 |
# File 'lib/localconfig/config.rb', line 59 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 |
#env ⇒ Object
LocalConfig.env
175 |
# File 'lib/localconfig/config.rb', line 175 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'
.
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/localconfig/config.rb', line 149 def git_repo(path, url, opts = {}) # {{{1 q = opts.fetch(:quiet, true) ? %w{ --quiet } : [] 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 _sys *(%w{ git fetch --force --tags } + q) \ unless rev && %x[ git rev-parse HEAD ] == %x[ git rev-parse --revs-only #{rev}^0 -- ] end else _sys *(%w{ git clone } + q + [url, dest]) end Dir.chdir(dest) do _sys *(%w{ git reset --hard } + q + [ref] + %w{ -- }) end end |
#glob(*args, &b) ⇒ Object
glob in path
75 76 77 |
# File 'lib/localconfig/config.rb', line 75 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
118 119 120 121 122 123 |
# File 'lib/localconfig/config.rb', line 118 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
94 95 96 |
# File 'lib/localconfig/config.rb', line 94 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
107 108 109 |
# File 'lib/localconfig/config.rb', line 107 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/...
70 71 72 |
# File 'lib/localconfig/config.rb', line 70 def path(*paths) ([dir,name] + paths)*'/' end |
#require(*files) ⇒ Object
require relative to path
80 81 82 83 |
# File 'lib/localconfig/config.rb', line 80 def require(*files) _files(files).each { |f| Kernel.require f[:path] } nil end |