easy_app_helper 1.0.2 → 1.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.
data/Rakefile CHANGED
@@ -1 +1 @@
1
- require "bundler/gem_tasks"
1
+ require "bundler/gem_tasks"
@@ -1,26 +1,26 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'easy_app_helper/version'
5
-
6
- Gem::Specification.new do |spec|
7
- spec.name = "easy_app_helper"
8
- spec.version = EasyAppHelper::EASY_APP_HELPER_VERSION
9
- spec.authors = ["L.Briais"]
10
- spec.email = ["lbnetid+rb@gmail.com"]
11
- spec.description = %q{Easy Application Helpers framework}
12
- spec.summary = %q{Provides cool helpers to your application, including configuration and logging features}
13
- spec.homepage = "https://github.com/lbriais/easy_app_helper"
14
- spec.license = "MIT"
15
-
16
- spec.files = `git ls-files`.split($/)
17
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
- spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ["lib"]
20
-
21
- spec.add_development_dependency "bundler"
22
- spec.add_development_dependency "rake"
23
- spec.add_development_dependency "pry"
24
-
25
- spec.add_runtime_dependency "slop"
26
- end
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'easy_app_helper/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "easy_app_helper"
8
+ spec.version = EasyAppHelper::EASY_APP_HELPER_VERSION
9
+ spec.authors = ["L.Briais"]
10
+ spec.email = ["lbnetid+rb@gmail.com"]
11
+ spec.description = %q{Easy Application Helpers framework}
12
+ spec.summary = %q{Provides cool helpers to your application, including configuration and logging features}
13
+ spec.homepage = "https://github.com/lbriais/easy_app_helper"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "pry"
24
+
25
+ spec.add_runtime_dependency "slop"
26
+ end
@@ -1,129 +1,142 @@
1
- ################################################################################
2
- # EasyAppHelper
3
- #
4
- # Copyright (c) 2013 L.Briais under MIT license
5
- # http://opensource.org/licenses/MIT
6
- ################################################################################
7
-
8
- require 'slop'
9
-
10
- # This class is the base class for the {EasyAppHelper::Core::Config config} object.
11
- # It handles the internal_configs hash that actually contains all configurations read from
12
- # various sources: command line, config files etc...
13
-
14
- class EasyAppHelper::Core::Base
15
- CHANGED_BY_CODE = 'Changed by code'
16
-
17
- attr_reader :script_filename, :app_name, :app_version, :app_description, :internal_configs, :logger
18
-
19
- def initialize(logger)
20
- @app_name = @app_version = @app_description = ""
21
- @script_filename = File.basename $0, '.*'
22
- @internal_configs = {modified: {content: {}, source: CHANGED_BY_CODE}}
23
- @logger = logger
24
- @slop_definition = Slop.new
25
- build_command_line_options
26
- end
27
-
28
-
29
- # @return [String] The formatted command line help
30
- def help
31
- @slop_definition.to_s
32
- end
33
-
34
- # sets the filename while maintaining the slop definition upto date
35
- # @param [String] filename
36
- def script_filename=(filename)
37
- @script_filename = filename
38
- @slop_definition.banner = build_banner
39
- end
40
- # sets the application name used for logging while maintaining the slop definition upto date
41
- # @param [String] fname
42
- def app_name=(name)
43
- @app_name = name
44
- @slop_definition.banner = build_banner
45
- end
46
- # sets the version while maintaining the slop definition upto date
47
- # @param [String] version
48
- def app_version=(version)
49
- @app_version = version
50
- @slop_definition.banner = build_banner
51
- end
52
- # sets the filename while maintaining the slop definition upto date
53
- # @param [String] description
54
- def app_description=(description)
55
- @app_description = description
56
- @slop_definition.banner = build_banner
57
- end
58
-
59
- # helper to add in one command any of the four base properties used
60
- # by the logger and the config objects.
61
- # @param [String] app_name
62
- # @param [String] script_filename
63
- # @param [String] app_version
64
- # @param [String] app_description
65
- def describes_application(app_name: nil, script_filename: nil, app_version: nil, app_description: nil)
66
- self.app_name = app_name unless app_name.nil?
67
- self.app_version = app_version unless app_version.nil?
68
- self.app_description = app_description unless app_description.nil?
69
- self.script_filename = script_filename unless script_filename.nil?
70
- end
71
-
72
- # @return [Hash] This hash built from slop definition correspond to the :command_line layer of internal_configs
73
- def command_line_config
74
- @slop_definition.parse
75
- @slop_definition.to_hash
76
- end
77
-
78
- # Yields a slop definition to modify the command line parameters
79
- # @param [String] title used to insert a slop separator
80
- def add_command_line_section(title='Script specific')
81
- raise "Incorrect usage" unless block_given?
82
- @slop_definition.separator build_separator(title)
83
- yield @slop_definition
84
- end
85
-
86
- # Sets the :command_line layer of internal_configs to the computed {#command_line_config}
87
- def load_config
88
- internal_configs[:command_line] = {content: command_line_config, source: 'Command line'}
89
- end
90
-
91
- # Any modification done to the config is in fact stored in the :modified layer of internal_configs
92
- # @param [String] key
93
- # @param [String] value
94
- def []=(key,value)
95
- internal_configs[:modified][:content][key] = value
96
- end
97
-
98
- # Reset the :modified layer of internal_configs rolling back any change done to the config
99
- def reset
100
- internal_configs[:modified] = {content: {}, source: CHANGED_BY_CODE}
101
- end
102
-
103
-
104
- # @return [Array] List of layers
105
- def layers
106
- internal_configs.keys
107
- end
108
-
109
- private
110
-
111
- def build_separator(title)
112
- "-- #{title} ".ljust 80, '-'
113
- end
114
-
115
- # Builds common used command line options
116
- def build_command_line_options
117
- add_command_line_section('Generic options') do |slop|
118
- slop.on :auto, 'Auto mode. Bypasses questions to user.', :argument => false
119
- slop.on :simulate, 'Do not perform the actual underlying actions.', :argument => false
120
- slop.on :v, :verbose, 'Enable verbose mode.', :argument => false
121
- slop.on :h, :help, 'Displays this help.', :argument => false
122
- end
123
- end
124
-
125
- def build_banner
126
- "\nUsage: #{script_filename} [options]\n#{app_name} Version: #{app_version}\n\n#{app_description}"
127
- end
128
-
1
+ ################################################################################
2
+ # EasyAppHelper
3
+ #
4
+ # Copyright (c) 2013 L.Briais under MIT license
5
+ # http://opensource.org/licenses/MIT
6
+ ################################################################################
7
+
8
+ require 'slop'
9
+
10
+ # This class is the base class for the {EasyAppHelper::Core::Config config} object.
11
+ # It handles the internal_configs hash that actually contains all configurations read from
12
+ # various sources: command line, config files etc...
13
+
14
+ class EasyAppHelper::Core::Base
15
+ CHANGED_BY_CODE = 'Changed by code'
16
+
17
+ attr_reader :script_filename, :app_name, :app_version, :app_description, :internal_configs, :logger
18
+
19
+ def initialize(logger)
20
+ @app_name = @app_version = @app_description = ""
21
+ @script_filename = File.basename $0, '.*'
22
+ @internal_configs = {modified: {content: {}, source: CHANGED_BY_CODE}}
23
+ @logger = logger
24
+ @slop_definition = Slop.new
25
+ build_command_line_options
26
+ end
27
+
28
+
29
+ # @return [String] The formatted command line help
30
+ def help
31
+ @slop_definition.to_s
32
+ end
33
+
34
+ # sets the filename while maintaining the slop definition upto date
35
+ # @param [String] filename
36
+ def script_filename=(filename)
37
+ @script_filename = filename
38
+ @slop_definition.banner = build_banner
39
+ end
40
+ # sets the application name used for logging while maintaining the slop definition upto date
41
+ # @param [String] fname
42
+ def app_name=(name)
43
+ @app_name = name
44
+ @slop_definition.banner = build_banner
45
+ end
46
+ # sets the version while maintaining the slop definition upto date
47
+ # @param [String] version
48
+ def app_version=(version)
49
+ @app_version = version
50
+ @slop_definition.banner = build_banner
51
+ end
52
+ # sets the filename while maintaining the slop definition upto date
53
+ # @param [String] description
54
+ def app_description=(description)
55
+ @app_description = description
56
+ @slop_definition.banner = build_banner
57
+ end
58
+
59
+ # helper to add in one command any of the four base properties used
60
+ # by the logger and the config objects.
61
+ # @param [String] app_name
62
+ # @param [String] script_filename
63
+ # @param [String] app_version
64
+ # @param [String] app_description
65
+ def describes_application(app_name: nil, script_filename: nil, app_version: nil, app_description: nil)
66
+ self.app_name = app_name unless app_name.nil?
67
+ self.app_version = app_version unless app_version.nil?
68
+ self.app_description = app_description unless app_description.nil?
69
+ self.script_filename = script_filename unless script_filename.nil?
70
+ end
71
+
72
+ # @return [Hash] This hash built from slop definition correspond to the :command_line layer of internal_configs
73
+ def command_line_config
74
+ @slop_definition.parse
75
+ @slop_definition.to_hash
76
+ end
77
+
78
+ # Yields a slop definition to modify the command line parameters
79
+ # @param [String] title used to insert a slop separator
80
+ def add_command_line_section(title='Script specific')
81
+ raise "Incorrect usage" unless block_given?
82
+ @slop_definition.separator build_separator(title)
83
+ yield @slop_definition
84
+ end
85
+
86
+ # Sets the :command_line layer of internal_configs to the computed {#command_line_config}
87
+ def load_config
88
+ internal_configs[:command_line] = {content: command_line_config, source: 'Command line'}
89
+ end
90
+
91
+ # Any modification done to the config is in fact stored in the :modified layer of internal_configs
92
+ # @param [String] key
93
+ # @param [String] value
94
+ def []=(key,value)
95
+ internal_configs[:modified][:content][key] = value
96
+ end
97
+
98
+ # Reset the :modified layer of internal_configs rolling back any change done to the config
99
+ def reset
100
+ internal_configs[:modified] = {content: {}, source: CHANGED_BY_CODE}
101
+ end
102
+
103
+
104
+ # @return [Array] List of layers
105
+ def layers
106
+ internal_configs.keys
107
+ end
108
+
109
+ # Executes code (block given) unless :simulate is in the config.
110
+ # If :simulate specified then display message instead of executing the code (block).
111
+ def safely_exec(message, *args)
112
+ raise "No block given" unless block_given?
113
+ if self[:simulate]
114
+ logger.puts_and_logs "SIMULATING: #{message}" unless message.nil?
115
+ else
116
+ logger.puts_and_logs message
117
+ yield(*args)
118
+ end
119
+ end
120
+
121
+
122
+ private
123
+
124
+ def build_separator(title)
125
+ "-- #{title} ".ljust 80, '-'
126
+ end
127
+
128
+ # Builds common used command line options
129
+ def build_command_line_options
130
+ add_command_line_section('Generic options') do |slop|
131
+ slop.on :auto, 'Auto mode. Bypasses questions to user.', :argument => false
132
+ slop.on :simulate, 'Do not perform the actual underlying actions.', :argument => false
133
+ slop.on :v, :verbose, 'Enable verbose mode.', :argument => false
134
+ slop.on :h, :help, 'Displays this help.', :argument => false
135
+ end
136
+ end
137
+
138
+ def build_banner
139
+ "\nUsage: #{script_filename} [options]\n#{app_name} Version: #{app_version}\n\n#{app_description}"
140
+ end
141
+
129
142
  end