asetus 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.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/Gemfile +3 -0
- data/README.md +50 -0
- data/Rakefile +47 -0
- data/asetus.gemspec +16 -0
- data/lib/asetus/adapter/json.rb +27 -0
- data/lib/asetus/adapter/yaml.rb +27 -0
- data/lib/asetus/configstruct.rb +58 -0
- data/lib/asetus.rb +97 -0
- metadata +66 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: aabab562229de22b1301e26910b4a000a2086a15
|
|
4
|
+
data.tar.gz: b7fd09d5490ff4a84a3659cc653c02c4a70910ad
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: cbc9b3ab70bd09b13c4d99ef328bebe75c2515b01f41c1074e5180f16878fc221db0e2bcb70f7aafa7d41fbfcbd1398383184954acf357e4932c4a38bb4684aa
|
|
7
|
+
data.tar.gz: 79c8ece12fa2d9a32bb58a1e67ff7cbc7c838aff04355d661cb2220d6f29b7afe8c416ccc76dcf8bad8628e3c19f3cff5863df72ba870b4a62a54a02f9b55644
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# Asetus
|
|
2
|
+
Configuration library for ruby with YAML/JSON backends with unified object
|
|
3
|
+
access
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
```
|
|
7
|
+
% gem install asetus
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
## Use
|
|
11
|
+
### Simple
|
|
12
|
+
```
|
|
13
|
+
require 'asetus'
|
|
14
|
+
cfg = Asetus.cfg
|
|
15
|
+
port = cfg.server.port
|
|
16
|
+
user = cfg.auth.user
|
|
17
|
+
pw = cfg.auth.password
|
|
18
|
+
```
|
|
19
|
+
It tried to detect your software name via caller_locations if no ':name'
|
|
20
|
+
argument was given.
|
|
21
|
+
It automatically loads /etc/name/config and ~/.config/name/config and merges
|
|
22
|
+
them together.
|
|
23
|
+
|
|
24
|
+
### Advanced
|
|
25
|
+
```
|
|
26
|
+
require 'asetus'
|
|
27
|
+
asetus = Asetus.new name: 'mykewlapp',
|
|
28
|
+
default: {'poop'=>'xyzzy'},
|
|
29
|
+
adapter: 'yaml',
|
|
30
|
+
usrdir: '/home/app/config/',
|
|
31
|
+
sysdir: '/System/config/',
|
|
32
|
+
load: false
|
|
33
|
+
asetus.default.poop2 = [1, 2, 3, 4]
|
|
34
|
+
asetus.default.starship.poopoers = 42
|
|
35
|
+
asetus.load :user
|
|
36
|
+
if asetus.user.empty?
|
|
37
|
+
asetus.user = asetus.default
|
|
38
|
+
asetus.save :user
|
|
39
|
+
end
|
|
40
|
+
asetus.load # load+merges cfg, takes argument :default, :system, :user
|
|
41
|
+
asetus.cfg # merged default + system + user (merged on load)
|
|
42
|
+
asetus.default # default only
|
|
43
|
+
asetus.system # system only
|
|
44
|
+
asetus.user # user only
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## TODO
|
|
48
|
+
|
|
49
|
+
* should I add feature to raise on unconfigured/unset?
|
|
50
|
+
* should I always merge to 'cfg' when default/system/config is set?
|
data/Rakefile
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
begin
|
|
2
|
+
require 'rake/testtask'
|
|
3
|
+
require 'bundler'
|
|
4
|
+
Bundler.setup
|
|
5
|
+
rescue LoadError
|
|
6
|
+
warn 'bunler missing'
|
|
7
|
+
exit 42
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
gemspec = eval(File.read(Dir['*.gemspec'].first))
|
|
11
|
+
file = [gemspec.name, gemspec.version].join('-') + '.gem'
|
|
12
|
+
|
|
13
|
+
desc 'Validate gemspec'
|
|
14
|
+
task :gemspec do
|
|
15
|
+
gemspec.validate
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
desc 'Run minitest'
|
|
19
|
+
task :test do
|
|
20
|
+
Rake::TestTask.new do |t|
|
|
21
|
+
t.libs.push "lib"
|
|
22
|
+
t.test_files = FileList['spec/*_spec.rb']
|
|
23
|
+
t.verbose = true
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
desc 'Build gem'
|
|
28
|
+
task :build do
|
|
29
|
+
system "gem build #{gemspec.name}.gemspec"
|
|
30
|
+
FileUtils.mkdir_p 'gems'
|
|
31
|
+
FileUtils.mv file, 'gems'
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
desc 'Install gem'
|
|
35
|
+
task :install => :build do
|
|
36
|
+
system "sudo -E sh -c \'umask 022; gem install gems/#{file}\'"
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
desc 'Remove gems'
|
|
40
|
+
task :clean do
|
|
41
|
+
FileUtils.rm_rf 'gems'
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
desc 'Push to rubygems'
|
|
45
|
+
task :push do
|
|
46
|
+
system "gem push gems/#{file}"
|
|
47
|
+
end
|
data/asetus.gemspec
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
Gem::Specification.new do |s|
|
|
2
|
+
s.name = 'asetus'
|
|
3
|
+
s.version = '0.0.1'
|
|
4
|
+
s.platform = Gem::Platform::RUBY
|
|
5
|
+
s.authors = [ 'Saku Ytti' ]
|
|
6
|
+
s.email = %w( saku@ytti.fi )
|
|
7
|
+
s.homepage = 'http://github.com/ytti/asetus'
|
|
8
|
+
s.summary = 'configuration library'
|
|
9
|
+
s.description = 'configuration library with object access to YAML/JSON/CSON backends'
|
|
10
|
+
s.rubyforge_project = s.name
|
|
11
|
+
s.files = `git ls-files`.split("\n")
|
|
12
|
+
s.executables = %w( )
|
|
13
|
+
s.require_path = 'lib'
|
|
14
|
+
|
|
15
|
+
s.add_dependency 'slop'
|
|
16
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
class Asetus
|
|
2
|
+
|
|
3
|
+
def to_json config
|
|
4
|
+
Adapter::JSON.to config._asetus_to_hash
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def from_json json
|
|
8
|
+
Adapter::JSON.from json
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
class Adapter
|
|
12
|
+
class JSON
|
|
13
|
+
class << self
|
|
14
|
+
def to hash
|
|
15
|
+
require 'json'
|
|
16
|
+
::JSON.pretty_generate hash
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def from json
|
|
20
|
+
require 'json'
|
|
21
|
+
::JSON.load json
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
class Asetus
|
|
2
|
+
|
|
3
|
+
def to_yaml config
|
|
4
|
+
Adapter::YAML.to config._asetus_to_hash
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def from_yaml yaml
|
|
8
|
+
Adapter::YAML.from yaml
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
class Adapter
|
|
12
|
+
class YAML
|
|
13
|
+
class << self
|
|
14
|
+
def to hash
|
|
15
|
+
require 'yaml'
|
|
16
|
+
::YAML.dump hash
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def from yaml
|
|
20
|
+
require 'yaml'
|
|
21
|
+
::YAML.load yaml
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
class Asetus
|
|
2
|
+
class ConfigStruct
|
|
3
|
+
|
|
4
|
+
def _asetus_to_hash
|
|
5
|
+
hash = {}
|
|
6
|
+
@cfg.each do |key, value|
|
|
7
|
+
if value.class == ConfigStruct
|
|
8
|
+
value = value._asetus_to_hash
|
|
9
|
+
end
|
|
10
|
+
hash[key] = value
|
|
11
|
+
end
|
|
12
|
+
hash
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def empty?
|
|
16
|
+
@cfg.empty?
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
private
|
|
20
|
+
|
|
21
|
+
def initialize hash=nil
|
|
22
|
+
@cfg = hash ? _asetus_from_hash(hash) : {}
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def method_missing name, *args, &block
|
|
26
|
+
name = name.to_s
|
|
27
|
+
arg = args.first
|
|
28
|
+
if name[-1..-1] == '='
|
|
29
|
+
_asetus_set name[0..-2], arg
|
|
30
|
+
else
|
|
31
|
+
_asetus_get name, arg
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def _asetus_set key, value
|
|
36
|
+
@cfg[key] = value
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def _asetus_get key, value
|
|
40
|
+
if @cfg.has_key? key
|
|
41
|
+
@cfg[key]
|
|
42
|
+
else
|
|
43
|
+
@cfg[key] = ConfigStruct.new
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def _asetus_from_hash hash
|
|
48
|
+
cfg = {}
|
|
49
|
+
hash.each do |key, value|
|
|
50
|
+
if value.class == Hash
|
|
51
|
+
value = _asetus_from_hash value
|
|
52
|
+
end
|
|
53
|
+
cfg[key] = value
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
end
|
|
58
|
+
end
|
data/lib/asetus.rb
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
require_relative 'asetus/configstruct'
|
|
2
|
+
require_relative 'asetus/adapter/yaml'
|
|
3
|
+
require_relative 'asetus/adapter/json'
|
|
4
|
+
require 'fileutils'
|
|
5
|
+
|
|
6
|
+
class AsetusError < StandardError; end
|
|
7
|
+
class NoName < AsetusError; end
|
|
8
|
+
|
|
9
|
+
class Asetus
|
|
10
|
+
CONFIG_FILE = 'config'
|
|
11
|
+
attr_reader :cfg, :default
|
|
12
|
+
attr_accessor :system, :user
|
|
13
|
+
|
|
14
|
+
class << self
|
|
15
|
+
def cfg *args
|
|
16
|
+
new(*args).cfg
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def load level=:all
|
|
21
|
+
if level == :default or level == :all
|
|
22
|
+
@cfg = merge @cfg, @default
|
|
23
|
+
end
|
|
24
|
+
if level == :system or level == :all
|
|
25
|
+
@system = load_cfg @sysdir
|
|
26
|
+
@cfg = merge @cfg, @system
|
|
27
|
+
end
|
|
28
|
+
if level == :user or level == :all
|
|
29
|
+
@user = load_cfg @usrdir
|
|
30
|
+
@cfg = merge @cfg, @user
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def save level=:user
|
|
35
|
+
if level == :user
|
|
36
|
+
save_cfg @usrdir, @user
|
|
37
|
+
elsif level == :system
|
|
38
|
+
save_cfg @sysdir, @system
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
private
|
|
43
|
+
|
|
44
|
+
def initialize opts={}
|
|
45
|
+
@name = (opts.delete(:name) or metaname)
|
|
46
|
+
@adapter = (opts.delete(:adapter) or 'yaml')
|
|
47
|
+
@usrdir = (opts.delete(:usrdir) or File.join(Dir.home, '.config', @name))
|
|
48
|
+
@sysdir = (opts.delete(:sysdir) or File.join('/etc', @name))
|
|
49
|
+
@default = ConfigStruct.new opts.delete(:default)
|
|
50
|
+
@system = ConfigStruct.new
|
|
51
|
+
@user = ConfigStruct.new
|
|
52
|
+
@cfg = ConfigStruct.new
|
|
53
|
+
@load = true
|
|
54
|
+
@load = opts.delete(:load) if opts.has_key?(:load)
|
|
55
|
+
load :all if @load
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def load_cfg dir
|
|
59
|
+
file = File.join dir, CONFIG_FILE
|
|
60
|
+
file = File.read file
|
|
61
|
+
ConfigStruct.new from(@adapter, file)
|
|
62
|
+
rescue Errno::ENOENT
|
|
63
|
+
ConfigStruct.new
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def save_cfg dir, config
|
|
67
|
+
config = to(@adapter, config)
|
|
68
|
+
file = File.join dir, CONFIG_FILE
|
|
69
|
+
FileUtils.mkdir_p dir
|
|
70
|
+
File.write file, config
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def merge *configs
|
|
74
|
+
hash = {}
|
|
75
|
+
configs.each do |config|
|
|
76
|
+
hash = hash.merge config._asetus_to_hash
|
|
77
|
+
end
|
|
78
|
+
ConfigStruct.new hash
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def from adapter, string
|
|
82
|
+
name = 'from_' + adapter
|
|
83
|
+
send name, string
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def to adapter, config
|
|
87
|
+
name = 'to_' + adapter
|
|
88
|
+
send name, config
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def metaname
|
|
92
|
+
path = caller_locations[-1].path
|
|
93
|
+
File.basename path, File.extname(path)
|
|
94
|
+
rescue
|
|
95
|
+
raise NoName, "can't figure out name, specify explicitly"
|
|
96
|
+
end
|
|
97
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: asetus
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Saku Ytti
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2014-03-29 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: slop
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - '>='
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '0'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - '>='
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '0'
|
|
27
|
+
description: configuration library with object access to YAML/JSON/CSON backends
|
|
28
|
+
email:
|
|
29
|
+
- saku@ytti.fi
|
|
30
|
+
executables: []
|
|
31
|
+
extensions: []
|
|
32
|
+
extra_rdoc_files: []
|
|
33
|
+
files:
|
|
34
|
+
- .gitignore
|
|
35
|
+
- Gemfile
|
|
36
|
+
- README.md
|
|
37
|
+
- Rakefile
|
|
38
|
+
- asetus.gemspec
|
|
39
|
+
- lib/asetus.rb
|
|
40
|
+
- lib/asetus/adapter/json.rb
|
|
41
|
+
- lib/asetus/adapter/yaml.rb
|
|
42
|
+
- lib/asetus/configstruct.rb
|
|
43
|
+
homepage: http://github.com/ytti/asetus
|
|
44
|
+
licenses: []
|
|
45
|
+
metadata: {}
|
|
46
|
+
post_install_message:
|
|
47
|
+
rdoc_options: []
|
|
48
|
+
require_paths:
|
|
49
|
+
- lib
|
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - '>='
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '0'
|
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
56
|
+
requirements:
|
|
57
|
+
- - '>='
|
|
58
|
+
- !ruby/object:Gem::Version
|
|
59
|
+
version: '0'
|
|
60
|
+
requirements: []
|
|
61
|
+
rubyforge_project: asetus
|
|
62
|
+
rubygems_version: 2.0.7
|
|
63
|
+
signing_key:
|
|
64
|
+
specification_version: 4
|
|
65
|
+
summary: configuration library
|
|
66
|
+
test_files: []
|