fedux_org-stdlib 0.3.2 → 0.6.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (175) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +2 -0
  3. data/.simplecov +7 -6
  4. data/Gemfile +33 -28
  5. data/README.md +1 -1
  6. data/Rakefile +8 -1
  7. data/doc/colors.md +2 -2
  8. data/doc/commands.md +2 -2
  9. data/doc/logic_converters.md +5 -5
  10. data/doc/models.md +5 -5
  11. data/fedux_org-stdlib.gemspec +3 -7
  12. data/lib/fedux_org_stdlib.rb +13 -0
  13. data/lib/fedux_org_stdlib/app_config.rb +247 -0
  14. data/lib/fedux_org_stdlib/ci.rb +6 -0
  15. data/lib/fedux_org_stdlib/colors/html_color.rb +18 -0
  16. data/lib/fedux_org_stdlib/command.rb +10 -0
  17. data/lib/fedux_org_stdlib/command/command_result.rb +13 -0
  18. data/lib/fedux_org_stdlib/command/run_command.rb +55 -0
  19. data/lib/fedux_org_stdlib/command/which.rb +60 -0
  20. data/lib/fedux_org_stdlib/core_ext/array.rb +6 -0
  21. data/lib/fedux_org_stdlib/core_ext/string.rb +3 -0
  22. data/lib/fedux_org_stdlib/environment.rb +32 -0
  23. data/lib/fedux_org_stdlib/filesystem.rb +248 -0
  24. data/lib/fedux_org_stdlib/filesystem/exceptions.rb +12 -0
  25. data/lib/fedux_org_stdlib/logging.rb +12 -0
  26. data/lib/fedux_org_stdlib/logging/logger.rb +79 -0
  27. data/lib/{fedux_org/stdlib → fedux_org_stdlib}/logging/severity.rb +1 -0
  28. data/lib/fedux_org_stdlib/logic_converters.rb +11 -0
  29. data/lib/{fedux_org/stdlib → fedux_org_stdlib}/logic_converters/exceptions.rb +1 -0
  30. data/lib/fedux_org_stdlib/logic_converters/logic_converter.rb +39 -0
  31. data/lib/fedux_org_stdlib/logic_converters/on_off_converter.rb +35 -0
  32. data/lib/fedux_org_stdlib/logic_converters/true_false_converter.rb +34 -0
  33. data/lib/fedux_org_stdlib/logic_converters/y_n_converter.rb +34 -0
  34. data/lib/fedux_org_stdlib/logic_converters/yes_no_converter.rb +34 -0
  35. data/lib/fedux_org_stdlib/models.rb +5 -0
  36. data/lib/fedux_org_stdlib/models/base_model.rb +129 -0
  37. data/lib/fedux_org_stdlib/models/class_based_model.rb +67 -0
  38. data/lib/fedux_org_stdlib/models/exceptions.rb +40 -0
  39. data/lib/fedux_org_stdlib/models/filesystem_based_model.rb +98 -0
  40. data/lib/fedux_org_stdlib/project.rb +6 -0
  41. data/lib/fedux_org_stdlib/project/generators/taskjuggler.rb +35 -0
  42. data/lib/fedux_org_stdlib/project/logger.rb +12 -0
  43. data/lib/fedux_org_stdlib/project/plan.rb +26 -0
  44. data/lib/fedux_org_stdlib/project/report.rb +63 -0
  45. data/lib/fedux_org_stdlib/rake.rb +8 -0
  46. data/lib/fedux_org_stdlib/rake/exceptions.rb +9 -0
  47. data/lib/{fedux_org/stdlib → fedux_org_stdlib}/rake/project.rb +4 -4
  48. data/lib/fedux_org_stdlib/rake/project_task.rb +29 -0
  49. data/lib/fedux_org_stdlib/rake/shell_task.rb +45 -0
  50. data/lib/fedux_org_stdlib/rake/sub_task.rb +26 -0
  51. data/lib/fedux_org_stdlib/rake/task.rb +118 -0
  52. data/lib/fedux_org_stdlib/rake/version_bump_task.rb +56 -0
  53. data/lib/fedux_org_stdlib/rake_tasks.rb +8 -0
  54. data/lib/fedux_org_stdlib/rake_tasks/console.rb +11 -0
  55. data/lib/fedux_org_stdlib/rake_tasks/documentation.rb +2 -0
  56. data/lib/fedux_org_stdlib/rake_tasks/documentation/yard.rb +9 -0
  57. data/lib/fedux_org_stdlib/rake_tasks/gems.rb +3 -0
  58. data/lib/fedux_org_stdlib/rake_tasks/gems/bundler.rb +9 -0
  59. data/lib/fedux_org_stdlib/rake_tasks/gems/package.rb +9 -0
  60. data/lib/fedux_org_stdlib/rake_tasks/project.rb +1 -0
  61. data/lib/fedux_org_stdlib/rake_tasks/project/report.rb +3 -0
  62. data/lib/fedux_org_stdlib/rake_tasks/project/report/generate.rb +10 -0
  63. data/lib/fedux_org_stdlib/rake_tasks/project/report/open.rb +12 -0
  64. data/lib/fedux_org_stdlib/rake_tasks/project/report/open_shortcut.rb +9 -0
  65. data/lib/fedux_org_stdlib/rake_tasks/qa.rb +4 -0
  66. data/lib/fedux_org_stdlib/rake_tasks/qa/rubocop.rb +13 -0
  67. data/lib/fedux_org_stdlib/rake_tasks/qa/travis.rb +13 -0
  68. data/lib/fedux_org_stdlib/rake_tasks/terminal.rb +9 -0
  69. data/lib/fedux_org_stdlib/rake_tasks/tests.rb +4 -0
  70. data/lib/fedux_org_stdlib/rake_tasks/tests/cucumber.rb +13 -0
  71. data/lib/fedux_org_stdlib/rake_tasks/tests/rspec.rb +12 -0
  72. data/lib/fedux_org_stdlib/rake_tasks/tests/travis.rb +11 -0
  73. data/lib/fedux_org_stdlib/rake_tasks/version.rb +6 -0
  74. data/lib/fedux_org_stdlib/rake_tasks/version/bump.rb +8 -0
  75. data/lib/fedux_org_stdlib/rake_tasks/version/restore.rb +11 -0
  76. data/lib/fedux_org_stdlib/rake_tasks/version/show.rb +12 -0
  77. data/lib/fedux_org_stdlib/rake_tasks/version/show_shortcut.rb +9 -0
  78. data/lib/fedux_org_stdlib/require_files.rb +27 -0
  79. data/lib/fedux_org_stdlib/version.rb +5 -0
  80. data/lib/fedux_org_stdlib/version_management/exceptions.rb +9 -0
  81. data/lib/fedux_org_stdlib/version_management/library_builder.rb +12 -0
  82. data/lib/fedux_org_stdlib/version_management/ruby_library.rb +13 -0
  83. data/lib/fedux_org_stdlib/version_management/rubygem_version_file.rb +78 -0
  84. data/lib/fedux_org_stdlib/version_management/rubygem_version_file_parser.rb +22 -0
  85. data/lib/fedux_org_stdlib/version_management/version.rb +25 -0
  86. data/lib/fedux_org_stdlib/version_management/version_builder.rb +12 -0
  87. data/lib/fedux_org_stdlib/versionable.rb +18 -0
  88. data/rakefiles/default.rake +8 -1
  89. data/rakefiles/travis.rake +1 -1
  90. data/script/console +1 -1
  91. data/script/terminal +3 -2
  92. data/spec/app_config_spec.rb +7 -0
  93. data/spec/colors/html_color_spec.rb +7 -14
  94. data/spec/command/run_command_spec.rb +9 -10
  95. data/spec/command/which_spec.rb +8 -9
  96. data/spec/environment_spec.rb +12 -11
  97. data/spec/logger/logger_spec.rb +17 -16
  98. data/spec/logic_converters/on_off_converter_spec.rb +8 -7
  99. data/spec/logic_converters/true_false_converter_spec.rb +8 -7
  100. data/spec/logic_converters/y_n_converter_spec.rb +8 -7
  101. data/spec/logic_converters/yes_no_converter_spec.rb +8 -7
  102. data/spec/models/base_model_spec.rb +7 -7
  103. data/spec/models/class_based_model_spec.rb +37 -39
  104. data/spec/models/filesystem_based_model_spec.rb +24 -24
  105. data/spec/project/plan_spec.rb +6 -5
  106. data/spec/project/report_spec.rb +35 -29
  107. data/spec/project/taskjuggler_spec.rb +13 -12
  108. data/spec/spec_helper.rb +15 -38
  109. data/spec/support/environment.rb +16 -0
  110. data/spec/support/filesystem.rb +19 -0
  111. data/spec/support/reporting.rb +2 -0
  112. data/spec/support/rspec.rb +5 -0
  113. data/spec/support/string.rb +2 -0
  114. data/spec/version_management/library_builder_spec.rb +5 -4
  115. data/spec/version_management/ruby_library_spec.rb +5 -4
  116. data/spec/version_management/rubygem_version_file_parser_spec.rb +27 -16
  117. data/spec/version_management/rubygem_version_file_spec.rb +56 -54
  118. data/spec/version_management/version_builder_spec.rb +5 -4
  119. data/spec/version_management/version_spec.rb +16 -16
  120. metadata +104 -110
  121. data/lib/fedux_org/stdlib.rb +0 -15
  122. data/lib/fedux_org/stdlib/colors/html_color.rb +0 -19
  123. data/lib/fedux_org/stdlib/command.rb +0 -11
  124. data/lib/fedux_org/stdlib/command/command_result.rb +0 -14
  125. data/lib/fedux_org/stdlib/command/run_command.rb +0 -55
  126. data/lib/fedux_org/stdlib/command/which.rb +0 -67
  127. data/lib/fedux_org/stdlib/environment.rb +0 -31
  128. data/lib/fedux_org/stdlib/filesystem.rb +0 -247
  129. data/lib/fedux_org/stdlib/filesystem/exceptions.rb +0 -13
  130. data/lib/fedux_org/stdlib/logging.rb +0 -13
  131. data/lib/fedux_org/stdlib/logging/logger.rb +0 -79
  132. data/lib/fedux_org/stdlib/logic_converters.rb +0 -12
  133. data/lib/fedux_org/stdlib/logic_converters/logic_converter.rb +0 -40
  134. data/lib/fedux_org/stdlib/logic_converters/on_off_converter.rb +0 -36
  135. data/lib/fedux_org/stdlib/logic_converters/true_false_converter.rb +0 -35
  136. data/lib/fedux_org/stdlib/logic_converters/y_n_converter.rb +0 -35
  137. data/lib/fedux_org/stdlib/logic_converters/yes_no_converter.rb +0 -35
  138. data/lib/fedux_org/stdlib/models.rb +0 -4
  139. data/lib/fedux_org/stdlib/models/base_model.rb +0 -129
  140. data/lib/fedux_org/stdlib/models/class_based_model.rb +0 -76
  141. data/lib/fedux_org/stdlib/models/exceptions.rb +0 -41
  142. data/lib/fedux_org/stdlib/models/filesystem_based_model.rb +0 -99
  143. data/lib/fedux_org/stdlib/project.rb +0 -5
  144. data/lib/fedux_org/stdlib/project/generators/taskjuggler.rb +0 -41
  145. data/lib/fedux_org/stdlib/project/logger.rb +0 -13
  146. data/lib/fedux_org/stdlib/project/plan.rb +0 -15
  147. data/lib/fedux_org/stdlib/project/report.rb +0 -67
  148. data/lib/fedux_org/stdlib/rake.rb +0 -7
  149. data/lib/fedux_org/stdlib/rake/console.rb +0 -3
  150. data/lib/fedux_org/stdlib/rake/documentation.rb +0 -1
  151. data/lib/fedux_org/stdlib/rake/documentation/yard.rb +0 -14
  152. data/lib/fedux_org/stdlib/rake/gems.rb +0 -2
  153. data/lib/fedux_org/stdlib/rake/gems/bundler.rb +0 -12
  154. data/lib/fedux_org/stdlib/rake/gems/package.rb +0 -7
  155. data/lib/fedux_org/stdlib/rake/library.rb +0 -8
  156. data/lib/fedux_org/stdlib/rake/terminal.rb +0 -9
  157. data/lib/fedux_org/stdlib/rake/tests.rb +0 -3
  158. data/lib/fedux_org/stdlib/rake/tests/cucumber.rb +0 -6
  159. data/lib/fedux_org/stdlib/rake/tests/rspec.rb +0 -6
  160. data/lib/fedux_org/stdlib/rake/tests/travis.rb +0 -15
  161. data/lib/fedux_org/stdlib/rake/travis.rb +0 -1
  162. data/lib/fedux_org/stdlib/rake/travis/lint.rb +0 -10
  163. data/lib/fedux_org/stdlib/rake/version.rb +0 -4
  164. data/lib/fedux_org/stdlib/rake/version/base.rb +0 -7
  165. data/lib/fedux_org/stdlib/rake/version/bump.rb +0 -78
  166. data/lib/fedux_org/stdlib/rake/version/restore.rb +0 -9
  167. data/lib/fedux_org/stdlib/rake/version/show.rb +0 -9
  168. data/lib/fedux_org/stdlib/rake/version/show_shortcut.rb +0 -6
  169. data/lib/fedux_org/stdlib/version.rb +0 -6
  170. data/lib/fedux_org/stdlib/version_management/library_builder.rb +0 -15
  171. data/lib/fedux_org/stdlib/version_management/ruby_library.rb +0 -14
  172. data/lib/fedux_org/stdlib/version_management/rubygem_version_file.rb +0 -90
  173. data/lib/fedux_org/stdlib/version_management/rubygem_version_file_parser.rb +0 -18
  174. data/lib/fedux_org/stdlib/version_management/version.rb +0 -33
  175. data/lib/fedux_org/stdlib/version_management/version_builder.rb +0 -15
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a658594f0dd9d467af925caa9620758e617922db
4
+ data.tar.gz: 203f8edd0ecaaab10cf209bd742ab00053116a6d
5
+ SHA512:
6
+ metadata.gz: ab79328e1e94f5dcf316262af0a0ea7d70262dfd95f4a3c3dc8f064343c2cc51ca7e76350291d5c390c94f61aa7e558b0d76ead92408e8d9b92d26ea59242c35
7
+ data.tar.gz: 6d545af99e51fe167f3da93031f4fbf079eb0da2d90293a26ea004d4f89cb68f22eac06e525392dfa7b333318b9e556ddeda8a22f00e637dd5ef6362b7852911
data/.rspec CHANGED
@@ -1,2 +1,4 @@
1
+ --require rspec/legacy_formatters
1
2
  --format Fuubar
2
3
  --order rand
4
+ --color
data/.simplecov CHANGED
@@ -1,10 +1,11 @@
1
+ # encoding: utf-8
1
2
  SimpleCov.start do
2
3
  add_filter "/features/"
3
4
  add_filter "/spec/"
4
- add_group "stdlib", "lib/fedux_org/stdlib"
5
- add_group "models", "lib/fedux_org/stdlib/models"
6
- add_group "logging", "lib/fedux_org/stdlib/logging"
7
- add_group "command", "lib/fedux_org/stdlib/command"
8
- add_group "environment", "lib/fedux_org/stdlib/environment"
9
- add_group "filesystem", "lib/fedux_org/stdlib/filesystem"
5
+ add_group "stdlib", "lib/fedux_org_stdlib"
6
+ add_group "models", "lib/fedux_org_stdlib/models"
7
+ add_group "logging", "lib/fedux_org_stdlib/logging"
8
+ add_group "command", "lib/fedux_org_stdlib/command"
9
+ add_group "environment", "lib/fedux_org_stdlib/environment"
10
+ add_group "filesystem", "lib/fedux_org_stdlib/filesystem"
10
11
  end
data/Gemfile CHANGED
@@ -1,37 +1,42 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gemspec
3
+ # use gem spec
4
+ gemspec
4
5
 
5
- gem 'launchy'
6
- gem 'command_exec'
7
- gem 'taskjuggler'
8
- gem 'versionomy'
6
+ group :development, :test do
7
+ gem 'pry', require: false
8
+ gem 'pry-doc', require: false
9
9
 
10
- group :test do
11
- gem 'rake'
12
- gem 'rspec'
13
- gem 'simplecov'
14
- gem 'aruba'
15
- gem 'fuubar'
16
- gem 'ffaker'
17
- end
10
+ if RUBY_VERSION < '2.0.0'
11
+ gem 'pry-debugger', require: false
12
+ gem 'debugger'
13
+ gem 'debugger-completion', require: false
14
+ else
15
+ gem 'pry-byebug', require: false
16
+ gem 'byebug'
17
+ end
18
18
 
19
- group :documentation do
20
- gem 'yard'
21
- gem 'redcarpet'
19
+ gem 'aruba', require: false
20
+ gem 'awesome_print', require: 'ap'
21
+ gem 'bundler', '>= 1.3', require: false
22
+ gem 'coveralls', require: false
23
+ gem 'cucumber', require: false
24
+ gem 'erubis'
25
+ gem 'fuubar'
22
26
  gem 'github-markup'
23
- end
27
+ gem 'rake', require: false
28
+ gem 'redcarpet'
29
+ gem 'rspec', require: false
30
+ gem 'rspec-legacy_formatters', require: false
31
+ gem 'rubocop', require: false
32
+ gem 'simplecov'
33
+ gem 'tmrb', require: false
34
+ gem 'versionomy'
35
+ gem 'filegen'
24
36
 
25
- group :development do
26
- gem 'tmrb'
27
- gem 'erubis'
28
- gem 'debugger'
29
- gem 'pry'
30
- gem 'pry-doc'
31
- gem 'pry-debugger'
32
- gem 'debugger-completion'
33
- gem 'awesome_print'
37
+ gem 'command_exec'
38
+ gem 'taskjuggler'
39
+ gem 'launchy'
40
+ gem 'yard'
34
41
  gem 'travis-lint'
35
- gem 'fuubar'
36
- gem 'churn'
37
42
  end
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # FeduxOrg::Stdlib
1
+ # FeduxOrgStdlib
2
2
 
3
3
  Gem with a lot of stuff which can be used in other libraries.
4
4
 
data/Rakefile CHANGED
@@ -1,3 +1,10 @@
1
1
  #!/usr/bin/env rake
2
2
 
3
- require 'fedux_org/stdlib/rake'
3
+ $LOAD_PATH << File.expand_path('../lib', __FILE__)
4
+
5
+ require 'fedux_org_stdlib/rake_tasks'
6
+
7
+ desc 'Show help for rake'
8
+ task :default do
9
+ sh 'bundle exec rake -T'
10
+ end
@@ -1,11 +1,11 @@
1
- # FeduxOrg::Stdlib::HtmlColor
1
+ # FeduxOrgStdlib::HtmlColor
2
2
 
3
3
  ## Usage
4
4
 
5
5
  ### HtmlColor
6
6
 
7
7
  ```ruby
8
- require 'fedux_org/stdlib/colors/html_color'
8
+ require 'fedux_org_stdlib/colors/html_color'
9
9
  ```
10
10
 
11
11
  ```ruby
@@ -1,4 +1,4 @@
1
- # FeduxOrg::Stdlib::Command
1
+ # FeduxOrgStdlib::Command
2
2
 
3
3
  ## Usage
4
4
 
@@ -7,7 +7,7 @@
7
7
  Searches command in PATH. It supports the usage of a full path as well.
8
8
 
9
9
  ```ruby
10
- require 'fedux_org/stdlib/command'
10
+ require 'fedux_org_stdlib/command'
11
11
 
12
12
  class XY
13
13
  include Command
@@ -1,27 +1,27 @@
1
- # FeduxOrg::Stdlib::LogicConverters
1
+ # FeduxOrgStdlib::LogicConverters
2
2
 
3
3
  ## Usage
4
4
 
5
5
  ### LogicConverter
6
6
 
7
7
  ```ruby
8
- require 'fedux_org/stdlib/logic_converters/logic_converter'
8
+ require 'fedux_org_stdlib/logic_converters/logic_converter'
9
9
  ```
10
10
 
11
11
  ### YesNoConverter
12
12
 
13
13
  ```ruby
14
- require 'fedux_org/stdlib/logic_converters/yes_no_converter'
14
+ require 'fedux_org_stdlib/logic_converters/yes_no_converter'
15
15
  ```
16
16
 
17
17
  ### TrueFalseConverter
18
18
 
19
19
  ```ruby
20
- require 'fedux_org/stdlib/logic_converters/true_false_converter'
20
+ require 'fedux_org_stdlib/logic_converters/true_false_converter'
21
21
  ```
22
22
 
23
23
  ### OnOffConverter
24
24
 
25
25
  ```ruby
26
- require 'fedux_org/stdlib/logic_converters/on_off_converter'
26
+ require 'fedux_org_stdlib/logic_converters/on_off_converter'
27
27
  ```
@@ -1,4 +1,4 @@
1
- # FeduxOrg::Stdlib::Models
1
+ # FeduxOrgStdlib::Models
2
2
 
3
3
  ## Usage
4
4
 
@@ -7,23 +7,23 @@
7
7
  This file requires all other files found below.
8
8
 
9
9
  ```ruby
10
- require 'fedux_org/stdlib/models'
10
+ require 'fedux_org_stdlib/models'
11
11
  ```
12
12
 
13
13
  ### BaseModel
14
14
 
15
15
  ```ruby
16
- require 'fedux_org/stdlib/models/base_model'
16
+ require 'fedux_org_stdlib/models/base_model'
17
17
  ```
18
18
 
19
19
  ### ClassBaseModel
20
20
 
21
21
  ```ruby
22
- require 'fedux_org/stdlib/models/class_based_model'
22
+ require 'fedux_org_stdlib/models/class_based_model'
23
23
  ```
24
24
 
25
25
  ### FileSystemBasedModel
26
26
 
27
27
  ```ruby
28
- require 'fedux_org/stdlib/models/filesystem_based_model'
28
+ require 'fedux_org_stdlib/models/filesystem_based_model'
29
29
  ```
@@ -1,15 +1,14 @@
1
1
  # coding: utf-8
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'fedux_org/stdlib/version'
4
+ require 'fedux_org_stdlib/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "fedux_org-stdlib"
8
- spec.version = FeduxOrg::Stdlib::VERSION
8
+ spec.version = FeduxOrgStdlib::VERSION
9
9
  spec.authors = ["Max Meyer"]
10
10
  spec.email = ["dev@fedux.org"]
11
- spec.description = %q{collection of useful libraries}
12
- spec.summary = %q{collection of useful libraries}
11
+ spec.summary = %q{Collection of useful libraries. It maybe depend on external libraries.}
13
12
  spec.homepage = ""
14
13
  spec.license = "MIT"
15
14
 
@@ -18,8 +17,5 @@ Gem::Specification.new do |spec|
18
17
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
18
  spec.require_paths = ['lib']
20
19
 
21
- spec.add_development_dependency 'bundler', '~> 1.3'
22
- spec.add_development_dependency 'rake'
23
-
24
20
  spec.add_dependency 'activesupport'
25
21
  end
@@ -0,0 +1,13 @@
1
+ require 'fedux_org_stdlib/command'
2
+ require 'fedux_org_stdlib/environment'
3
+ require 'fedux_org_stdlib/filesystem'
4
+ require 'fedux_org_stdlib/logging'
5
+ require 'fedux_org_stdlib/logic_converters'
6
+ require 'fedux_org_stdlib/models'
7
+ require 'fedux_org_stdlib/version'
8
+
9
+ module FeduxOrgStdlib
10
+ def self.root_directory
11
+ File.expand_path( '../../..', __FILE__ )
12
+ end
13
+ end
@@ -0,0 +1,247 @@
1
+ # encoding: utf-8
2
+ require 'fedux_org_stdlib/require_files'
3
+ require_library %w{ json psych active_support/core_ext/string/inflections }
4
+
5
+ module FeduxOrgStdlib
6
+ module Exceptions
7
+ # One wants to modify a locked config
8
+ class ConfigLocked < StandardError; end
9
+
10
+ # Found config file is not readable
11
+ class ConfigFileNotReadable < StandardError; end
12
+
13
+ # No allowed config file could be found
14
+ class NoConfigFileFound < StandardError; end
15
+ end
16
+
17
+ # This class makes a config file available as an object. The config file
18
+ # needs to be `YAML` by default. It is read by `Psych` and converted to a
19
+ # hash. If you chose to use a different file format: Each config file needs
20
+ # to translatable to a hash or another data structure which responds to
21
+ # `[]` by the given `config_engine`.
22
+ #
23
+ # By default it will look for a suitable config file in the given order:
24
+ #
25
+ # 1. $HOME/.config/<application_name>/<config_file>
26
+ # 2. $HOME/.<application_name>/<config_file>
27
+ # 3. /etc/.<application_name>/<config_file>
28
+ #
29
+ # Most conventions are implemented by separate methods. If one conventions
30
+ # is not suitable for your use case, just overwrite the method.
31
+ #
32
+ # If you prefer to use a different path to the config file or name of the
33
+ # config file one of the following methods needs to be overwritten:
34
+ #
35
+ # * config_file
36
+ # * config_name
37
+ # * application_name
38
+ #
39
+ # If you want the class to look for your config file at a different place
40
+ # overwrite the following method
41
+ #
42
+ # * allowed_config_file_paths
43
+ #
44
+ # Below you find some examples for the usage of the class:
45
+ #
46
+ # @example Create config
47
+ #
48
+ # class MyConfig < AppConfig
49
+ # # This is the default if no config file exists
50
+ # option :option1, 'data1'
51
+ # end
52
+ #
53
+ # @example Config file
54
+ #
55
+ # option1: 'data2'
56
+ class AppConfig
57
+
58
+ # Set all neccessary class instance variables
59
+ def self.inherited(base)
60
+ base.instance_variable_set :@options, Set.new
61
+ base.instance_variable_set :@process_environment, ProcessEnvironment.new
62
+ end
63
+
64
+ class << self
65
+ # @!attribute [r] options
66
+ # Read access to defined options
67
+ #
68
+ # @!attribute [r] process_environment
69
+ # Read access to environment of process using this config
70
+ attr_reader :options, :process_environment
71
+
72
+ # Define a reader for option
73
+ #
74
+ # @param [Symbol] option
75
+ # Name of option
76
+ #
77
+ # @param [Object] default_value
78
+ # The default value of this option
79
+ def option_reader(option, default_value)
80
+ define_method option.to_sym do
81
+ config.fetch(option.to_sym, default_value)
82
+ end
83
+
84
+ @options << option
85
+ end
86
+
87
+ # Define a writer for option
88
+ #
89
+ # @param [Symbol] option
90
+ # Name of option
91
+ #
92
+ # @raise [Exceptions::ConfigLocked]
93
+ # If one tries to modified a locked config
94
+ def option_writer(option)
95
+ define_method "#{option}=".to_sym do |value|
96
+ begin
97
+ config[option.to_sym] = value
98
+ rescue RuntimeError
99
+ raise Exceptions::ConfigLocked
100
+ end
101
+ end
102
+
103
+ @options << option
104
+ end
105
+
106
+ # Define a writer and a reader for option
107
+ #
108
+ # @param [Symbol] option
109
+ # Name of option
110
+ #
111
+ # @param [Object] default_value
112
+ # Default value of option
113
+ def option(option, default_value)
114
+ option_reader(option, default_value)
115
+ option_writer(option)
116
+ end
117
+ end
118
+
119
+ private
120
+
121
+ attr_reader :config
122
+
123
+ public
124
+
125
+ # Create a new instance of config
126
+ #
127
+ # @param [String] file
128
+ # Path where config file is stored. The file will be read by the
129
+ # `config_engine`.
130
+ #
131
+ # @param [Object] config_engine (Psych)
132
+ # The engine to read config file
133
+ #
134
+ # @raise [Exceptions::NoConfigFileFound]
135
+ # If the config file could not be found
136
+ #
137
+ # @raise [Exceptions::ConfigFileNotReadable]
138
+ # If an avaiable config file could not be read by the config engine
139
+ #
140
+ # @return [AppConfig]
141
+ # The config instance. If the resulting data structure created by the
142
+ # config_engine does not respond to `:[]` an empty config object will be
143
+ # created.
144
+ def initialize(file = available_config_file, config_engine = Psych)
145
+ fail Exceptions::NoConfigFileFound, JSON.dump(files: allowed_config_file_paths) unless file
146
+
147
+ config_mutex = Mutex.new
148
+
149
+ yaml = config_mutex.synchronize do
150
+ begin
151
+ Psych.load_file(file)
152
+ rescue StandardError => e
153
+ fail Exceptions::ConfigFileNotReadable, JSON.dump(message: e.message, file: file)
154
+ end
155
+ end
156
+
157
+ if yaml.respond_to? :[]
158
+ @config = yaml.symbolize_keys
159
+ else
160
+ @config = {}
161
+ end
162
+ end
163
+
164
+ # Lock the configuration
165
+ def lock
166
+ config.freeze
167
+ end
168
+
169
+ # Output a string presentation of the configuration
170
+ #
171
+ # @return [String]
172
+ # An formatted version of the configuration
173
+ def to_s
174
+ result = []
175
+ result << sprintf("%20s | %s", 'option', 'value')
176
+ result << sprintf("%s + %s", '-' * 20, '-' * 80)
177
+
178
+ Config.options.each do |o|
179
+ result << sprintf("%20s | %s", o, Array(public_send(o)).join(', '))
180
+ end
181
+
182
+ result.join("\n")
183
+ end
184
+
185
+ private
186
+
187
+ # The name of the config file
188
+ #
189
+ # @return [String]
190
+ # The name of the config file. It defaults to `<config_name>.yaml`. If
191
+ # you want to use a different file name you need to overwrite this
192
+ # method.
193
+ def config_file
194
+ "#{config_name}.yaml"
195
+ end
196
+
197
+ # The base name of the config
198
+ #
199
+ # @return [String]
200
+ # This one returns the base name of the config file (without the file
201
+ # extension). It uses the class name of the config class
202
+ #
203
+ # @example Determine the base name of the config
204
+ #
205
+ # class ClientConfig; end
206
+ #
207
+ # This will result in `client` as base name for the config file.
208
+ def config_name
209
+ self.class.name.demodulize.sub(/Config/, '').underscore.pluralize
210
+ end
211
+
212
+ # The name of your application
213
+ #
214
+ # @return [String]
215
+ # This will strip of the class part of fully qualified class name and
216
+ # converted it to a path.
217
+ #
218
+ # @example Determine application name
219
+ #
220
+ # class MyApplication::MyConfig; end
221
+ #
222
+ # This will be converted to
223
+ #
224
+ # my_application
225
+ def application_name
226
+ self.class.to_s.deconstantize.underscore
227
+ end
228
+
229
+ # The paths where to look for the config file
230
+ #
231
+ # @return [Array]
232
+ # A list of paths where the config object should look for its config
233
+ # file.
234
+ def allowed_config_file_paths
235
+ [
236
+ ::File.expand_path(::File.join(self.class.process_environment.fetch('HOME'), '.config', application_name, config_file)),
237
+ ::File.expand_path(::File.join(self.class.process_environment.fetch('HOME'), format('.%s', application_name), config_file)),
238
+ ::File.expand_path(::File.join('/etc', application_name, config_file)),
239
+ # ::File.expand_path("../../../../files/#{config_file}", __FILE__),
240
+ ]
241
+ end
242
+
243
+ def available_config_file
244
+ allowed_config_file_paths.find { |f| ::File.exists? f }
245
+ end
246
+ end
247
+ end