strada 0.0.2 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6a981211dee0078b5673c81a679e548935bc96a89b5a276ad929a4f9f78fec62
4
- data.tar.gz: 2a2b608ebbbe7278cd8c15e5e595d6e3c8ac1084700b163a3a7d919c234a49fe
3
+ metadata.gz: 41300d7c5eb1f56f0631a7820e9985c5acc0727dbb8db6b2b97a9850a2610351
4
+ data.tar.gz: 39f971b1074a5dcbf1b107427ff8bbe6a9b05c1dec498c58135b1e71b162d59e
5
5
  SHA512:
6
- metadata.gz: dd490ff66da8df469d1c68d81a089af33f4bea7710c204800b2afc896629902554f8a1fd7aba7b81f350c5927527e4dc9a70aa9f0bd14db2a0ecb4d0dea9c716
7
- data.tar.gz: 645f418feeef553fab68563d2490aa122dd170bcb275231efead05e485cb7dae05861001de383d804b612777059c605dc364b145b8454aaceeefbf23c3c1d934
6
+ metadata.gz: f67d1199c690c9bfb36f3cfd29f022a5300839532f4ae7cc37ce946cc24deeeca4bc767a9392adcdc952dae3efaf26bfb81f07230ac4b6e5183fbfb4ef9f0596
7
+ data.tar.gz: 06cc0d0b95db050c3e14d3eadfe238e2247016418533292633cc63bab172c37e1785fac4e4eeced7d5abe9df19afff009f8f8950f8e495a0fffbd1d711f21d80
data/.gitignore CHANGED
@@ -1,2 +1,2 @@
1
1
  Gemfile.lock
2
- .*gems
2
+ *gem
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 WENWU YAN
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md CHANGED
@@ -1,59 +1,76 @@
1
1
  # Strada
2
- Configuration library for ruby with YAML/JSON/TOML backends with unified object
3
- access
2
+
3
+ Configuration library for ruby with YAML/JSON/TOML backends with unified object access
4
4
 
5
5
  ## Install
6
- ```
7
- % gem install Strada
6
+
7
+ ```shell
8
+ % gem install strada
8
9
  ```
9
10
 
10
11
  ## Use
12
+
11
13
  ### Simple
12
- ```
13
- require 'Strada'
14
+
15
+ ```ruby
16
+ require 'strada'
17
+ require 'pp'
14
18
  cfg = Strada.cfg
19
+ # print cfg datastructure
20
+ PP.pp cfg
21
+
15
22
  port = cfg.server.port
16
23
  user = cfg.auth.user
17
24
  pw = cfg.auth.password
25
+
26
+ PP.pp cfg
18
27
  ```
28
+
19
29
  It tried to detect your software name via caller_locations if no ':name'
20
30
  argument was given.
21
31
  It automatically loads /etc/name/config and ~/.config/name/config and merges
22
32
  them together.
23
33
 
24
34
  ### Advanced
25
- ```
26
- require 'Strada'
35
+
36
+ ```ruby
37
+ require 'strada'
38
+
27
39
  Strada = Strada.new name: 'mykewlapp',
28
- default: {'poop'=>'xyzzy'},
40
+ default: { 'poop' => 'xyzzy' },
29
41
  adapter: 'yaml',
30
42
  usrdir: '/home/app/config/',
31
43
  sysdir: '/System/config/',
32
44
  load: false
33
- Strada.default.poop2 = [1, 2, 3, 4]
45
+
46
+ Strada.default.poop2 = [1, 2, 3, 4]
34
47
  Strada.default.starship.poopers = 42
35
48
  Strada.load :user
49
+
36
50
  if Strada.user.empty?
37
51
  Strada.user = Strada.default
38
52
  Strada.save :user
39
53
  end
40
- Strada.load # load+merges cfg, takes argument :default, :system, :user
41
- Strada.cfg # merged default + system + user (merged on load)
54
+
55
+ Strada.load # load+merges cfg, takes argument :default, :system, :user
56
+ Strada.cfg # merged default + system + user (merged on load)
42
57
  Strada.default # default only
43
- Strada.system # system only
44
- Strada.user # user only
58
+ Strada.system # system only
59
+ Strada.user # user only
45
60
  ```
46
61
 
47
62
  ## Reserved methods
48
63
 
49
- * each - iterate all config keys in current level
64
+ * each - iterate all config keys in current level
50
65
  * has_key?(arg) - check if current level has key arg
51
66
  * [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)
67
+ * key? - all keys have question mark version reserved, checks if key exists+true (true), exists+false (false),
68
+ not-exists (nil)
69
+
53
70
  + all object class methods
54
71
 
55
72
  ## TODO
56
73
 
57
- * should I add feature to raise on unconfigured/unset?
58
- * should I always merge to 'cfg' when default/system/config is set?
74
+ * should I add feature to raise on unconfigured/unset?
75
+ * should I always merge to 'cfg' when default/system/config is set?
59
76
 
@@ -13,13 +13,14 @@ class Strada
13
13
  class JSON
14
14
  # 定义类方法
15
15
  class << self
16
- require "json"
17
16
  # 将 RUBY(HASH) 数据结构转换为 JSON
18
17
  def to(hash)
18
+ require "json"
19
19
  ::JSON.pretty_generate hash
20
20
  end
21
21
  # 将 JSON 转换为 RUBY 数据结构
22
22
  def from(json)
23
+ require "json"
23
24
  ::JSON.load json
24
25
  end
25
26
  end
@@ -13,14 +13,14 @@ class Strada
13
13
  class TOML
14
14
  # 定义类方法
15
15
  class << self
16
- require "toml"
17
-
18
16
  # 将 RUBY(HASH) 数据结构转换为 TOML
19
17
  def to(hash)
18
+ require "toml"
20
19
  ::TOML::Generator.new(hash).body
21
20
  end
22
21
  # 将 TOML 转换为 RUBY 数据结构
23
22
  def from(toml)
23
+ require "toml"
24
24
  ::TOML.load toml
25
25
  end
26
26
  end
@@ -13,14 +13,14 @@ class Strada
13
13
  class YAML
14
14
  # 定义类方法
15
15
  class << self
16
- require "yaml"
17
-
18
16
  # 将 RUBY(HASH) 数据结构转换为 YAML
19
17
  def to(hash)
18
+ require "yaml"
20
19
  ::YAML.dump hash
21
20
  end
22
21
  # 将 YAML 转换为 RUBY(HASH) 数据结构
23
22
  def from(yaml)
23
+ require "yaml"
24
24
  ::YAML.load yaml
25
25
  end
26
26
  end
@@ -2,6 +2,39 @@
2
2
 
3
3
  class Strada
4
4
  class ConfigStruct
5
+ # 类对象初始化函数入口
6
+ def initialize(hash = nil, opts = {})
7
+ @key_to_s = opts.delete(:key_to_s) || false
8
+ @cfg = hash ? _config_from_hash(hash) : {}
9
+ end
10
+
11
+ # 方法反射
12
+ def method_missing(name, *args, &block)
13
+ name = name.to_s
14
+ # 检索 ConfigStruct 键值对
15
+ # strada.cfg['foo']
16
+ name = args.shift if name[0..1] == "[]"
17
+ arg = args.first
18
+
19
+ if name[-1..-1] == "?"
20
+ # 查询是否包含某个 KEY
21
+ # strada.cfg.foo.bar?
22
+ if @cfg.has_key? name[0..-2]
23
+ @cfg[name[0..-2]]
24
+ else
25
+ nil
26
+ end
27
+ elsif name[-1..-1] == "="
28
+ # strada.cfg.foo.bar = 'bala'
29
+ # ConfigStruct 键值对赋值
30
+ _config_set name[0..-2], arg
31
+ else
32
+ # strada.cfg.foo.bar
33
+ # ConfigStruct 查询某个属性
34
+ _config_get name, arg
35
+ end
36
+ end
37
+
5
38
  # 将配置信息转换为 HASH 对象
6
39
  def _config_to_hash
7
40
  hash = {}
@@ -9,7 +42,9 @@ class Strada
9
42
  if value.class == ConfigStruct
10
43
  value = value._config_to_hash
11
44
  end
12
- key = key.to_s if @key_to_s
45
+ # 是否需要将 key 转为 to_s
46
+ key = key.to_s if @key_to_s
47
+ # 保存键值对数据到 HASH
13
48
  hash[key] = value
14
49
  end
15
50
  hash
@@ -36,37 +71,13 @@ class Strada
36
71
  end
37
72
 
38
73
  private
39
- def initialize(hash = nil, opts = {})
40
- @key_to_s = opts.delete :key_to_s
41
- @cfg = hash ? _config_from_hash(hash) : {}
42
- end
43
-
44
- # 方法反射
45
- def method_missing(name, *args, &block)
46
- name = name.to_s
47
- name = args.shift if name[0..1] == "[]" # strada.cfg['foo']
48
- arg = args.first
49
-
50
- if name[-1..-1] == "?" # strada.cfg.foo.bar?
51
- if @cfg.has_key? name[0..-2]
52
- @cfg[name[0..-2]]
53
- else
54
- nil
55
- end
56
- elsif name[-1..-1] == "=" # strada.cfg.foo.bar = 'quux'
57
- _config_set name[0..-2], arg
58
- else
59
- _config_get name, arg # strada.cfg.foo.bar
60
- end
61
- end
62
-
63
- # 设置键值对
64
74
  def _config_set(key, value)
75
+ # 设置键值对
65
76
  @cfg[key] = value
66
77
  end
67
78
 
68
- # 查询 KEY VALUE
69
79
  def _config_get(key, value)
80
+ # 查询 KEY VALUE
70
81
  if @cfg.has_key? key
71
82
  @cfg[key]
72
83
  else
@@ -74,8 +85,8 @@ class Strada
74
85
  end
75
86
  end
76
87
 
77
- # 转换 HASH 数据为配置对象
78
88
  def _config_from_hash(hash)
89
+ # 转换 HASH 数据为配置对象
79
90
  cfg = {}
80
91
  hash.each do |key, value|
81
92
  if value.class == Hash
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Strada
4
- VERSION = "0.0.2"
4
+ VERSION = "0.0.5"
5
5
  end
data/lib/strada.rb CHANGED
@@ -28,11 +28,6 @@ class UnknownOption < StradaError; end
28
28
  # ssh_hosts = CFG.ssh.hosts
29
29
 
30
30
  class Strada
31
- CONFIG_FILE = "config"
32
- # 类对象属性
33
- attr_reader :cfg, :default, :file
34
- attr_accessor :system, :user
35
-
36
31
  # 类方法
37
32
  class << self
38
33
  def cfg(*args)
@@ -40,6 +35,42 @@ class Strada
40
35
  end
41
36
  end
42
37
 
38
+ # 类变量 - 常量,配置保存文件名
39
+ CONFIG_FILE = "config"
40
+
41
+ # 类对象属性
42
+ attr_reader :cfg, :default, :file, :usr_dir, :sys_dir
43
+ attr_accessor :system, :user
44
+
45
+ # 类对象实例化函数入口
46
+ def initialize(opts = {})
47
+ # @param [Hash] opts options for Strada.new
48
+ # @option opts [String] :name name to use for strada (/etc/name/, ~/.config/name/) - autodetected if not defined
49
+ # @option opts [String] :adapter adapter to use 'yaml', 'json' or 'toml' for now
50
+ # @option opts [String] :usr_dir directory for storing user config ~/.config/name/ by default
51
+ # @option opts [String] :sys_dir directory for storing system config /etc/name/ by default
52
+ # @option opts [String] :cfg_file configuration filename, by default CONFIG_FILE
53
+ # @option opts [Hash] :default default settings to use
54
+ # @option opts [boolean] :load automatically load+merge system+user config with defaults in #cfg
55
+ # @option opts [boolean] :key_to_s convert keys to string by calling #to_s for keys
56
+ @name = opts.delete(:name) || meta_name
57
+ @adapter = opts.delete(:adapter) || "yaml"
58
+ @usr_dir = opts.delete(:usr_dir) || File.join(Dir.home, ".config", @name)
59
+ @sys_dir = opts.delete(:sys_dir) || File.join("/etc", @name)
60
+ @cfg_file = opts.delete(:cfg_file) || CONFIG_FILE
61
+
62
+ # 配置对象属性
63
+ @default = ConfigStruct.new opts.delete(:default)
64
+ @system = ConfigStruct.new
65
+ @user = ConfigStruct.new
66
+ @cfg = ConfigStruct.new
67
+ @load = true
68
+ @load = opts.delete(:load) if opts.has_key?(:load)
69
+ @key_to_s = opts.delete(:key_to_s)
70
+ raise UnknownOption, "option '#{opts}' not recognized" unless opts.empty?
71
+ load :all if @load
72
+ end
73
+
43
74
  # When this is called, by default :system and :user are loaded from
44
75
  # filesystem and merged with default, so that user overrides system which
45
76
  # overrides default
@@ -78,8 +109,8 @@ class Strada
78
109
  # @option opts [boolean] :load load config once saved, by default false
79
110
  # @return [boolean] true if config didn't exist and was created, false if config already exists
80
111
  def create(opts = {})
81
- src = opts.delete :source || :default
82
- dst = opts.delete :destination || :user
112
+ src = opts.delete(:source) || :default
113
+ dst = opts.delete(:destination) || :user
83
114
 
84
115
  no_config = false
85
116
  no_config = true if @system.empty? && @user.empty?
@@ -87,42 +118,34 @@ class Strada
87
118
  src = instance_variable_get "@" + src.to_s
88
119
  instance_variable_set("@" + dst.to_s, src.dup)
89
120
  save dst
90
- load if opts.delete :load
121
+ load if opts.delete(:load)
91
122
  end
92
123
  no_config
93
124
  end
94
125
 
95
- private
96
- def initialize(opts = {})
97
- # @param [Hash] opts options for Strada.new
98
- # @option opts [String] :name name to use for strada (/etc/name/, ~/.config/name/) - autodetected if not defined
99
- # @option opts [String] :adapter adapter to use 'yaml', 'json' or 'toml' for now
100
- # @option opts [String] :usr_dir directory for storing user config ~/.config/name/ by default
101
- # @option opts [String] :sys_dir directory for storing system config /etc/name/ by default
102
- # @option opts [String] :cfg_file configuration filename, by default CONFIG_FILE
103
- # @option opts [Hash] :default default settings to use
104
- # @option opts [boolean] :load automatically load+merge system+user config with defaults in #cfg
105
- # @option opts [boolean] :key_to_s convert keys to string by calling #to_s for keys
106
- @name = opts.delete(:name) || meta_name
107
- @adapter = opts.delete(:adapter) || "yaml"
108
- @usr_dir = opts.delete(:usr_dir) || File.join(Dir.home, ".config", @name)
109
- @sys_dir = opts.delete(:sys_dir) || File.join("/etc", @name)
110
- @cfg_file = opts.delete(:cfg_file) || CONFIG_FILE
111
-
112
- # 配置对象属性
113
- @default = ConfigStruct.new opts.delete(:default)
114
- @system = ConfigStruct.new
115
- @user = ConfigStruct.new
116
- @cfg = ConfigStruct.new
117
- @load = true
118
- @load = opts.delete(:load) if opts.has_key?(:load)
119
- @key_to_s = opts.delete(:key_to_s)
120
- raise UnknownOption, "option '#{opts}' not recognized" unless opts.empty?
121
- load :all if @load
122
- end
126
+ # 将 JSON|YAML|TOML 等数据格式转换为 RUBY 数据结构
127
+ def from(adapter, config)
128
+ name = "from_" + adapter
129
+ send name, config
130
+ end
131
+
132
+ # RUBY 数据结构转换为 JSON|YAML|TOML 对象
133
+ def to(adapter, config)
134
+ name = "to_" + adapter
135
+ send name, config
136
+ end
123
137
 
124
- # 加载配置文件
138
+ # 基础的配置标致名称
139
+ def meta_name
140
+ path = caller_locations[-1].path
141
+ File.basename path, File.extname(path)
142
+ rescue
143
+ raise NoName, "can't figure out name, specify explicitly"
144
+ end
145
+
146
+ private
125
147
  def load_cfg(dir)
148
+ # 加载配置文件
126
149
  @file = File.join dir, @cfg_file
127
150
  file = File.read @file
128
151
  ConfigStruct.new(from(@adapter, file), key_to_s: @key_to_s)
@@ -134,7 +157,9 @@ class Strada
134
157
  def save_cfg(dir, config)
135
158
  config = to(@adapter, config)
136
159
  file = File.join dir, @cfg_file
137
- FileUtils.mkdir_p dir
160
+
161
+ # 判断目标文件夹是否存在,不存在则新建
162
+ FileUtils.mkdir_p dir unless Dir.exist? dir
138
163
  File.write file, config
139
164
  end
140
165
 
@@ -143,38 +168,18 @@ class Strada
143
168
  hash = {}
144
169
  # 将相关配置依次迭代并转换为 RUBY HASH 对象
145
170
  configs.each do |config|
146
- hash = hash._config_deep_merge config._config_to_hash
171
+ hash = hash._config_deep_merge(config._config_to_hash)
147
172
  end
148
173
  # 将合并后的 HASH 数据结构转换为配置对象
149
174
  ConfigStruct.new hash
150
175
  end
151
-
152
- # 将 JSON|YAML|TOML 等数据格式转换为 RUBY 数据结构
153
- def from(adapter, string)
154
- name = "from_" + adapter
155
- send name, string
156
- end
157
-
158
- # 将 RUBY 数据结构转换为 JSON|YAML|TOML 对象
159
- def to(adapter, config)
160
- name = "to_" + adapter
161
- send name, config
162
- end
163
-
164
- # 基础的配置标致名称
165
- def meta_name
166
- path = caller_locations[-1].path
167
- File.basename path, File.extname(path)
168
- rescue
169
- raise NoName, "can't figure out name, specify explicitly"
170
- end
171
176
  end
172
177
 
173
178
  # 增加 HASH 方法
174
179
  class Hash
175
180
  def _config_deep_merge(new_hash)
176
181
  merger = proc do |key, old_val, new_val|
177
- Hash === old_val && Hash === new_val ? old_val.merge(new_val, &merger) : new_val
182
+ (Hash === old_val && Hash === new_val) ? old_val.merge(new_val, &merger) : new_val
178
183
  end
179
184
  merge new_hash, &merger
180
185
  end
data/strada.gemspec CHANGED
@@ -2,16 +2,23 @@
2
2
 
3
3
  require_relative "lib/strada/version"
4
4
  Gem::Specification.new do |s|
5
- s.name = "strada"
6
- s.version = Strada::VERSION
7
- s.licenses = ["MIT"]
8
- s.platform = Gem::Platform::RUBY
9
- s.authors = ["WENWU.YAN"]
10
- s.email = "968826@gmail.com"
11
- s.homepage = "http://github.com/ciscolive/strada"
12
- s.summary = "configuration library"
13
- s.description = "configuration library with object access to YAML/JSON/TOML backends"
5
+ s.name = "strada"
6
+ s.version = Strada::VERSION
7
+ s.licenses = ["MIT"]
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["WENWU.YAN"]
10
+ s.email = "968826@gmail.com"
11
+ s.homepage = "http://github.com/ciscolive/strada"
12
+ s.summary = "configuration library"
13
+ s.description = "configuration library with object access to YAML/JSON/TOML backends"
14
+
15
+ s.required_ruby_version = ">= 2.6.0"
16
+ # spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
17
+
18
+ s.metadata["homepage_uri"] = s.homepage
19
+ s.metadata["source_code_uri"] = "https://github.com/ciscolive/strada"
20
+ s.metadata["changelog_uri"] = "https://github.com/ciscolive/strada/blob/main/README.md"
21
+
14
22
  s.files = `git ls-files`.split("\n")
15
- s.executables = %w( )
16
23
  s.require_path = "lib"
17
24
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: strada
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - WENWU.YAN
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-04-14 00:00:00.000000000 Z
11
+ date: 2022-04-17 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: configuration library with object access to YAML/JSON/TOML backends
14
14
  email: 968826@gmail.com
@@ -26,6 +26,7 @@ files:
26
26
  - ".idea/vcs.xml"
27
27
  - ".rubocop.yml"
28
28
  - Gemfile
29
+ - LICENSE
29
30
  - README.md
30
31
  - Rakefile
31
32
  - lib/strada.rb
@@ -38,7 +39,10 @@ files:
38
39
  homepage: http://github.com/ciscolive/strada
39
40
  licenses:
40
41
  - MIT
41
- metadata: {}
42
+ metadata:
43
+ homepage_uri: http://github.com/ciscolive/strada
44
+ source_code_uri: https://github.com/ciscolive/strada
45
+ changelog_uri: https://github.com/ciscolive/strada/blob/main/README.md
42
46
  post_install_message:
43
47
  rdoc_options: []
44
48
  require_paths:
@@ -47,7 +51,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
47
51
  requirements:
48
52
  - - ">="
49
53
  - !ruby/object:Gem::Version
50
- version: '0'
54
+ version: 2.6.0
51
55
  required_rubygems_version: !ruby/object:Gem::Requirement
52
56
  requirements:
53
57
  - - ">="