easy_app_helper 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,10 +1,10 @@
1
- ################################################################################
2
- # EasyAppHelper
3
- #
4
- # Copyright (c) 2013 L.Briais under MIT license
5
- # http://opensource.org/licenses/MIT
6
- ################################################################################
7
-
8
- module EasyAppHelper
9
- EASY_APP_HELPER_VERSION = "1.0.3"
10
- end
1
+ ################################################################################
2
+ # EasyAppHelper
3
+ #
4
+ # Copyright (c) 2013 L.Briais under MIT license
5
+ # http://opensource.org/licenses/MIT
6
+ ################################################################################
7
+
8
+ module EasyAppHelper
9
+ EASY_APP_HELPER_VERSION = "1.0.4"
10
+ end
@@ -0,0 +1,128 @@
1
+ #-------------------------------------------------------------------------------
2
+ #
3
+ #
4
+ # Copyright (c) 2013 L.Briais under MIT license
5
+ # http://opensource.org/licenses/MIT
6
+ #-------------------------------------------------------------------------------
7
+
8
+ require 'rspec'
9
+ require 'easy_app_helper'
10
+
11
+
12
+ #describe EasyAppHelper::Core::Config do
13
+ describe EasyAppHelper.config do
14
+ SAMPLE_STRING = 'Sample String'
15
+
16
+
17
+ it 'should be fully initialized when first accessed' do
18
+ subject.should_not be nil
19
+ subject.logger.should_not be nil
20
+ end
21
+
22
+ it 'should be consistent regarding the way it is accessed' do
23
+ subject[:basic_test] = SAMPLE_STRING
24
+ expect(subject[]).to eq subject.to_hash
25
+ expect(subject[:basic_test]).to eq SAMPLE_STRING
26
+ end
27
+
28
+
29
+ it 'should be the same object accross different instances' do
30
+ expect(subject[:basic_test]).to eq SAMPLE_STRING
31
+ end
32
+
33
+ it 'should store the data in the :modified layer' do
34
+ expect(subject.find_layer :basic_test).to eq :modified
35
+ expect(subject.internal_configs[:modified][:content][:basic_test]).to eq subject[:basic_test]
36
+
37
+ end
38
+
39
+ it 'should provide a direct r/w access to layers' do
40
+ subject.internal_configs[:system][:content][:stupid_conf] = SAMPLE_STRING
41
+ expect(subject[:stupid_conf]).to eq SAMPLE_STRING
42
+ end
43
+
44
+ it 'should be reloaded when :config-file property changes changes' do
45
+ subject.should_receive(:force_reload)
46
+ subject[:'config-file'] = SAMPLE_STRING
47
+ end
48
+
49
+ it 'should be reloaded when script_filename changes' do
50
+ subject.should_receive(:force_reload)
51
+ subject.script_filename = SAMPLE_STRING
52
+ end
53
+
54
+
55
+ describe 'should override data when present in multiple layers' do
56
+ before(:all) do
57
+ EasyAppHelper.config.layers.each do |layer|
58
+ EasyAppHelper.config.internal_configs[layer][:content][:basic_test] = "#{SAMPLE_STRING} #{layer.to_s}"
59
+ end
60
+ EasyAppHelper.config.internal_configs[:command_line][:content][:'config-file'] = true
61
+ end
62
+
63
+ context "when requesting some data" do
64
+ let(:layers) {[:modified, :command_line, :specific_file, :user, :global, :system]}
65
+
66
+ original_ordered_layers = [:modified, :command_line, :specific_file, :user, :global, :system]
67
+ layers = original_ordered_layers.dup
68
+ original_ordered_layers.each do |layer|
69
+ test_descr = "should find the data in #{layer} if present in #{layer}"
70
+ unless layers.length == original_ordered_layers.length
71
+ already_removed = original_ordered_layers - layers
72
+ if already_removed.length == 1
73
+ test_descr += " and #{already_removed[0]} level is not specified."
74
+ end
75
+ if already_removed.length > 1
76
+ test_descr += " and #{already_removed.join ', '} levels are not specified."
77
+ end
78
+ end
79
+
80
+ it test_descr, layers: layers.dup do
81
+ layers = example.metadata[:layers]
82
+ expect(subject.find_layer :basic_test).to eq layer
83
+ expect(subject[:basic_test]).to eq "#{SAMPLE_STRING} #{layer.to_s}"
84
+ subject.internal_configs[layer][:content].delete :basic_test
85
+ end
86
+
87
+ layers.shift
88
+
89
+ end
90
+ end
91
+
92
+ end
93
+
94
+ context "when reset" do
95
+
96
+ it "should remove all modifications done the standard way" do
97
+ subject[:test_remove] = SAMPLE_STRING
98
+ subject.reset
99
+ expect(subject[:test_remove]).to be_nil
100
+ end
101
+
102
+ it "should keep modifications directly done on internal layers" do
103
+ subject.internal_configs[:system][:content][:stupid_conf] = SAMPLE_STRING
104
+ subject.reset
105
+ expect(subject.internal_configs[:system][:content][:stupid_conf]).to eq SAMPLE_STRING
106
+ end
107
+
108
+ end
109
+
110
+ context "when reloaded" do
111
+
112
+ it "should keep all modifications done the standard way" do
113
+ subject[:test_remove] = SAMPLE_STRING
114
+ subject.load_config
115
+ expect(subject[:test_remove]).to eq SAMPLE_STRING
116
+ end
117
+
118
+ it "should remove all modifications directly done on internal layers" do
119
+ subject.internal_configs[:system][:content][:stupid_conf] = SAMPLE_STRING
120
+ subject.internal_configs[:command_line][:content][:stupid_conf] = SAMPLE_STRING
121
+ subject.load_config
122
+ expect(subject.internal_configs[:system][:content][:stupid_conf]).to be_nil
123
+ expect(subject.internal_configs[:command_line][:content][:stupid_conf]).to be_nil
124
+ end
125
+
126
+ end
127
+
128
+ end
@@ -0,0 +1,33 @@
1
+ #-------------------------------------------------------------------------------
2
+ #
3
+ #
4
+ # Copyright (c) 2013 L.Briais under MIT license
5
+ # http://opensource.org/licenses/MIT
6
+ #-------------------------------------------------------------------------------
7
+
8
+ require 'rspec'
9
+ require 'easy_app_helper'
10
+
11
+
12
+ #describe EasyAppHelper::Core::Logger
13
+ describe EasyAppHelper.logger do
14
+ let (:config) {EasyAppHelper.config}
15
+
16
+ context "to modify the log level" do
17
+
18
+ it 'should be ok to use the config object' do
19
+ Logger::Severity::DEBUG.upto(Logger::Severity::UNKNOWN) do |severity|
20
+ config[:'log-level'] = severity
21
+ expect(subject.level).to eq severity
22
+ end
23
+ end
24
+
25
+ it 'should be ok to use the #log_level= method' do
26
+ Logger::Severity::DEBUG.upto(Logger::Severity::UNKNOWN) do |severity|
27
+ subject.level= severity
28
+ expect(config[:'log-level']).to eq severity
29
+ end
30
+ end
31
+ end
32
+
33
+ end
@@ -1,7 +1,7 @@
1
- stupid_config: true
2
-
3
- pipo: bimbo
4
-
5
- hash_example:
6
- first: one
7
- second: two
1
+ stupid_config: true
2
+
3
+ pipo: bimbo
4
+
5
+ hash_example:
6
+ first: one
7
+ second: two
@@ -1,33 +1,33 @@
1
- #!/usr/bin/env ruby
2
-
3
-
4
- require 'easy_app_helper'
5
-
6
-
7
- # EasyAppHelper.logger.level = 0
8
- EasyAppHelper.puts_and_logs "Groovy baby !"
9
- EasyAppHelper.config[:zboubi] = "Hi shared"
10
-
11
- class A
12
- include EasyAppHelper
13
-
14
- def echo
15
- puts_and_logs config[:zboubi]
16
- end
17
- end
18
-
19
- A.new.echo
20
- EasyAppHelper.puts_and_logs EasyAppHelper.config[:zboubi]
21
-
22
- include EasyAppHelper
23
- puts_and_logs "ZBOUBI: #{config[:zboubi]}"
24
- config.reset
25
- puts_and_logs "ZBOUBI2: #{config[:zboubi]}"
26
-
27
- puts config.to_yaml
28
- config.script_filename = 'batch_audio_convert'
29
- puts 'Internal configs'
30
- puts config.internal_configs.to_yaml
31
- puts 'Resulting config'
32
- puts config.to_yaml
33
-
1
+ #!/usr/bin/env ruby
2
+
3
+
4
+ require 'easy_app_helper'
5
+
6
+
7
+ # EasyAppHelper.logger.level = 0
8
+ EasyAppHelper.puts_and_logs "Groovy baby !"
9
+ EasyAppHelper.config[:zboubi] = "Hi shared"
10
+
11
+ class A
12
+ include EasyAppHelper
13
+
14
+ def echo
15
+ puts_and_logs config[:zboubi]
16
+ end
17
+ end
18
+
19
+ A.new.echo
20
+ EasyAppHelper.puts_and_logs EasyAppHelper.config[:zboubi]
21
+
22
+ include EasyAppHelper
23
+ puts_and_logs "ZBOUBI: #{config[:zboubi]}"
24
+ config.reset
25
+ puts_and_logs "ZBOUBI2: #{config[:zboubi]}"
26
+
27
+ puts config.to_yaml
28
+ config.script_filename = 'batch_audio_convert'
29
+ puts 'Internal configs'
30
+ puts config.internal_configs.to_yaml
31
+ puts 'Resulting config'
32
+ puts config.to_yaml
33
+
@@ -1,90 +1,90 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'easy_app_helper'
4
-
5
- # You can directly access the config or the logger through the EasyAppHelper module
6
- puts "The application verbose flag is #{EasyAppHelper.config[:verbose]}"
7
-
8
- # You can directly use the logger according to the command line flags
9
- # This will do nothing unless --debug is set and --log-level is set to the correct level
10
- EasyAppHelper.logger.info "Hi guys!"
11
-
12
- # Fed up with the EasyAppHelper prefix ? Just include the module where you want
13
- include EasyAppHelper
14
-
15
- # You can override programmatically any part of the config
16
- config[:debug] = true
17
- logger.level = 1
18
- config[:test] = 'Groovy'
19
- EasyAppHelper.logger.info "Hi guys!... again"
20
-
21
- # You can see the internals of the config
22
- puts config.internal_configs.to_yaml
23
- # Which will output
24
- #:modified:
25
- # :content:
26
- # :log-level: 1
27
- # :debug: true
28
- # :test: cool
29
- # :source: Changed by code
30
- #:command_line:
31
- # :content:
32
- # :auto:
33
- # :simulate:
34
- # :verbose: true
35
- # :help:
36
- # :config-file:
37
- # :config-override:
38
- # :debug:
39
- # :debug-on-err:
40
- # :log-level:
41
- # :log-file:
42
- # :source: Command line
43
- #:system:
44
- # :content: {}
45
- # :source:
46
- # :origin: EasyAppHelper
47
- #:global:
48
- # :content: {}
49
- # :source:
50
- # :origin: ''
51
- #:user:
52
- # :content: {}
53
- # :source:
54
- # :origin: ''
55
- #:specific_file:
56
- # :content: {}
57
-
58
- # You see of course that the two modifications we did are in the modified sub-hash
59
- # And now the merged config
60
- puts config.to_hash
61
-
62
- # But you can see the modified part as it is:
63
- puts config.internal_configs[:modified]
64
-
65
- # Of course you can access it from any class
66
- class Dummy
67
- include EasyAppHelper
68
-
69
- def initialize
70
- puts "#{config[:test]} baby !"
71
- # Back to the original
72
- config.reset
73
- puts config.internal_configs[:modified]
74
- end
75
- end
76
-
77
- Dummy.new
78
-
79
- # Some methods are provided to ease common tasks. For example this one will log at info level
80
- # (so only displayed if debug mode and log level low enough), but will also puts on the console
81
- # if verbose if set...
82
- puts_and_logs "Hi world"
83
-
84
- # It is actually one of the few methods added to regular Logger class (The added value of this logger
85
- # is much more to be tightly coupled with the config object). Thus could access it like that:
86
- logger.puts_and_logs "Hi world"
87
-
88
- # or even
89
- EasyAppHelper.logger.puts_and_logs "Hi world... 3 is enough."
90
-
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'easy_app_helper'
4
+
5
+ # You can directly access the config or the logger through the EasyAppHelper module
6
+ puts "The application verbose flag is #{EasyAppHelper.config[:verbose]}"
7
+
8
+ # You can directly use the logger according to the command line flags
9
+ # This will do nothing unless --debug is set and --log-level is set to the correct level
10
+ EasyAppHelper.logger.info "Hi guys!"
11
+
12
+ # Fed up with the EasyAppHelper prefix ? Just include the module where you want
13
+ include EasyAppHelper
14
+
15
+ # You can override programmatically any part of the config
16
+ config[:debug] = true
17
+ logger.level = 1
18
+ config[:test] = 'Groovy'
19
+ EasyAppHelper.logger.info "Hi guys!... again"
20
+
21
+ # You can see the internals of the config
22
+ puts config.internal_configs.to_yaml
23
+ # Which will output
24
+ #:modified:
25
+ # :content:
26
+ # :log-level: 1
27
+ # :debug: true
28
+ # :test: cool
29
+ # :source: Changed by code
30
+ #:command_line:
31
+ # :content:
32
+ # :auto:
33
+ # :simulate:
34
+ # :verbose: true
35
+ # :help:
36
+ # :config-file:
37
+ # :config-override:
38
+ # :debug:
39
+ # :debug-on-err:
40
+ # :log-level:
41
+ # :log-file:
42
+ # :source: Command line
43
+ #:system:
44
+ # :content: {}
45
+ # :source:
46
+ # :origin: EasyAppHelper
47
+ #:global:
48
+ # :content: {}
49
+ # :source:
50
+ # :origin: ''
51
+ #:user:
52
+ # :content: {}
53
+ # :source:
54
+ # :origin: ''
55
+ #:specific_file:
56
+ # :content: {}
57
+
58
+ # You see of course that the two modifications we did are in the modified sub-hash
59
+ # And now the merged config
60
+ puts config.to_hash
61
+
62
+ # But you can see the modified part as it is:
63
+ puts config.internal_configs[:modified]
64
+
65
+ # Of course you can access it from any class
66
+ class Dummy
67
+ include EasyAppHelper
68
+
69
+ def initialize
70
+ puts "#{config[:test]} baby !"
71
+ # Back to the original
72
+ config.reset
73
+ puts config.internal_configs[:modified]
74
+ end
75
+ end
76
+
77
+ Dummy.new
78
+
79
+ # Some methods are provided to ease common tasks. For example this one will log at info level
80
+ # (so only displayed if debug mode and log level low enough), but will also puts on the console
81
+ # if verbose if set...
82
+ puts_and_logs "Hi world"
83
+
84
+ # It is actually one of the few methods added to regular Logger class (The added value of this logger
85
+ # is much more to be tightly coupled with the config object). Thus could access it like that:
86
+ logger.puts_and_logs "Hi world"
87
+
88
+ # or even
89
+ EasyAppHelper.logger.puts_and_logs "Hi world... 3 is enough."
90
+
@@ -1,36 +1,36 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'easy_app_helper'
4
-
5
- class MyApp
6
- include EasyAppHelper
7
-
8
- APP_NAME = "My super application"
9
- # SCRIPT_NAME = File.basename($0, '.*')
10
- VERSION = '0.0.1'
11
- DESCRIPTION = 'This application is a proof of concept for EasyAppHelper.'
12
-
13
-
14
- def initialize
15
- # Providing this data is optional
16
- config.describes_application(app_name: APP_NAME, app_version: VERSION, app_description: DESCRIPTION)
17
- end
18
-
19
-
20
- def run
21
- if config[:help]
22
- puts config.help
23
- exit 0
24
- end
25
- puts_and_logs "Application is starting"
26
- do_some_processing
27
- end
28
-
29
- def do_some_processing
30
- puts_and_logs "Starting some heavy processing"
31
- end
32
-
33
- end
34
-
35
-
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'easy_app_helper'
4
+
5
+ class MyApp
6
+ include EasyAppHelper
7
+
8
+ APP_NAME = "My super application"
9
+ # SCRIPT_NAME = File.basename($0, '.*')
10
+ VERSION = '0.0.1'
11
+ DESCRIPTION = 'This application is a proof of concept for EasyAppHelper.'
12
+
13
+
14
+ def initialize
15
+ # Providing this data is optional
16
+ config.describes_application(app_name: APP_NAME, app_version: VERSION, app_description: DESCRIPTION)
17
+ end
18
+
19
+
20
+ def run
21
+ if config[:help]
22
+ puts config.help
23
+ exit 0
24
+ end
25
+ puts_and_logs "Application is starting"
26
+ do_some_processing
27
+ end
28
+
29
+ def do_some_processing
30
+ puts_and_logs "Starting some heavy processing"
31
+ end
32
+
33
+ end
34
+
35
+
36
36
  MyApp.new.run