libis-tools 0.9.4 → 0.9.5
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 +4 -4
- data/README.md +27 -6
- data/lib/libis/tools/config.rb +31 -78
- data/lib/libis/tools/config_file.rb +78 -0
- data/lib/libis/tools/deep_struct.rb +11 -0
- data/lib/libis/tools/version.rb +1 -1
- data/libis-tools.gemspec +1 -1
- data/spec/command_spec.rb +19 -23
- data/spec/config_file_spec.rb +35 -0
- data/spec/config_spec.rb +93 -67
- data/spec/data/test.yml +1 -0
- data/spec/data/test_config.yml +15 -0
- data/spec/deep_struct_spec.rb +115 -59
- metadata +9 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 29a2f53e5ac6fb5e2432eddac4617092644fc545
|
4
|
+
data.tar.gz: 8576f7fb158f2e8cde213edbe2cbf9d0444ac6fd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ebbd5da879c6a915be6659b699fbdb5e751c932c11b7f4bf85451f5ced8f612d02f191a6976468a5d55e521323035b408e28e7942d9224232cdf9207b081120b
|
7
|
+
data.tar.gz: 2275268123728cfaf39fa811cdf09a1e154855f5bafcb083c9aefa49f335a5230283a831da39e02a73bb16d85dcbf01dbbf975670bab4e584935e5348fb955e7
|
data/README.md
CHANGED
@@ -1,9 +1,7 @@
|
|
1
|
+
[](http://badge.fury.io/rb/libis-tools)
|
1
2
|
[](https://travis-ci.org/Kris-LIBIS/LIBIS_Tools)
|
2
3
|
[](https://coveralls.io/r/Kris-LIBIS/LIBIS_Tools)
|
3
4
|
[](https://gemnasium.com/Kris-LIBIS/LIBIS_Tools)
|
4
|
-
<!--[](https://codeclimate.com/github/Kris-LIBIS/LIBIS_Tools)
|
5
|
-
[](https://codeclimate.com/github/Kris-LIBIS/LIBIS_Tools)
|
6
|
-
-->
|
7
5
|
|
8
6
|
# Libis::Tools
|
9
7
|
|
@@ -131,11 +129,33 @@ or:
|
|
131
129
|
Note that the Command class uses Open3#popen3 internally. All arguments supplied to Command#run are passed to the popen3
|
132
130
|
call. Unfortunately JRuby has some known issues with popen3. Please use and test carefully in JRuby environments.
|
133
131
|
|
132
|
+
### DeepStruct
|
133
|
+
|
134
|
+
A class that derives from OpenStruct through the RecursiveOpenStruct. A RecursiveOpenStruct is derived from stdlib's
|
135
|
+
OpenStruct, but can be made recursive. DeepStruct enforces this behaviour and adds a clear! method.
|
136
|
+
|
137
|
+
### ConfigFile
|
138
|
+
|
139
|
+
A base class for Config, but useable on it's own. It extends the DeepStruct with << and >> methods that supports
|
140
|
+
loading and saving of configuration values from and to files. Note that ERB commands will get lost during a round-trip.
|
141
|
+
|
142
|
+
```ruby
|
143
|
+
require 'libis/tools/config_file'
|
144
|
+
cfg_file = ::Libis::Tools::ConfigFile.new
|
145
|
+
cfg_file << {foo: 'bar'}
|
146
|
+
cfg_file.my_value = 10
|
147
|
+
p cfg_file[:my_value] # => 10
|
148
|
+
cfg_file{:my_text] = 'abc'
|
149
|
+
p cfg_file['my_text'] # => 'abc'
|
150
|
+
p cfg_file.to_hash # => { :foo => 'bar', 'my_value' => 10, :my_text => 'abc' }
|
151
|
+
cfg >> 'my_config.yml'
|
152
|
+
```
|
134
153
|
### Config
|
135
154
|
|
136
|
-
The Config class is a convenience
|
137
|
-
loading configurations from multiple YAML files containing ERB statements.
|
138
|
-
pattern and behaves like a Hash/OpenStruct/HashWithIndifferentAccess
|
155
|
+
The Config class is a convenience class for easy configuration maintenance and loading. Based on ConfigFile and
|
156
|
+
DeepStruc, it supports code defaults and loading configurations from multiple YAML files containing ERB statements.
|
157
|
+
The Config class follows the Singleton pattern and behaves like a Hash/OpenStruct/HashWithIndifferentAccess with
|
158
|
+
recursion over hashes and arrays. It also initializes a default Logger instance.
|
139
159
|
|
140
160
|
For each configuration parameter, the value can be accessed via the class or the Singleton instance through a method
|
141
161
|
call or via the Hash operator using the parameter name either as a string or a symbol.
|
@@ -145,6 +165,7 @@ Examples:
|
|
145
165
|
```ruby
|
146
166
|
require 'libis/tools/config'
|
147
167
|
cfg = ::Libis::Tools::Config
|
168
|
+
cfg << 'my_config.yml'
|
148
169
|
cfg['my_value'] = 10
|
149
170
|
p cfg.instance.my_value # => 10
|
150
171
|
cfg.instance.my_text = 'abc'
|
data/lib/libis/tools/config.rb
CHANGED
@@ -1,17 +1,15 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
require 'singleton'
|
3
|
-
require 'set'
|
4
3
|
require 'yaml'
|
5
4
|
require 'erb'
|
6
5
|
require 'logger'
|
7
|
-
require 'backports'
|
8
6
|
|
9
|
-
require 'libis/tools/
|
7
|
+
require 'libis/tools/config_file'
|
10
8
|
|
11
9
|
module Libis
|
12
10
|
module Tools
|
13
11
|
|
14
|
-
# The Config class is a convenience
|
12
|
+
# The Config class is a convenience class for easy configuration maintenance, loading and saving.
|
15
13
|
# It supports code defaults, loading configurations from multiple YAML files containing ERB statements.
|
16
14
|
# The Config class follows the Singleton pattern and behaves like a Hash/OpenStruct/HashWithIndifferentAccess.
|
17
15
|
# It also initializes a default Logger instance.
|
@@ -27,7 +25,7 @@ module Libis
|
|
27
25
|
# p cfg[:my_text] # => 'abc'
|
28
26
|
# p cfg.logger.warn('message') # => W, [2015-03-16T12:51:01.180548 #28935] WARN -- : message
|
29
27
|
#
|
30
|
-
class Config
|
28
|
+
class Config < ConfigFile
|
31
29
|
include Singleton
|
32
30
|
|
33
31
|
class << self
|
@@ -36,37 +34,23 @@ module Libis
|
|
36
34
|
|
37
35
|
# For each configuration parameter, the value can be accessed via the class or the Singleton instance.
|
38
36
|
def method_missing(name, *args, &block)
|
39
|
-
instance.send(name, *args, &block)
|
37
|
+
result = instance.send(name, *args, &block)
|
38
|
+
self === result ? self : result
|
40
39
|
end
|
41
40
|
|
42
41
|
end
|
43
42
|
|
44
43
|
# Load configuration parameters from a YAML file or Hash.
|
45
44
|
#
|
46
|
-
# The
|
47
|
-
# loaded. Instead of a YAML file, a Hash can be passed. The file paths and Hashes are memorised and loaded again
|
48
|
-
# by the {#reload} methods.
|
45
|
+
# The file paths and Hashes are memorised and loaded again by the {#reload} methods.
|
49
46
|
# @param [String,Hash] file_or_hash
|
50
47
|
def <<(file_or_hash)
|
51
|
-
|
52
|
-
hash = case file_or_hash
|
53
|
-
when Hash
|
54
|
-
@sources << file_or_hash
|
55
|
-
file_or_hash
|
56
|
-
when String
|
57
|
-
return unless File.exist?(file_or_hash)
|
58
|
-
@sources << File.absolute_path(file_or_hash)
|
59
|
-
data = ERB.new(open(file_or_hash).read).result
|
60
|
-
# noinspection RubyResolve
|
61
|
-
YAML.load(data).to_hash rescue {}
|
62
|
-
else
|
63
|
-
{}
|
64
|
-
end
|
65
|
-
@data.merge! hash.key_symbols_to_strings recursive: true
|
66
|
-
self
|
48
|
+
super(file_or_hash) { |data| @sources << data }
|
67
49
|
end
|
68
50
|
|
69
|
-
# Load all files and Hashes again.
|
51
|
+
# Load all files and Hashes again.
|
52
|
+
#
|
53
|
+
# Will not reset the configuration parameters. Parameters set directly on the
|
70
54
|
# configuration are kept intact unless they also exist in the files or hashes in which case they will be overwritten.
|
71
55
|
def reload
|
72
56
|
sources = @sources.dup
|
@@ -75,39 +59,28 @@ module Libis
|
|
75
59
|
self
|
76
60
|
end
|
77
61
|
|
78
|
-
|
62
|
+
alias_method :deep_struct_clear!, :clear!
|
63
|
+
|
64
|
+
# Clear data and load all files and Hashes again.
|
65
|
+
#
|
66
|
+
# All configuration parameters are first deleted which means that any parameters
|
79
67
|
# added directly (not via file or hash) will no longer be available. Parameters set explicitly that also exist in
|
80
68
|
# the files or hashes will be reset to the values in those files and hashes.
|
81
69
|
def reload!
|
82
|
-
|
70
|
+
deep_struct_clear!
|
83
71
|
reload
|
84
72
|
end
|
85
73
|
|
86
|
-
#
|
87
|
-
#
|
88
|
-
#
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
# @param [Object] value the new value for the parameter
|
97
|
-
# @return [Object] parameter value
|
98
|
-
def []=(name, value)
|
99
|
-
@data.store(name.to_s, value)
|
100
|
-
end
|
101
|
-
|
102
|
-
# Return the ::Logger instance.
|
103
|
-
def logger
|
104
|
-
@logger
|
105
|
-
end
|
106
|
-
|
107
|
-
# Set the ::Logger instance.
|
108
|
-
# @param [::Logger] my_logger new logger instance
|
109
|
-
def logger=(my_logger)
|
110
|
-
@logger = my_logger
|
74
|
+
# Clear all data.
|
75
|
+
#
|
76
|
+
# Not only all configuration parameters are deleted, but also the memoirized list of loaded files
|
77
|
+
# and hashes are cleared and the logger configuration is reset to it's default status.
|
78
|
+
def clear!
|
79
|
+
super
|
80
|
+
@sources = Array.new
|
81
|
+
@logger = ::Logger.new(STDOUT)
|
82
|
+
set_log_formatter
|
83
|
+
self
|
111
84
|
end
|
112
85
|
|
113
86
|
# Set the ::Logger instance's formatter.
|
@@ -118,40 +91,20 @@ module Libis
|
|
118
91
|
#
|
119
92
|
# @param [Proc] formatter the formatter procedure or nil for default formatter
|
120
93
|
def set_log_formatter(formatter = nil)
|
121
|
-
logger.formatter = formatter || proc do |severity, time, progname, msg|
|
94
|
+
self.logger.formatter = formatter || proc do |severity, time, progname, msg|
|
122
95
|
"%s, [%s#%d] %5s -- %s: %s\n" % [severity[0..0],
|
123
96
|
(time.strftime('%Y-%m-%dT%H:%M:%S.') << '%06d ' % time.usec),
|
124
97
|
$$, severity, progname, msg]
|
125
98
|
end
|
126
99
|
end
|
127
100
|
|
128
|
-
|
101
|
+
attr_accessor :logger
|
129
102
|
|
130
|
-
|
131
|
-
key = name.to_s
|
132
|
-
if name.to_s =~ /^(.*)(=)$/
|
133
|
-
key = $1
|
134
|
-
end
|
135
|
-
if @data.has_key?(key)
|
136
|
-
if key =~/^\w+$/ # not all key names are safe to use as method names
|
137
|
-
self.instance_eval <<-END
|
138
|
-
def #{key}
|
139
|
-
self['#{key}']
|
140
|
-
end
|
141
|
-
def #{name}=(value)
|
142
|
-
self['#{name}'] = value
|
143
|
-
end
|
144
|
-
END
|
145
|
-
end
|
146
|
-
end
|
147
|
-
($2.nil? || $2.empty?) ? (@data.fetch(key) rescue nil) : @data.store(key, args.first)
|
148
|
-
end
|
103
|
+
protected
|
149
104
|
|
150
105
|
def initialize
|
151
|
-
|
152
|
-
|
153
|
-
@logger = ::Logger.new(STDOUT)
|
154
|
-
set_log_formatter
|
106
|
+
super
|
107
|
+
clear!
|
155
108
|
end
|
156
109
|
|
157
110
|
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'singleton'
|
3
|
+
require 'yaml'
|
4
|
+
require 'erb'
|
5
|
+
require 'logger'
|
6
|
+
|
7
|
+
require 'libis/tools/deep_struct'
|
8
|
+
|
9
|
+
module Libis
|
10
|
+
module Tools
|
11
|
+
|
12
|
+
# The ConfigFile class is a convenience class for interfacing with YAML configuration files. These files can
|
13
|
+
# contain ERB statements. An initial hash or file can be loaded during initialization. The class supports loading
|
14
|
+
# and saving of files, but note that any ERB statements in the file are lost by performing such a round trip.
|
15
|
+
# The class is derived from the DeepStruct class and therefore supports nested hashes and arrays and supports
|
16
|
+
# the OpenStruct style of accessors.
|
17
|
+
#
|
18
|
+
# The parameters can be accessed by getter/setter method or using the Hash syntax:
|
19
|
+
#
|
20
|
+
# require 'libis/tools/config_file'
|
21
|
+
# cfg_file = ::Libis::Tools::ConfigFile.new
|
22
|
+
# cfg_file << {foo: 'bar'}
|
23
|
+
# cfg_file.my_value = 10
|
24
|
+
# p cfg_file[:my_value] # => 10
|
25
|
+
# cfg_file{:my_text] = 'abc'
|
26
|
+
# p cfg_file['my_text'] # => 'abc'
|
27
|
+
# p cfg_file.to_hash # => { :foo => 'bar', 'my_value' => 10, :my_text => 'abc' }
|
28
|
+
# cfg >> 'my_config.yml'
|
29
|
+
#
|
30
|
+
class ConfigFile < DeepStruct
|
31
|
+
|
32
|
+
# Create a new ConfigFile instance. The optional argument can either be a Hash or a String. The argument is
|
33
|
+
# passed to the {#<<} method after initialization.
|
34
|
+
#
|
35
|
+
# @param [String,Hash] file_or_hash optional String or Hash argument to initialize the data.
|
36
|
+
def initialize(file_or_hash = nil)
|
37
|
+
super
|
38
|
+
self << file_or_hash
|
39
|
+
end
|
40
|
+
|
41
|
+
# Load configuration parameters from a YAML file or Hash.
|
42
|
+
#
|
43
|
+
# The YAML file can contain ERB syntax values that will be evaluated at loading time. Instead of a YAML file,
|
44
|
+
# a Hash can be passed.
|
45
|
+
#
|
46
|
+
# Note that the method also yields the hash or absolute path to a given block. This is for data management of
|
47
|
+
# derived classes such as ::Libis::Tools::Config.
|
48
|
+
#
|
49
|
+
# @param [String,Hash] file_or_hash optional String or Hash argument to initialize the data.
|
50
|
+
def <<(file_or_hash)
|
51
|
+
return self if file_or_hash.nil?
|
52
|
+
hash = case file_or_hash
|
53
|
+
when Hash
|
54
|
+
yield file_or_hash if block_given?
|
55
|
+
file_or_hash
|
56
|
+
when String
|
57
|
+
return unless File.exist?(file_or_hash)
|
58
|
+
yield File.absolute_path(file_or_hash) if block_given?
|
59
|
+
# noinspection RubyResolve
|
60
|
+
YAML.load(ERB.new(open(file_or_hash).read).result) rescue {}
|
61
|
+
else
|
62
|
+
{}
|
63
|
+
end
|
64
|
+
return self unless hash.is_a? Hash
|
65
|
+
hash.each { |key, value| self[key] = value }
|
66
|
+
self
|
67
|
+
end
|
68
|
+
|
69
|
+
# Save configuration parameters in a YAML file.
|
70
|
+
#
|
71
|
+
# @param [String] file path of the YAML file to save the configuration to.
|
72
|
+
def >>(file)
|
73
|
+
File.open(file, 'w') { |f| f.write to_hash.to_yaml }
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -3,13 +3,24 @@ require 'recursive-open-struct'
|
|
3
3
|
|
4
4
|
module Libis
|
5
5
|
module Tools
|
6
|
+
|
7
|
+
# A class that derives from OpenStruct through the RecursiveOpenStruct. A RecursiveOpenStruct is derived from
|
8
|
+
# stdlib's OpenStruct, but can be made recursive. DeepStruct enforces this behaviour and adds a clear! method.
|
6
9
|
class DeepStruct < RecursiveOpenStruct
|
7
10
|
|
8
11
|
def initialize(hash = {}, opts = {})
|
12
|
+
hash = {} unless hash
|
13
|
+
opts = {} unless opts
|
9
14
|
hash = {default: hash} unless hash.is_a? Hash
|
10
15
|
super(hash, {recurse_over_arrays: true}.merge(opts))
|
11
16
|
end
|
12
17
|
|
18
|
+
# Delete all data fields
|
19
|
+
def clear!
|
20
|
+
@table.keys.each { |key| delete_field(key) }
|
21
|
+
@sub_elements = {}
|
22
|
+
end
|
23
|
+
|
13
24
|
end
|
14
25
|
end
|
15
26
|
end
|
data/lib/libis/tools/version.rb
CHANGED
data/libis-tools.gemspec
CHANGED
@@ -36,6 +36,6 @@ Gem::Specification.new do |spec|
|
|
36
36
|
spec.add_runtime_dependency 'nokogiri', '~> 1.6'
|
37
37
|
spec.add_runtime_dependency 'gyoku', '~> 1.2'
|
38
38
|
spec.add_runtime_dependency 'nori', '~> 2.4'
|
39
|
-
spec.add_runtime_dependency 'recursive-open-struct', '~>0.6'
|
39
|
+
spec.add_runtime_dependency 'recursive-open-struct', '~>0.6.4'
|
40
40
|
|
41
41
|
end
|
data/spec/command_spec.rb
CHANGED
@@ -4,18 +4,22 @@ require 'libis/tools/command'
|
|
4
4
|
|
5
5
|
describe 'Command' do
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
Dir.
|
10
|
-
|
11
|
-
|
12
|
-
|
7
|
+
let!(:dir) { Dir.pwd }
|
8
|
+
let(:entries) {
|
9
|
+
Dir.entries('.').inject([]) do |array, value|
|
10
|
+
array << value unless File.directory?(value)
|
11
|
+
array
|
12
|
+
end.sort
|
13
|
+
}
|
14
|
+
|
15
|
+
before(:each) { Dir.chdir(File.join(File.dirname(__FILE__),'data')) }
|
16
|
+
after(:each) { Dir.chdir(dir) }
|
13
17
|
|
14
18
|
it 'should run ls command' do
|
15
19
|
|
16
20
|
result = Libis::Tools::Command.run('ls')
|
17
21
|
|
18
|
-
expect(result[:out]).to
|
22
|
+
expect(result[:out].sort).to match entries
|
19
23
|
expect(result[:err]).to eq []
|
20
24
|
expect(result[:status]).to eq 0
|
21
25
|
|
@@ -23,15 +27,11 @@ describe 'Command' do
|
|
23
27
|
|
24
28
|
it 'should run ls command with an option' do
|
25
29
|
|
26
|
-
result = Libis::Tools::Command.run('ls', '-
|
30
|
+
result = Libis::Tools::Command.run('ls', '-1')
|
27
31
|
|
28
32
|
output = result[:out]
|
29
|
-
expect(output.size).to eq
|
30
|
-
expect(output.
|
31
|
-
expect(output[1]).to match /^-[rwx-]{9}.+test.data$/
|
32
|
-
expect(output[2]).to match /^-[rwx-]{9}.+test.xml$/
|
33
|
-
expect(output[3]).to match /^-[rwx-]{9}.+test.yml$/
|
34
|
-
|
33
|
+
expect(output.size).to eq (entries.size)
|
34
|
+
expect(output.sort).to match entries
|
35
35
|
expect(result[:err]).to eq []
|
36
36
|
expect(result[:status]).to eq 0
|
37
37
|
|
@@ -39,17 +39,13 @@ describe 'Command' do
|
|
39
39
|
|
40
40
|
it 'should run ls command with multiple options' do
|
41
41
|
|
42
|
-
result = Libis::Tools::Command.run('ls', '-
|
42
|
+
result = Libis::Tools::Command.run('ls', '-1', '-a', '-p')
|
43
43
|
|
44
44
|
output = result[:out]
|
45
|
-
expect(output.size).to eq
|
46
|
-
expect(output
|
47
|
-
expect(output[1]).to
|
48
|
-
expect(output[2]).to match
|
49
|
-
expect(output[3]).to match /^-[rwx-]{9}.+test.data$/
|
50
|
-
expect(output[4]).to match /^-[rwx-]{9}.+test.xml$/
|
51
|
-
expect(output[5]).to match /^-[rwx-]{9}.+test.yml$/
|
52
|
-
|
45
|
+
expect(output.size).to eq (entries.size + 2)
|
46
|
+
expect(output[0]).to eq './'
|
47
|
+
expect(output[1]).to eq '../'
|
48
|
+
expect(output[2..-1].sort).to match entries
|
53
49
|
expect(result[:err]).to eq []
|
54
50
|
expect(result[:status]).to eq 0
|
55
51
|
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require_relative 'spec_helper'
|
3
|
+
require 'libis/tools/config_file'
|
4
|
+
|
5
|
+
describe ::Libis::Tools::ConfigFile do
|
6
|
+
|
7
|
+
let!(:test_file) { File.join(File.dirname(__FILE__), 'data', 'test_config.yml') }
|
8
|
+
|
9
|
+
let(:hash) {
|
10
|
+
{
|
11
|
+
a: {x: 0, y: 0, z: 0},
|
12
|
+
b: {x: 10, y: -5, z: 2.5},
|
13
|
+
c: [
|
14
|
+
[
|
15
|
+
{a: [{a1: 1, a2: 2}]},
|
16
|
+
]
|
17
|
+
]
|
18
|
+
}
|
19
|
+
}
|
20
|
+
|
21
|
+
context 'after default initialization' do
|
22
|
+
|
23
|
+
it 'has empty hash' do
|
24
|
+
# noinspection RubyResolve
|
25
|
+
expect(subject.to_hash).to be_empty
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'loads from file' do
|
29
|
+
subject << test_file
|
30
|
+
expect(subject.to_hash).to eq hash
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
data/spec/config_spec.rb
CHANGED
@@ -4,83 +4,109 @@ require 'libis/tools/config'
|
|
4
4
|
|
5
5
|
describe 'Config' do
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
before(:all) do
|
8
|
+
::Libis::Tools::Config << {appname: 'LIBIS Default'}
|
9
|
+
end
|
10
|
+
|
11
|
+
subject(:config) { ::Libis::Tools::Config.clear! }
|
10
12
|
|
11
|
-
it '
|
12
|
-
expect(config.appname).to eq 'LIBIS Default'
|
13
|
+
it 'has defaults set' do
|
13
14
|
expect(config.logger).to be_a ::Logger
|
14
15
|
end
|
15
16
|
|
16
|
-
|
17
|
-
|
18
|
-
config
|
19
|
-
|
20
|
-
|
17
|
+
it 'clears all values' do
|
18
|
+
# noinspection RubyResolve
|
19
|
+
config.test_value = 5
|
20
|
+
config.logger.level = ::Logger::FATAL
|
21
|
+
# noinspection RubyResolve
|
22
|
+
expect(config.test_value).to be 5
|
23
|
+
expect(config.logger.level).to be ::Logger::FATAL
|
21
24
|
|
22
|
-
|
23
|
-
|
24
|
-
expect(config.
|
25
|
+
config.clear!
|
26
|
+
# noinspection RubyResolve
|
27
|
+
expect(config.test_value).to be_nil
|
28
|
+
expect(config.logger.level).to be ::Logger::DEBUG
|
25
29
|
end
|
26
30
|
|
27
|
-
|
28
|
-
it 'should allow to set and get in different ways' do
|
29
|
-
config.test_value = 10
|
30
|
-
expect(config.test_value).to be 10
|
31
|
-
expect(config['test_value']).to be 10
|
32
|
-
expect(config[:test_value]).to be 10
|
33
|
-
expect(config.instance.test_value).to be 10
|
34
|
-
expect(config.instance['test_value']).to be 10
|
35
|
-
expect(config.instance[:test_value]).to be 10
|
36
|
-
|
37
|
-
config[:test_value] = 11
|
38
|
-
expect(config.test_value).to be 11
|
39
|
-
expect(config['test_value']).to be 11
|
40
|
-
expect(config[:test_value]).to be 11
|
41
|
-
expect(config.instance.test_value).to be 11
|
42
|
-
expect(config.instance['test_value']).to be 11
|
43
|
-
expect(config.instance[:test_value]).to be 11
|
44
|
-
|
45
|
-
config['test_value'] = 12
|
46
|
-
expect(config.test_value).to be 12
|
47
|
-
expect(config['test_value']).to be 12
|
48
|
-
expect(config[:test_value]).to be 12
|
49
|
-
expect(config.instance.test_value).to be 12
|
50
|
-
expect(config.instance['test_value']).to be 12
|
51
|
-
expect(config.instance[:test_value]).to be 12
|
52
|
-
end
|
31
|
+
context 'adding values with setters' do
|
53
32
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
expect(config.instance['appname']).to be config.instance.appname
|
59
|
-
expect(config.instance[:appname]).to be config.instance.appname
|
33
|
+
it 'by symbol' do
|
34
|
+
config[:test_value] = 5
|
35
|
+
expect(config['test_value']).to be 5
|
36
|
+
end
|
60
37
|
|
61
|
-
|
62
|
-
|
63
|
-
|
38
|
+
it 'by name' do
|
39
|
+
config['test_value'] = 6
|
40
|
+
# noinspection RubyResolve
|
41
|
+
expect(config.test_value).to be 6
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'by method' do
|
45
|
+
# noinspection RubyResolve
|
46
|
+
config.test_value = 7
|
47
|
+
expect(config[:test_value]).to be 7
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'allows to set on instance' do
|
51
|
+
config.instance[:test_value] = :abc
|
52
|
+
# noinspection RubyResolve
|
53
|
+
expect(config.test_value).to be :abc
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'allows to set on class' do
|
57
|
+
# noinspection RubyResolve
|
58
|
+
config.test_value = :def
|
59
|
+
expect(config.instance[:test_value]).to be :def
|
60
|
+
end
|
64
61
|
|
65
|
-
# noinspection RubyResolve
|
66
|
-
it 'should reset only values loaded from file' do
|
67
|
-
config[:appname] = 'foo'
|
68
|
-
config[:process] = 'bar'
|
69
|
-
config[:baz] = 'qux'
|
70
|
-
|
71
|
-
expect(config.appname).to eq 'foo'
|
72
|
-
expect(config.process).to eq 'bar'
|
73
|
-
expect(config.baz).to eq 'qux'
|
74
|
-
|
75
|
-
config.reload
|
76
|
-
expect(config.appname).to eq 'LIBIS Default'
|
77
|
-
expect(config.process).to eq 'Test Configuration'
|
78
|
-
expect(config.baz).to eq 'qux'
|
79
|
-
|
80
|
-
config.reload!
|
81
|
-
expect(config.appname).to eq 'LIBIS Default'
|
82
|
-
expect(config.process).to eq 'Test Configuration'
|
83
|
-
expect(config.baz).to be_nil
|
84
62
|
end
|
85
63
|
|
64
|
+
context 'loading from file' do
|
65
|
+
|
66
|
+
let(:test_file) { File.join(File.dirname(__FILE__), 'data', 'test.yml') }
|
67
|
+
subject(:config) { Libis::Tools::Config.clear! << test_file }
|
68
|
+
|
69
|
+
it 'has configuration parameters set' do
|
70
|
+
expect(config.process).to eq 'Test Configuration'
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'resets only values loaded from file' do
|
74
|
+
config[:process] = 'foo'
|
75
|
+
config[:bar] = 'qux'
|
76
|
+
|
77
|
+
expect(config.process).to eq 'foo'
|
78
|
+
expect(config.bar).to eq 'qux'
|
79
|
+
|
80
|
+
config.reload
|
81
|
+
expect(config.process).to eq 'Test Configuration'
|
82
|
+
expect(config.bar).to eq 'qux'
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'resets all values' do
|
86
|
+
config[:process] = 'foo'
|
87
|
+
config[:bar] = 'qux'
|
88
|
+
|
89
|
+
expect(config.process).to eq 'foo'
|
90
|
+
expect(config.bar).to eq 'qux'
|
91
|
+
|
92
|
+
config.reload!
|
93
|
+
expect(config.process).to eq 'Test Configuration'
|
94
|
+
expect(config.bar).to be_nil
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'clears all values' do
|
98
|
+
config[:process] = 'foo'
|
99
|
+
config[:bar] = 'qux'
|
100
|
+
|
101
|
+
expect(config.process).to eq 'foo'
|
102
|
+
expect(config.bar).to eq 'qux'
|
103
|
+
|
104
|
+
config.clear!
|
105
|
+
expect(config.process).to be_nil
|
106
|
+
expect(config.bar).to be_nil
|
107
|
+
# noinspection RubyResolve
|
108
|
+
expect(config.to_h).to be_empty
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
86
112
|
end
|
data/spec/data/test.yml
CHANGED
data/spec/deep_struct_spec.rb
CHANGED
@@ -4,79 +4,135 @@ require 'libis/tools/deep_struct'
|
|
4
4
|
|
5
5
|
describe 'DeepStruct' do
|
6
6
|
|
7
|
-
|
8
|
-
recursive_hash = {
|
9
|
-
a: {x: 0, y: 0, z: 0},
|
10
|
-
b: {x: 10, y: -5, z: 2.5},
|
11
|
-
c: [
|
12
|
-
[
|
13
|
-
{a: 1},
|
14
|
-
{a: 2}
|
15
|
-
]
|
16
|
-
]
|
17
|
-
}
|
18
|
-
|
19
|
-
it 'should initialize' do
|
20
|
-
ds = Libis::Tools::DeepStruct.new
|
21
|
-
expect(ds).not_to eq nil
|
22
|
-
end
|
7
|
+
context 'default contructor' do
|
23
8
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
9
|
+
subject(:ds) { Libis::Tools::DeepStruct.new }
|
10
|
+
|
11
|
+
it 'should initialize' do
|
12
|
+
is_expected.not_to be_nil
|
13
|
+
# noinspection RubyResolve
|
14
|
+
expect(ds.to_hash).to be_empty
|
15
|
+
end
|
31
16
|
|
32
|
-
it 'should allow access through methods' do
|
33
|
-
ds = Libis::Tools::DeepStruct.new hash
|
34
|
-
expect(ds.a).to eq 1
|
35
|
-
expect(ds.b).to eq '2'
|
36
|
-
expect(ds.c).to eq 3.0
|
37
17
|
end
|
38
18
|
|
39
|
-
|
40
|
-
ds = Libis::Tools::DeepStruct.new hash
|
41
|
-
expect(ds.a).to eq 1
|
42
|
-
expect(ds.b).to eq '2'
|
43
|
-
expect(ds.c).to eq 3.0
|
19
|
+
context 'contructed with hash' do
|
44
20
|
|
45
|
-
|
46
|
-
|
47
|
-
expect(ds['c']).to eq 3.0
|
21
|
+
let(:hash) { {a: 1, 'b' => '2', c: 3.0} }
|
22
|
+
subject(:ds) { Libis::Tools::DeepStruct.new(hash)}
|
48
23
|
|
49
|
-
|
50
|
-
|
51
|
-
|
24
|
+
it 'has Hash values initialized' do
|
25
|
+
expect(ds[:a]).to eq 1
|
26
|
+
expect(ds[:b]).to eq '2'
|
27
|
+
expect(ds[:c]).to eq 3.0
|
28
|
+
expect(ds.to_hash).to eq hash
|
29
|
+
end
|
52
30
|
|
53
|
-
|
54
|
-
|
31
|
+
it 'allows access through methods' do
|
32
|
+
expect(ds.a).to eq 1
|
33
|
+
expect(ds.b).to eq '2'
|
34
|
+
expect(ds.c).to eq 3.0
|
55
35
|
|
56
|
-
|
36
|
+
ds.a = 5
|
37
|
+
expect(ds.a).to be 5
|
38
|
+
expect(ds[:a]).to be 5
|
39
|
+
expect(ds['a']).to be 5
|
40
|
+
end
|
57
41
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
42
|
+
it 'allows access through key strings' do
|
43
|
+
expect(ds['a']).to eq 1
|
44
|
+
expect(ds['b']).to eq '2'
|
45
|
+
expect(ds['c']).to eq 3.0
|
46
|
+
|
47
|
+
ds['a'] = 5
|
48
|
+
expect(ds.a).to be 5
|
49
|
+
expect(ds[:a]).to be 5
|
50
|
+
expect(ds['a']).to be 5
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'allows access through key symbols' do
|
54
|
+
expect(ds[:a]).to eq 1
|
55
|
+
expect(ds[:b]).to eq '2'
|
56
|
+
expect(ds[:c]).to eq 3.0
|
57
|
+
|
58
|
+
ds[:a] = 5
|
59
|
+
expect(ds.a).to be 5
|
60
|
+
expect(ds[:a]).to be 5
|
61
|
+
expect(ds['a']).to be 5
|
62
|
+
end
|
62
63
|
|
63
|
-
it 'should store recursive Hashes as DeepStructs' do
|
64
|
-
ds = Libis::Tools::DeepStruct.new(recursive_hash)
|
65
|
-
expect(ds[:a]).to be_a Libis::Tools::DeepStruct
|
66
|
-
expect(ds.b).to be_a Libis::Tools::DeepStruct
|
67
64
|
end
|
68
65
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
66
|
+
context 'initialized with non-hash' do
|
67
|
+
|
68
|
+
subject(:ds) { Libis::Tools::DeepStruct.new 'abc' }
|
69
|
+
|
70
|
+
it 'stores value in hash with :default key' do
|
71
|
+
expect(ds[:default]).to eq 'abc'
|
72
|
+
end
|
73
|
+
|
74
74
|
end
|
75
75
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
76
|
+
context 'initialized with nil value' do
|
77
|
+
|
78
|
+
subject(:ds) { Libis::Tools::DeepStruct.new nil }
|
79
|
+
|
80
|
+
it 'has no data' do
|
81
|
+
# noinspection RubyResolve
|
82
|
+
expect(ds.to_hash).to be_empty
|
83
|
+
end
|
84
|
+
|
80
85
|
end
|
81
86
|
|
87
|
+
context 'initialized with recursive hash' do
|
88
|
+
|
89
|
+
let(:recursive_hash) { {
|
90
|
+
a: {x: 0, y: 0, z: 0},
|
91
|
+
b: {x: 10, y: -5, z: 2.5},
|
92
|
+
c: [
|
93
|
+
[
|
94
|
+
{a: [ {a1: 1, a2: 2}] },
|
95
|
+
]
|
96
|
+
]
|
97
|
+
} }
|
98
|
+
|
99
|
+
subject(:ds) { Libis::Tools::DeepStruct.new(recursive_hash) }
|
100
|
+
|
101
|
+
it 'stores recursive Hashes as DeepStructs' do
|
102
|
+
expect(ds[:a]).to be_a Libis::Tools::DeepStruct
|
103
|
+
expect(ds.b).to be_a Libis::Tools::DeepStruct
|
104
|
+
end
|
105
|
+
|
106
|
+
it 'recurses over arrays' do
|
107
|
+
expect(ds.c[0][0]).to be_a Libis::Tools::DeepStruct
|
108
|
+
expect(ds.c.first.first.a.first.a1).to eq 1
|
109
|
+
expect(ds.c.first.first.a.first.a2).to eq 2
|
110
|
+
end
|
111
|
+
|
112
|
+
it 'can reproduce original hash' do
|
113
|
+
expect(ds.to_hash).to eq recursive_hash
|
114
|
+
expect(ds.b.to_hash).to eq recursive_hash[:b]
|
115
|
+
end
|
116
|
+
|
117
|
+
it 'clears data and methods' do
|
118
|
+
expect(ds).to respond_to 'a'
|
119
|
+
expect(ds).to respond_to 'b'
|
120
|
+
expect(ds).to respond_to 'c'
|
121
|
+
expect(ds).to respond_to 'a='
|
122
|
+
expect(ds).to respond_to 'b='
|
123
|
+
expect(ds).to respond_to 'c='
|
124
|
+
|
125
|
+
ds.clear!
|
126
|
+
expect(ds.a).to be_nil
|
127
|
+
expect(ds.b).to be_nil
|
128
|
+
expect(ds.c).to be_nil
|
129
|
+
expect(ds).not_to respond_to 'a'
|
130
|
+
expect(ds).not_to respond_to 'b'
|
131
|
+
expect(ds).not_to respond_to 'c'
|
132
|
+
expect(ds).not_to respond_to 'a='
|
133
|
+
expect(ds).not_to respond_to 'b='
|
134
|
+
expect(ds).not_to respond_to 'c='
|
135
|
+
end
|
136
|
+
|
137
|
+
end
|
82
138
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: libis-tools
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kris Dekeyser
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-05-
|
11
|
+
date: 2015-05-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -156,14 +156,14 @@ dependencies:
|
|
156
156
|
requirements:
|
157
157
|
- - "~>"
|
158
158
|
- !ruby/object:Gem::Version
|
159
|
-
version:
|
159
|
+
version: 0.6.4
|
160
160
|
type: :runtime
|
161
161
|
prerelease: false
|
162
162
|
version_requirements: !ruby/object:Gem::Requirement
|
163
163
|
requirements:
|
164
164
|
- - "~>"
|
165
165
|
- !ruby/object:Gem::Version
|
166
|
-
version:
|
166
|
+
version: 0.6.4
|
167
167
|
description: Some tool classes for other LIBIS gems.
|
168
168
|
email:
|
169
169
|
- kris.dekeyser@libis.be
|
@@ -185,6 +185,7 @@ files:
|
|
185
185
|
- lib/libis/tools/checksum.rb
|
186
186
|
- lib/libis/tools/command.rb
|
187
187
|
- lib/libis/tools/config.rb
|
188
|
+
- lib/libis/tools/config_file.rb
|
188
189
|
- lib/libis/tools/dc_record.rb
|
189
190
|
- lib/libis/tools/deep_struct.rb
|
190
191
|
- lib/libis/tools/extend/empty.rb
|
@@ -203,10 +204,12 @@ files:
|
|
203
204
|
- spec/assert_spec.rb
|
204
205
|
- spec/checksum_spec.rb
|
205
206
|
- spec/command_spec.rb
|
207
|
+
- spec/config_file_spec.rb
|
206
208
|
- spec/config_spec.rb
|
207
209
|
- spec/data/test.data
|
208
210
|
- spec/data/test.xml
|
209
211
|
- spec/data/test.yml
|
212
|
+
- spec/data/test_config.yml
|
210
213
|
- spec/deep_struct_spec.rb
|
211
214
|
- spec/logger_spec.rb
|
212
215
|
- spec/parameter_container_spec.rb
|
@@ -245,10 +248,12 @@ test_files:
|
|
245
248
|
- spec/assert_spec.rb
|
246
249
|
- spec/checksum_spec.rb
|
247
250
|
- spec/command_spec.rb
|
251
|
+
- spec/config_file_spec.rb
|
248
252
|
- spec/config_spec.rb
|
249
253
|
- spec/data/test.data
|
250
254
|
- spec/data/test.xml
|
251
255
|
- spec/data/test.yml
|
256
|
+
- spec/data/test_config.yml
|
252
257
|
- spec/deep_struct_spec.rb
|
253
258
|
- spec/logger_spec.rb
|
254
259
|
- spec/parameter_container_spec.rb
|