asetus 0.0.6 → 0.0.7

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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +8 -0
  3. data/asetus.gemspec +1 -1
  4. data/lib/asetus.rb +53 -0
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a35c4aaaa3dcbbb3cebc0d3739e0bbea12dbf516
4
- data.tar.gz: df049bdce93f3d388e556e82af8bf16babfc2210
3
+ metadata.gz: ffddca763a36284504ce06cc389c15fb12dfd823
4
+ data.tar.gz: 47fde4f5d41ceccf13a910d9b13e2f14beaba9f3
5
5
  SHA512:
6
- metadata.gz: 6826d9593bc2fd0475b476ff2324840641859afab3340c2623e8a5119b96b86bf21173ef222b2a26d5b39735c9eac77a2022e28e5dced6623ce97223a2d539a5
7
- data.tar.gz: 9aa66d652a42552230d5f8dd0d0ff076257943e2d17f7d0409f5f02263491b246b328bae0929fb4840b123e4cb6b8e9e814eb76ca792be78e11db78d381896c4
6
+ metadata.gz: 2992513481cbabfbef49fe4fd3e67413a6292e800c6305d70025c52c5b885d7e54ccef04ed79872301517cd3558e58e83de352ade7a6c2c89ecf0e3632a67bd2
7
+ data.tar.gz: 4dbe149a790b539b24607003e6b744ba81c0e341dd12eb53296822e3f47f22d8451fe516e6bfc88d4708409c2044fd27ea9bc2a387ed8d862de7dd8dc261c38a
data/README.md CHANGED
@@ -44,6 +44,14 @@ asetus.system # system only
44
44
  asetus.user # user only
45
45
  ```
46
46
 
47
+ ## Reserved methods
48
+
49
+ * each - iterate all config keys in current level
50
+ * has_key?(arg) - check if current level has key arg
51
+ * [arg] - fetch arg (useful for non-literal retrieval, instead of using #send)
52
+ * key? - all keys have question mark version reserved, checks if key exists+true (true), exists+false (false), not-exists (nil)
53
+ + all object class methods
54
+
47
55
  ## TODO
48
56
 
49
57
  * should I add feature to raise on unconfigured/unset?
data/asetus.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'asetus'
3
- s.version = '0.0.6'
3
+ s.version = '0.0.7'
4
4
  s.platform = Gem::Platform::RUBY
5
5
  s.authors = [ 'Saku Ytti' ]
6
6
  s.email = %w( saku@ytti.fi )
data/lib/asetus.rb CHANGED
@@ -7,6 +7,20 @@ class AsetusError < StandardError; end
7
7
  class NoName < AsetusError; end
8
8
  class UnknownOption < AsetusError; end
9
9
 
10
+ # @example common use case
11
+ # CFGS = Asetus.new :name=>'my_sweet_program' :load=>false # do not load config from filesystem
12
+ # CFGS.default.ssh.port = 22
13
+ # CFGS.default.ssh.hosts = %w(host1.example.com host2.example.com)
14
+ # CFGS.default.auth.user = lana
15
+ # CFGS.default.auth.password = dangerzone
16
+ # CFGS.load # load system config and user config from filesystem and merge with defaults to #cfg
17
+ # raise StandardError, 'edit ~/.config/my_sweet_program/config' if CFGS.create # create user config from default config if no system or user config exists
18
+ # # use the damn thing
19
+ # CFG = CFGS.cfg
20
+ # user = CFG.auth.user
21
+ # password = CFG.auth.password
22
+ # ssh_port = CFG.ssh.port
23
+ # ssh_hosts = CFG.ssh.hosts
10
24
  class Asetus
11
25
  CONFIG_FILE = 'config'
12
26
  attr_reader :cfg, :default
@@ -18,6 +32,12 @@ class Asetus
18
32
  end
19
33
  end
20
34
 
35
+ # When this is called, by default :system and :user are loaded from
36
+ # filesystem and merged with defefault, so that user overrides system which
37
+ # overrides default
38
+ #
39
+ # @param [Symbol] level which configuration level to load, by defaukt :all
40
+ # @return [void]
21
41
  def load level=:all
22
42
  if level == :default or level == :all
23
43
  @cfg = merge @cfg, @default
@@ -32,6 +52,8 @@ class Asetus
32
52
  end
33
53
  end
34
54
 
55
+ # @param [Symbol] level which configuration level to save, by default :user
56
+ # @return [void]
35
57
  def save level=:user
36
58
  if level == :user
37
59
  save_cfg @usrdir, @user
@@ -40,8 +62,39 @@ class Asetus
40
62
  end
41
63
  end
42
64
 
65
+ # @example create user config from default config and raise error, if no config was found
66
+ # raise StandardError, 'edit ~/.config/name/config' if asetus.create
67
+ # @param [Hash] opts options for Asetus
68
+ # @option opts [Symbol] :source source to use for settings to save, by defaylt :default
69
+ # @option opts [Symbol] :destination destinatino to use for settings to save, by default :user
70
+ # @option opts [boolean] :load load config once saved, by default false
71
+ # @return [boolean] true if config didn't exist and was created, false if config already exists
72
+ def create opts={}
73
+ src = opts.delete :source
74
+ src ||= :default
75
+ dst = opts.delete :destination
76
+ dst ||= :user
77
+ no_config = false
78
+ no_config = true if @system.empty? and @user.empty?
79
+ if no_config
80
+ src = instance_variable_get '@' + src.to_s
81
+ instance_variable_set('@'+dst.to_s, src.dup)
82
+ save dst
83
+ load if opts.delete :load
84
+ end
85
+ no_config
86
+ end
87
+
43
88
  private
44
89
 
90
+ # @param [Hash] opts options for Asetus.new
91
+ # @option opts [String] :name name to use for asetus (/etc/name/, ~/.config/name/) - autodetected if not defined
92
+ # @option opts [String] :adapter adapter to use 'yaml' or 'json' for now
93
+ # @option opts [String] :usrdir directory for storing user config ~/.config/name/ by default
94
+ # @option opts [String] :sysdir directory for storing system config /etc/name/ by default
95
+ # @option opts [Hash] :default default settings to use
96
+ # @option opts [boolean] :load automatically load+merge system+user config with defaults in #cfg
97
+ # @option opts [boolean] :key_to_s convert keys to string by calling #to_s for keys
45
98
  def initialize opts={}
46
99
  @name = (opts.delete(:name) or metaname)
47
100
  @adapter = (opts.delete(:adapter) or 'yaml')
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: asetus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Saku Ytti
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-14 00:00:00.000000000 Z
11
+ date: 2014-04-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: slop