qonfig 0.4.0 → 0.5.0

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: afe6dc7a96eec7d6f1e45a84d88fe5d4d7188547c6f55fb6aff65f7e46b92620
4
- data.tar.gz: c045b73600ef8b751229a81ca8030cdbd45b6955f47a1c8e1d0cff48b1229a67
3
+ metadata.gz: f4478b75a75d9df31fedab5728cc55af2509b858158d522cefc8fbe15811c216
4
+ data.tar.gz: d50efec1ffcc132225be42a6e54d9b47e6d548cde6b45ac09f345a9f01fbc2d3
5
5
  SHA512:
6
- metadata.gz: '0933d09ee590ab14c764373e66a90adce051cad88e3edcb8fb72ea75c39616797d263621f99f3ffe53645a652a6a8f8d566762a4b12d165168c0196ca4c87579'
7
- data.tar.gz: 5dbcfedecf3a20906e00adb5f0ffec49f240bff0402e2db75690c29cb4b0bb8a394ccf9f1531723ff46c97a51d230c4e2484aa0eae122479189fa57882b41df8
6
+ metadata.gz: e5be60954fc592e1c62545c92d025d48cc6d5643ec80955cc9b4dea9bc0c65ccde98638de01a17aabc451bfdfc1d6be4b2465d5ce1f7d2f472ac9e2ef4012efe
7
+ data.tar.gz: 1a6e33b588e5df992e10921fd3c09df4de70786aa54968feeb3966170ff33083615110de566f40dd18b8921ee65da5dfd47b40956b08464104f246f68e625671
data/.travis.yml CHANGED
@@ -1,13 +1,14 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.2.10
4
- - 2.3.7
5
- - 2.4.4
6
- - 2.5.1
7
- - ruby-head
8
- - jruby-head
9
-
3
+ - 2.3.7
4
+ - 2.4.4
5
+ - 2.5.1
6
+ - ruby-head
7
+ - jruby-head
10
8
  sudo: false
11
9
  cache: bundler
12
10
  before_install: gem install bundler
13
11
  script: bundle exec rspec
12
+ notifications:
13
+ slack:
14
+ secure: I03SDv+IrKy3IkeGjjHUJ9VneFMiYpLzIgPOixGFO5zGVXgulwmun82KsrPSW5HkK1UEQCahfoXt1hNECPwxcdsY01q6LBYJQx1jD0q17bXHllN/q0C6lx3peRN0KqNAAOU3/mHZxLt3HFV+N3HsSnoxDMuSYJgKbjCL/QVG2L2UYT9vi+JRNM/thj8R6MWKWlOHemik40GbLr2anCOCqiALzxnJzh5nJyGj+9SGvjhHfQq/fAIrg1+Scu+UchG8d+1yS5JWsGTt1/JF08i+Ux4ceTQ7GNBNeA5cj/xuUzvRx6A85renxyTiZMKIL0+jeceUm8c+/46XFcq0F7/kJB36lwjFhX1JRphcu/VouRdEwW/BvH74wnyHtygqOpk0LH4shp7A1DIS1DlnBbeJxrR5hdMDnmV85kTU6w6H0BbncsYY/pH1fji1kgH6jCsdwqDlq4wLB8x8I+eZABBsfb/r55z/HnBmmxlvR7Rt3X5/yR3gJrsgRrDBBZ5LwYy1RSCDu76vMQqWGJKG03FdfqSNqmC5V4MC5Ez8yjGDW0Yle09mwvsL2c6fDXyDPCeB/gwNk+FvgLRXnv7C4BaBNEoG4JnCL/dwauoJNg0lB6uF34BEmYAhZIrdY1CI6BqPWnaVMJfcJSYekBVXDIELDnTFRWjaGU1y8dM6lNzDmYE=
data/CHANGELOG.md CHANGED
@@ -1,7 +1,16 @@
1
1
  # Changelog
2
2
  All notable changes to this project will be documented in this file.
3
3
 
4
+ ## [0.5.0] - 2018-07-27
5
+ ### Added
6
+ - `load_from_json`- a command that provides an ability to define config settings
7
+ by loading them from a json file (in `load_from_yaml` manner);
8
+
9
+ ### Changed
10
+ - Support for Ruby 2.2 has ended;
11
+
4
12
  ## [0.4.0] - 2018-06-24
13
+ ### Added
5
14
  - Introduce Plugin Ecosystem (`Qonfig::Plugins`):
6
15
  - load plugin: `Qonfig.plugin('plugin_name')` or `Qonfig.plugin(:plugin_name)`;
7
16
  - get registered plugins: `Qonfig.plugins #=> array of strings`
data/README.md CHANGED
@@ -31,6 +31,7 @@ require 'qonfig'
31
31
  - [State freeze](#state-freeze)
32
32
  - [Settings as Predicates](#settings-as-predicates)
33
33
  - [Load from YAML file](#load-from-yaml-file)
34
+ - [Load from JSON file](#load-from-json-file)
34
35
  - [Load from ENV](#load-from-env)
35
36
  - [Load from \_\_END\_\_](#load-from-__end__) (aka `load_from_self`)
36
37
  - [Smart Mixin](#smart-mixin) (`Qonfig::Configurable`)
@@ -482,6 +483,65 @@ Config.new.to_h # => { "nonexistent_yaml" => {}, "another_key" => nil }
482
483
 
483
484
  ---
484
485
 
486
+ ### Load from JSON file
487
+
488
+ - `:strict` mode (fail behaviour when the required yaml file doesnt exist):
489
+ - `true` (by default) - causes `Qonfig::FileNotFoundError`;
490
+ - `false` - do nothing, ignore current command;
491
+
492
+ ```json
493
+ <!-- options.json -->
494
+ {
495
+ "user": "0exp",
496
+ "password": 12345,
497
+ "rubySettings": {
498
+ "allowedVersions": ["2.3", "2.4.2", "1.9.8"],
499
+ "gitLink": null,
500
+ "withAdditionals": false
501
+ }
502
+ }
503
+ ```
504
+
505
+ ```ruby
506
+ class Config < Qonfig::DataSet
507
+ load_from_json 'options.json'
508
+ end
509
+
510
+ config = Config.new
511
+
512
+ config.settings.user # => '0exp'
513
+ config.settings.password # => 12345
514
+ config.settings.rubySettings.allowedVersions # => ['2.3', '2.4.2', '1.9.8']
515
+ config.settings.rubySettings.gitLink # => nil
516
+ config.settings.rubySettings.withAdditionals # => false
517
+ ```
518
+
519
+ ```ruby
520
+ # --- strict mode ---
521
+ class Config < Qonfig::DataSet
522
+ setting :nonexistent_json do
523
+ load_from_yaml 'nonexistent_json.json', strict: true # true by default
524
+ end
525
+
526
+ setting :another_key
527
+ end
528
+
529
+ Config.new # => Qonfig::FileNotFoundError
530
+
531
+ # --- non-strict mode ---
532
+ class Config < Qonfig::DataSet
533
+ settings :nonexistent_json do
534
+ load_from_yaml 'nonexistent_json.json', strict: false
535
+ end
536
+
537
+ setting :another_key
538
+ end
539
+
540
+ Config.new.to_h # => { "nonexistent_json" => {}, "another_key" => nil }
541
+ ```
542
+
543
+ ---
544
+
485
545
  ### Load from ENV
486
546
 
487
547
  - `:convert_values` (`false` by default):
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Qonfig
4
+ module Commands
5
+ # @api private
6
+ # @since 0.5.0
7
+ class LoadFromJSON < Base
8
+ # @return [String]
9
+ #
10
+ # @api private
11
+ # @since 0.5.0
12
+ attr_reader :file_path
13
+
14
+ # @return [Boolean]
15
+ #
16
+ # @api private
17
+ # @sicne 0.5.0
18
+ attr_reader :strict
19
+
20
+ # @param file_path [String]
21
+ # @option strict [Boolean]
22
+ #
23
+ # @api private
24
+ # @since 0.5.0
25
+ def initialize(file_path, strict: true)
26
+ @file_path = file_path
27
+ @strict = strict
28
+ end
29
+
30
+ # @param settings [Qonfig::Settings]
31
+ # @return [void]
32
+ #
33
+ # @api private
34
+ # @since 0.5.0
35
+ def call(settings)
36
+ json_data = Qonfig::Loaders::JSON.load_file(file_path, fail_on_unexist: strict)
37
+
38
+ raise(
39
+ Qonfig::IncompatibleJSONStructureError,
40
+ 'JSON object should have a hash-like structure'
41
+ ) unless json_data.is_a?(Hash)
42
+
43
+ json_based_settings = build_data_set_class(json_data).new.settings
44
+
45
+ settings.__append_settings__(json_based_settings)
46
+ end
47
+
48
+ private
49
+
50
+ # @param json_data [Hash]
51
+ # @return [Class<Qonfig::DataSet>]
52
+ #
53
+ # @api private
54
+ # @since 0.5.0
55
+ def build_data_set_class(json_data)
56
+ Qonfig::DataSet::ClassBuilder.build_from_hash(json_data)
57
+ end
58
+ end
59
+ end
60
+ end
data/lib/qonfig/dsl.rb CHANGED
@@ -98,5 +98,15 @@ module Qonfig
98
98
  trim_prefix: trim_prefix
99
99
  )
100
100
  end
101
+
102
+ # @param file_path [String]
103
+ # @option strict [Boolean]
104
+ # @return [void]
105
+ #
106
+ # @api public
107
+ # @since 0.5.0
108
+ def load_from_json(file_path, strict: true)
109
+ commands << Qonfig::Commands::LoadFromJSON.new(file_path, strict: strict)
110
+ end
101
111
  end
102
112
  end
data/lib/qonfig/error.rb CHANGED
@@ -51,6 +51,12 @@ module Qonfig
51
51
  # @since 0.2.0
52
52
  IncompatibleYAMLStructureError = Class.new(Error)
53
53
 
54
+ # @see Qonfig::Commands::LoadFromJSON
55
+ #
56
+ # @api public
57
+ # @since 0.5.0
58
+ IncompatibleJSONStructureError = Class.new(Error)
59
+
54
60
  # @see Qonfig::Loaders::YAML
55
61
  #
56
62
  # @api public
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Qonfig
4
+ module Loaders
5
+ # @api private
6
+ # @sicne 0.5.0
7
+ module Basic
8
+ # @param data [String]
9
+ # @return [void]
10
+ #
11
+ # @api private
12
+ # @since 0.5.0
13
+ def load(data)
14
+ nil
15
+ end
16
+
17
+ # @return [void]
18
+ #
19
+ # @api private
20
+ # @since 0.5.0
21
+ def load_empty_data
22
+ nil
23
+ end
24
+
25
+ # @param file_path [String]
26
+ # @option fail_on_unexist [Boolean]
27
+ # @return [Object]
28
+ #
29
+ # @raise [Qonfig::FileNotFoundError]
30
+ #
31
+ # @api private
32
+ # @since 0.5.0
33
+ def load_file(file_path, fail_on_unexist: true)
34
+ load(::File.read(file_path))
35
+ rescue Errno::ENOENT => error
36
+ fail_on_unexist ? (raise Qonfig::FileNotFoundError, error.message) : load_empty_data
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Qonfig
4
+ module Loaders
5
+ # @api private
6
+ # @since 0.5.0
7
+ module JSON
8
+ # @since 0.5.0
9
+ extend Qonfig::Loaders::Basic
10
+
11
+ class << self
12
+ # @param data [String]
13
+ # @return [Object]
14
+ #
15
+ # @api private
16
+ # @since 0.5.0
17
+ def load(data)
18
+ ::JSON.parse(data, max_nesting: false, allow_nan: true)
19
+ end
20
+
21
+ # @return [Object]
22
+ #
23
+ # @api private
24
+ # @since 0.5.0
25
+ def load_empty_data
26
+ load('{}')
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -5,6 +5,9 @@ module Qonfig
5
5
  # @api private
6
6
  # @since 0.2.0
7
7
  module YAML
8
+ # @since 0.5.0
9
+ extend Qonfig::Loaders::Basic
10
+
8
11
  class << self
9
12
  # @param data [String]
10
13
  # @return [Object]
@@ -15,18 +18,12 @@ module Qonfig
15
18
  ::YAML.load(ERB.new(data).result)
16
19
  end
17
20
 
18
- # @param file_path [String]
19
- # @option fail_on_unexist [Boolean]
20
21
  # @return [Object]
21
22
  #
22
- # @raise [Qonfig::FileNotFoundError]
23
- #
24
23
  # @api private
25
- # @since 0.2.0
26
- def load_file(file_path, fail_on_unexist: true)
27
- load(::File.read(file_path))
28
- rescue Errno::ENOENT => error
29
- fail_on_unexist ? (raise Qonfig::FileNotFoundError, error.message) : load('{}')
24
+ # @since 0.5.0
25
+ def load_empty_data
26
+ load('{}')
30
27
  end
31
28
  end
32
29
  end
@@ -3,5 +3,5 @@
3
3
  module Qonfig
4
4
  # @api public
5
5
  # @since 0.1.0
6
- VERSION = '0.4.0'
6
+ VERSION = '0.5.0'
7
7
  end
data/lib/qonfig.rb CHANGED
@@ -1,16 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'yaml'
4
+ require 'json'
4
5
  require 'erb'
5
6
 
6
7
  module Qonfig
7
8
  require_relative 'qonfig/error'
9
+ require_relative 'qonfig/loaders/basic'
10
+ require_relative 'qonfig/loaders/json'
8
11
  require_relative 'qonfig/loaders/yaml'
9
12
  require_relative 'qonfig/commands/base'
10
13
  require_relative 'qonfig/commands/add_option'
11
14
  require_relative 'qonfig/commands/add_nested_option'
12
15
  require_relative 'qonfig/commands/compose'
13
16
  require_relative 'qonfig/commands/load_from_yaml'
17
+ require_relative 'qonfig/commands/load_from_json'
14
18
  require_relative 'qonfig/commands/load_from_self'
15
19
  require_relative 'qonfig/commands/load_from_env'
16
20
  require_relative 'qonfig/commands/load_from_env/value_converter'
data/qonfig.gemspec CHANGED
@@ -6,7 +6,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
6
  require 'qonfig/version'
7
7
 
8
8
  Gem::Specification.new do |spec|
9
- spec.required_ruby_version = '>= 2.2.10'
9
+ spec.required_ruby_version = '>= 2.3.7'
10
10
 
11
11
  spec.name = 'qonfig'
12
12
  spec.version = Qonfig::VERSION
@@ -32,7 +32,7 @@ Gem::Specification.new do |spec|
32
32
  spec.add_development_dependency 'simplecov', '~> 0.14'
33
33
  spec.add_development_dependency 'simplecov-json', '~> 0.2'
34
34
  spec.add_development_dependency 'rspec', '~> 3.7'
35
- spec.add_development_dependency 'armitage-rubocop', '~> 0.3'
35
+ spec.add_development_dependency 'armitage-rubocop', '~> 0.4'
36
36
 
37
37
  spec.add_development_dependency 'bundler'
38
38
  spec.add_development_dependency 'rake'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: qonfig
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rustam Ibragimov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-06-24 00:00:00.000000000 Z
11
+ date: 2018-07-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: coveralls
@@ -72,14 +72,14 @@ dependencies:
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '0.3'
75
+ version: '0.4'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '0.3'
82
+ version: '0.4'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: bundler
85
85
  requirement: !ruby/object:Gem::Requirement
@@ -152,6 +152,7 @@ files:
152
152
  - lib/qonfig/commands/compose.rb
153
153
  - lib/qonfig/commands/load_from_env.rb
154
154
  - lib/qonfig/commands/load_from_env/value_converter.rb
155
+ - lib/qonfig/commands/load_from_json.rb
155
156
  - lib/qonfig/commands/load_from_self.rb
156
157
  - lib/qonfig/commands/load_from_yaml.rb
157
158
  - lib/qonfig/configurable.rb
@@ -159,6 +160,8 @@ files:
159
160
  - lib/qonfig/data_set/class_builder.rb
160
161
  - lib/qonfig/dsl.rb
161
162
  - lib/qonfig/error.rb
163
+ - lib/qonfig/loaders/basic.rb
164
+ - lib/qonfig/loaders/json.rb
162
165
  - lib/qonfig/loaders/yaml.rb
163
166
  - lib/qonfig/plugins.rb
164
167
  - lib/qonfig/plugins/abstract.rb
@@ -182,7 +185,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
182
185
  requirements:
183
186
  - - ">="
184
187
  - !ruby/object:Gem::Version
185
- version: 2.2.10
188
+ version: 2.3.7
186
189
  required_rubygems_version: !ruby/object:Gem::Requirement
187
190
  requirements:
188
191
  - - ">="