flex_config 0.0.2 → 0.0.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 492b688111180e1e7964f43b67b03e7494c7b72a
4
- data.tar.gz: 14ee3e8b0884af9c38eb6294389714be3f5c15cf
3
+ metadata.gz: 39fdff2621755a3113b0102519cee359b767dbfd
4
+ data.tar.gz: e211bcafa3ffddaa84d030de886d281960926468
5
5
  SHA512:
6
- metadata.gz: 9be5b50da2bfdfae2c0441a6284cc7a7806bee1f9f45c995907aff1519eb1bfa3cc4b26ac0a5f26d4c7d840bcac4e9a2e4bc1a0af021172d7371cb58992ed632
7
- data.tar.gz: a80e68c6ce7de705b94242b154ca9c67b4d6e032458e57cbb4f169f3d2dce7202201179ba3c7f7ee697e8876fc38a92f40ce331a5e83cf7843cd898410f5268f
6
+ metadata.gz: 141b3610a9a3b2bda029416eb46ad488e14d27b1cd5552fc875724970e8d2ff4c25367355014afbce12f07e19cb47a3a3c837228c66a533bc88dc2a241bcadc8
7
+ data.tar.gz: 8ff7bc0c776479dde3fbd40198885fc38845633ee96e5696e8df17398c01b7f70eb471b90a439848dbef02fd29c9876767e63218bcf94ad406bf9faf03374a72
@@ -0,0 +1,19 @@
1
+ class FlexConfig
2
+
3
+ class ConfigObj
4
+
5
+ def initialize (parent, key, contents)
6
+ @parent = parent
7
+ @key = key
8
+ @contents = contents
9
+ end
10
+
11
+ def method_missing (method, *args)
12
+ @contents.send(method, *args)
13
+ @parent.config[@key] = @contents
14
+ @parent.save_config
15
+ end
16
+
17
+ end
18
+
19
+ end
data/lib/flex_config.rb CHANGED
@@ -1,31 +1,83 @@
1
+ require 'flex_config/config_obj'
2
+ require 'flex_mods'
1
3
  require 'yaml'
2
4
 
3
5
  class FlexConfig
4
- def initialize (path, configs)
5
6
 
6
- unless Dir.exist?(path)
7
- Dir.mkdir(path)
7
+ class << self
8
+
9
+ def get_config (path)
10
+ file_path = path + '/config.yml'
11
+ YAML::load(File.open(file_path, 'r'))
8
12
  end
9
13
 
10
- file_path = path + '/config.yml'
14
+ end
11
15
 
12
- if File.exist?(file_path)
13
- config = YAML::load(File.open(file_path, 'r'))
14
- else
15
- config = {}
16
+ def initialize (path, configs)
17
+
18
+ file_path = check_path(path) + '/config.yml'
19
+ config = check_file(file_path)
20
+
21
+ case configs
22
+ when Hash
23
+ config.merge!(configs) unless configs == {}
24
+ when Array
25
+ new = {}
26
+ configs.each { |key| new[key] = {} }
27
+ config.merge!(new)
28
+ else
29
+ raise ArgumentError('Second argument must be a hash or array of keys')
16
30
  end
17
31
 
18
- config.merge!(configs)
32
+ @config = config
33
+ @file_path = file_path
19
34
 
20
- output = File.open(file_path, 'w')
21
- output.puts config.to_yaml
22
- output.close
35
+ save_config
36
+ end
23
37
 
38
+ def method_missing (method, *args)
39
+ @config.send(method, *args)
24
40
  end
25
41
 
26
- def get_config (path)
27
- file_path = path + '/config.yml'
42
+ def [] (key)
43
+ state =
44
+ if @config[key].nil?
45
+ {}
46
+ else
47
+ @config[key].deep_copy
48
+ end
49
+
50
+ ConfigObj.new(self, key, state)
51
+ end
28
52
 
29
- YAML::load(File.open(file_path, 'r'))
53
+ def to_h
54
+ @config
30
55
  end
31
- end
56
+
57
+ def check_path (path)
58
+ dir_path = path + '/config'
59
+ unless Dir.exist?(dir_path)
60
+ Dir.mkdir(dir_path)
61
+ end
62
+ dir_path
63
+ end
64
+
65
+ def check_file (path)
66
+ if File.exist?(path)
67
+ YAML::load(File.open(path, 'r'))
68
+ else
69
+ {}
70
+ end
71
+ end
72
+
73
+ def save_config
74
+ output = File.open(@file_path, 'w')
75
+ output.puts @config.to_yaml
76
+ output.close
77
+ @config
78
+ end
79
+
80
+ end
81
+
82
+
83
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flex_config
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eugene Lai
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-27 00:00:00.000000000 Z
11
+ date: 2016-03-05 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: To do
14
14
  email: ejt.lai@gmail.com
@@ -17,6 +17,7 @@ extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
19
  - lib/flex_config.rb
20
+ - lib/flex_config/config_obj.rb
20
21
  homepage:
21
22
  licenses: []
22
23
  metadata: {}