sinclair 2.0.1 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +3 -3
  3. data/config/check_specs.yml +2 -0
  4. data/config/yardstick.yml +5 -1
  5. data/lib/sinclair/chain_settable.rb +83 -0
  6. data/lib/sinclair/core_ext/object.rb +39 -0
  7. data/lib/sinclair/env_settable.rb +35 -17
  8. data/lib/sinclair/settable/builder.rb +140 -0
  9. data/lib/sinclair/settable/caster.rb +61 -0
  10. data/lib/sinclair/settable/class_methods.rb +80 -0
  11. data/lib/sinclair/settable.rb +115 -0
  12. data/lib/sinclair/version.rb +1 -1
  13. data/lib/sinclair.rb +2 -0
  14. data/spec/integration/yard/sinclair/env_settable_spec.rb +6 -25
  15. data/spec/integration/yard/sinclair/settable/caster_spec.rb +41 -0
  16. data/spec/integration/yard/sinclair/settable/class_methods_spec.rb +11 -0
  17. data/spec/integration/yard/sinclair/settable_spec.rb +26 -0
  18. data/spec/lib/sinclair/chain_settable_spec.rb +76 -0
  19. data/spec/lib/sinclair/env_settable_spec.rb +4 -2
  20. data/spec/lib/sinclair/settable/builder_spec.rb +47 -0
  21. data/spec/lib/sinclair/settable_spec.rb +46 -0
  22. data/spec/support/files/config.yml +1 -0
  23. data/spec/support/models/app_client.rb +1 -0
  24. data/spec/support/models/hash_app_client.rb +14 -0
  25. data/spec/support/models/hash_settable.rb +10 -0
  26. data/spec/support/models/json_env_settings.rb +18 -0
  27. data/spec/support/models/math_env_settings.rb +17 -0
  28. data/spec/support/models/my_app_client.rb +1 -0
  29. data/spec/support/models/non_default_app_client.rb +8 -0
  30. data/spec/support/models/yaml_file_settings.rb +26 -0
  31. data/spec/support/shared_examples/settable.rb +86 -0
  32. metadata +21 -5
  33. data/lib/sinclair/env_settable/builder.rb +0 -61
  34. data/spec/lib/sinclair/env_settable/builder_spec.rb +0 -35
  35. data/spec/support/shared_examples/env_settable.rb +0 -43
@@ -1,61 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Sinclair
4
- module EnvSettable
5
- # @api private
6
- # @author darthjee
7
- #
8
- # Env setting methods builder
9
- #
10
- # This builder does the magic of adding methods
11
- # that will fetch variables from env or a default value
12
- class Builder < Sinclair
13
- # @param klass [Class] Class that will receive the methods
14
- # @param prefix [String] Env keys prefix
15
- # @param (see EnvSettable#with_settings)
16
- def initialize(klass, prefix, *settings_name, **defaults)
17
- super(klass, prefix: prefix)
18
-
19
- @settings = Sinclair::InputHash.input_hash(*settings_name, **defaults)
20
-
21
- add_all_methods
22
- end
23
-
24
- private
25
-
26
- attr_reader :settings
27
- # @method settings
28
- # @private
29
- # @api private
30
- #
31
- # Settings map with default values
32
- #
33
- # @return [Hash<Symbol,Object>]
34
-
35
- delegate :prefix, to: :options_object
36
- # @method prefix
37
- # @private
38
- # @api private
39
- #
40
- # Env keys prefix
41
- #
42
- # @return [String]
43
-
44
- # @private
45
- # @api private
46
- #
47
- # Process all settings adding the methods
48
- #
49
- # @return (see settings)
50
- def add_all_methods
51
- settings.each do |name, value|
52
- key = [prefix, name].compact.join('_').to_s.upcase
53
-
54
- add_class_method(name) do
55
- ENV[key] || value
56
- end
57
- end
58
- end
59
- end
60
- end
61
- end
@@ -1,35 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- describe Sinclair::EnvSettable::Builder do
6
- subject(:settable) { Class.new }
7
-
8
- let(:username) { 'my_login' }
9
- let(:password) { Random.rand(10_000).to_s }
10
-
11
- let(:builder) do
12
- described_class.new(settable, prefix, :username, :password, host: 'my-host.com')
13
- end
14
-
15
- before { builder.build }
16
-
17
- context 'when not using prefix' do
18
- let(:prefix) { nil }
19
-
20
- let(:username_key) { 'USERNAME' }
21
- let(:password_key) { 'PASSWORD' }
22
- let(:host_key) { 'HOST' }
23
-
24
- it_behaves_like 'settings reading from env'
25
- end
26
-
27
- context 'when defining a prefix' do
28
- let(:prefix) { 'MY_APP' }
29
- let(:username_key) { 'MY_APP_USERNAME' }
30
- let(:password_key) { 'MY_APP_PASSWORD' }
31
- let(:host_key) { 'MY_APP_HOST' }
32
-
33
- it_behaves_like 'settings reading from env'
34
- end
35
- end
@@ -1,43 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- shared_examples 'settings reading from env' do
4
- before do
5
- ENV[username_key] = username
6
- ENV[password_key] = password
7
- end
8
-
9
- after do
10
- ENV.delete(username_key)
11
- ENV.delete(password_key)
12
- end
13
-
14
- it 'retrieves username from env' do
15
- expect(settable.username).to eq(username)
16
- end
17
-
18
- it 'retrieves password from env' do
19
- expect(settable.password).to eq(password)
20
- end
21
-
22
- context 'when defining defaults' do
23
- it 'returns default value' do
24
- expect(settable.host).to eq('my-host.com')
25
- end
26
-
27
- context 'when setting the env variable' do
28
- let(:other_host) { 'other-host.com' }
29
-
30
- before do
31
- ENV[host_key] = other_host
32
- end
33
-
34
- after do
35
- ENV.delete(host_key)
36
- end
37
-
38
- it 'retrieves host from env' do
39
- expect(settable.host).to eq(other_host)
40
- end
41
- end
42
- end
43
- end