knowledge 0.1.0 → 0.1.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 51d29764dd9bdb6b270f4442758757ff5c718382ea9809b4dcabe56c5699b5ae
4
- data.tar.gz: 7d3497e51d391f8749f31e7405415a49dbd769ce4a73f8e50ed287ff3c85acb7
3
+ metadata.gz: 26d7ecee28ecca4444ab9b42c7f98a0a429783fcb4692dbf13db06d30382bbef
4
+ data.tar.gz: e4775687f169d0171b5b9ab97afb0b676a0222bc90046862e4cddb248d715145
5
5
  SHA512:
6
- metadata.gz: 1bf281b1fb5c068842bebd16c71379f028726749f76ff0f2120a6b99155afa2a546a5a95193eb5e103fc4317730656c96cf5666b612015101dfc42e06e95ce46
7
- data.tar.gz: 3426d895a27222546963c49a7adbc656db3a25aa741dc845a52541f6c03dd9424d52d473cacc6d8123a68859e83383181b691e0b3826d1a2081bf29bfb32746b
6
+ metadata.gz: e338eb5b0dc2b621d66a1370c10f5080218a949c8dbf1ab1f6729b37bc48dc60288cbb09f8467898b4ccf2e496d2f8bc5a76323b27e450e3e764749b80549021
7
+ data.tar.gz: 5849a2224c0f9a436d323e868d7e69f4105d3d9c088b4c67846e886536caf35b78fbdf81946a392c5eb90094696f52e42bf80d199852999e5e248b4de501fdc2
@@ -0,0 +1,53 @@
1
+ # Ruby CircleCI 2.0 configuration file
2
+ #
3
+ # Check https://circleci.com/docs/2.0/language-ruby/ for more details
4
+ #
5
+ version: 2
6
+ jobs:
7
+ build:
8
+ docker:
9
+ - image: circleci/ruby:2.5
10
+
11
+ working_directory: ~/repo
12
+
13
+ steps:
14
+ - checkout
15
+
16
+ # Download and cache dependencies
17
+ - restore_cache:
18
+ keys:
19
+ - v1-dependencies-{{ checksum "Gemfile.lock" }}
20
+ # fallback to using the latest cache if no exact match is found
21
+ - v1-dependencies-
22
+
23
+ - run:
24
+ name: install dependencies
25
+ command: |
26
+ bundle install --jobs=4 --retry=3 --path vendor/bundle
27
+
28
+ - save_cache:
29
+ paths:
30
+ - ./vendor/bundle
31
+ key: v1-dependencies-{{ checksum "Gemfile.lock" }}
32
+
33
+ - run:
34
+ name: RSpec specs
35
+ command: |
36
+ mkdir /tmp/test-results
37
+ TEST_FILES="$(circleci tests glob "spec/**/*_spec.rb" | circleci tests split --split-by=timings)"
38
+
39
+ bundle exec rspec --format progress \
40
+ --out /tmp/test-results/rspec.xml \
41
+ --format progress \
42
+ $TEST_FILES
43
+
44
+ # collect reports
45
+ - store_test_results:
46
+ path: /tmp/test-results
47
+ - store_artifacts:
48
+ path: /tmp/test-results
49
+ destination: test-results
50
+
51
+ - run:
52
+ name: Rubocop linting
53
+ command: bundle exec rubocop
data/.gitignore CHANGED
@@ -10,4 +10,5 @@
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
12
 
13
- /.ruby-*
13
+ /.ruby-*
14
+ /knowledge-*.gem
@@ -4,6 +4,9 @@ Metrics/LineLength:
4
4
  Metrics/MethodLength:
5
5
  Max: 50
6
6
 
7
+ Metrics/ClassLength:
8
+ Max: 200
9
+
7
10
  Metrics/BlockLength:
8
11
  Exclude:
9
12
  - knowledge.gemspec
data/README.md CHANGED
@@ -2,11 +2,14 @@
2
2
 
3
3
  Configuration variables are a project's knowledge. This gem is here to help your projects learn what they need to work properly.
4
4
 
5
+ Knowledge is a multi-source highly customizable configuration variables manager.
6
+
7
+
5
8
  ## Disclaimer
6
9
 
7
- The full documentation is currently being written. You should be able to find a better documentation in a few hours.
10
+ The full documentation is currently being written. You should be able to find a better documentation in a few hours / days.
8
11
 
9
- Waiting for the full documentation, you can have a look at the code which is already well-documented.
12
+ Waiting for the full documentation, you can have a look at the code which is already well-documented or at the [wiki](https://github.com/knowledge-ruby/knowledge/wiki)
10
13
 
11
14
  ## Installation
12
15
 
@@ -40,6 +43,43 @@ knowledge.gather!
40
43
  Knowledge::Configuration.key # => "value"
41
44
  ```
42
45
 
46
+ **Using a config file**:
47
+
48
+ ```yml
49
+ key: value
50
+ ```
51
+
52
+ ```ruby
53
+ knowledge = Knowledge::Learner.new
54
+
55
+ knowledge.use(name: :file)
56
+ knowledge.variables = 'path/to/file.yml'
57
+ knowledge.gather!
58
+
59
+ Knowledge::Configuration.key # => "value"
60
+ ```
61
+
62
+ Or
63
+
64
+ ```yml
65
+ development:
66
+ key: value
67
+ production:
68
+ key: other_value
69
+ ```
70
+
71
+ ```ruby
72
+ Knowledge.config.environment = :production
73
+
74
+ knowledge = Knowledge::Learner.new
75
+
76
+ knowledge.use(name: :file)
77
+ knowledge.variables = 'path/to/file.yml'
78
+ knowledge.gather!
79
+
80
+ Knowledge::Configuration.key # => "other_value"
81
+ ```
82
+
43
83
  **Using your own setter**:
44
84
 
45
85
  ```ruby
@@ -7,11 +7,11 @@ require 'knowledge/exceptions'
7
7
  require 'knowledge/learner'
8
8
 
9
9
  #
10
- # === Description ===
10
+ # === Description
11
11
  #
12
12
  # Configuration is your project's knowledge, let's make it very simple!
13
13
  #
14
- # === Configuration ===
14
+ # === Configuration
15
15
  #
16
16
  # Funny but quite normal, this gem needs some config.
17
17
  # If you're familiar with dry-configurable, it should be very understandable for you.
@@ -27,7 +27,7 @@ require 'knowledge/learner'
27
27
  # config.environment = ENV['RACK_ENV'] || Rails.env || ENV['APP_ENV'] # Or whatever you want
28
28
  # end
29
29
  #
30
- # === Usage ===
30
+ # === Usage
31
31
  #
32
32
  # @example:
33
33
  # Knowledge.configure do |config|
@@ -47,9 +47,9 @@ require 'knowledge/learner'
47
47
  # learner.gather!
48
48
  #
49
49
  module Knowledge
50
- # == Behaviors ===================================================================================================
50
+ # == Behaviors =======================================================================================================
51
51
  extend Dry::Configurable
52
52
 
53
- # == Settings ====================================================================================================
53
+ # == Settings ========================================================================================================
54
54
  setting :environment, :development
55
55
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Knowledge
4
4
  #
5
- # === Description ===
5
+ # === Description
6
6
  #
7
7
  # This is the namespace used to put the lib's adapters.
8
8
  # You can find the adapters by looking to the inclusions below or to the adapters folder.
@@ -3,29 +3,43 @@
3
3
  module Knowledge
4
4
  module Adapters
5
5
  #
6
- # === Description ===
6
+ # === Description
7
7
  #
8
8
  # This adapter is the base adapter.
9
9
  # It does nothing specific but is meant to manage all generic stuff.
10
10
  #
11
- # === Usage ===
11
+ # === Usage
12
12
  #
13
13
  # Just inherit from it
14
14
  #
15
- # === Attributes ===
15
+ # @example:
16
+ #
17
+ # class MySuperAdapter < Knowledge::Adapters::Base; end
18
+ #
19
+ # === Attributes
16
20
  #
17
21
  # @attr_reader [Class] setter
18
22
  # @attr_reader [Hash] variables
19
23
  #
20
24
  class Base
21
25
  # == Attributes ==================================================================================================
22
- attr_reader :setter, :variables
26
+
27
+ # Setter object used to set variables once retrieved
28
+ attr_reader :setter
29
+
30
+ # Variables descriptor
31
+ attr_reader :variables
23
32
 
24
33
  # == Constructor =================================================================================================
34
+
25
35
  #
26
- # @option [Hash] :variables
27
- # @option [Class] :setter
28
- # @option [Hash] :params
36
+ # Just initializes instance variables with given params
37
+ #
38
+ # === Parameters
39
+ #
40
+ # @param :variables [Hash]
41
+ # @param :setter [Class]
42
+ # @param :params [Hash]
29
43
  #
30
44
  def initialize(variables:, setter:, params: nil) # rubocop:disable Lint/UnusedMethodArgument
31
45
  @variables = variables
@@ -33,13 +47,12 @@ module Knowledge
33
47
  end
34
48
 
35
49
  # == Instance Methods ============================================================================================
36
- #
37
- # === Description ===
50
+
38
51
  #
39
52
  # Should run the actual adapter.
40
53
  # This method is meant to be overriden
41
54
  #
42
- # === Errors ===
55
+ # === Errors
43
56
  #
44
57
  # @raise [Knowledge::AdapterRunMethodNotImplemented] if not overridden by subclasses
45
58
  #
@@ -3,26 +3,30 @@
3
3
  module Knowledge
4
4
  module Adapters
5
5
  #
6
- # === Description ===
6
+ # === Description
7
7
  #
8
8
  # This adapter takes some vars in ENV vars and put it in your project's config.
9
9
  #
10
- # === Usage ===
10
+ # === Usage
11
11
  #
12
12
  # @example:
13
+ # # Define your vars with the name of the variable as key and the name of the env var as value
14
+ # my_vars = { application_token: 'APPLICATION_TOKEN', aws_secret: 'AWS_SECRET_KEY' }
15
+ #
16
+ # # Instanciate the adapter
13
17
  # adapter = Knowledge::Adapters::Environment.new(setter: MySetter, variables: my_vars)
14
18
  #
19
+ # # And run it
15
20
  # adapter.run
16
21
  #
17
- # === Attributes ===
22
+ # === Attributes
18
23
  #
19
24
  # @attr_reader [Class] setter
20
25
  # @attr_reader [Hash] variables
21
26
  #
22
27
  class Environment < Base
23
28
  # == Instance Methods ============================================================================================
24
- #
25
- # === Description ===
29
+
26
30
  #
27
31
  # Runs the actual adapter.
28
32
  #
@@ -3,19 +3,26 @@
3
3
  module Knowledge
4
4
  module Adapters
5
5
  #
6
- # === Description ===
6
+ # === Description
7
7
  #
8
8
  # This adapter takes some vars in a config file and put it in your project's config.
9
9
  # The config file should provide some YAML with key=value format.
10
10
  #
11
- # === Usage ===
11
+ # It works exactly like the KeyValue adapter, just pass a path to the learner as variables descriptor.
12
+ #
13
+ # === Usage
12
14
  #
13
15
  # @example:
16
+ # # Define your vars with the name of the variable as key and the value as value
17
+ # my_vars = { application_token: 's3cret', aws_secret: 's3cret' }
18
+ #
19
+ # # Initializes the adapter
14
20
  # adapter = Knowledge::Adapters::File.new(setter: MySetter, variables: my_vars)
15
21
  #
22
+ # # And run it
16
23
  # adapter.run
17
24
  #
18
- # === Attributes ===
25
+ # === Attributes
19
26
  #
20
27
  # @attr_reader [Class] setter
21
28
  # @attr_reader [Hash] variables
@@ -3,27 +3,31 @@
3
3
  module Knowledge
4
4
  module Adapters
5
5
  #
6
- # === Description ===
6
+ # === Description
7
7
  #
8
8
  # This adapter takes some vars in a config object and put it in your project's config.
9
9
  # The config object should provide a hash with key=value format.
10
10
  #
11
- # === Usage ===
11
+ # === Usage
12
12
  #
13
13
  # @example:
14
+ # # Define your vars with the name of the variable as key and the value as value
15
+ # my_vars = { application_token: 's3cret', aws_secret: 's3cret' }
16
+ #
17
+ # # Instanciate the adapter
14
18
  # adapter = Knowledge::Adapters::KeyValue.new(setter: MySetter, variables: my_vars)
15
19
  #
20
+ # # And run it
16
21
  # adapter.run
17
22
  #
18
- # === Attributes ===
23
+ # === Attributes
19
24
  #
20
25
  # @attr_reader [Class] setter
21
26
  # @attr_reader [Hash] variables
22
27
  #
23
28
  class KeyValue < Base
24
29
  # == Instance Methods ============================================================================================
25
- #
26
- # === Description ===
30
+
27
31
  #
28
32
  # Runs the actual adapter.
29
33
  #
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yaml'
4
+
5
+ require 'knowledge/configuration'
6
+
7
+ module Knowledge
8
+ #
9
+ # === Description
10
+ #
11
+ # Object used to backup configuration variables.
12
+ #
13
+ # === Usage
14
+ #
15
+ # @example:
16
+ #
17
+ # backupper = Knowledge::Backupper.new
18
+ #
19
+ # backupper.register(name: :foo, value: 'bar')
20
+ # backupper.register(name: :bar, value: 'baz')
21
+ #
22
+ # backupper.backup!
23
+ #
24
+ # === Attributes
25
+ #
26
+ # @attr_reader [Hash] configuration
27
+ # @attr_reader [String] path
28
+ #
29
+ class Backupper
30
+ # == Attributes ====================================================================================================
31
+
32
+ # Configuration hash
33
+ attr_reader :configuration
34
+
35
+ # Backup file path
36
+ attr_reader :path
37
+
38
+ # == Constructor ===================================================================================================
39
+
40
+ #
41
+ # Just sets the basic configuration object.
42
+ #
43
+ # === Parameters
44
+ #
45
+ # @param :path [String] Path to the YAML file where to backup the config
46
+ #
47
+ def initialize(path:)
48
+ @configuration = {}
49
+ @path = path
50
+ end
51
+
52
+ # == Instance methods ==============================================================================================
53
+
54
+ #
55
+ # Backups the configuration.
56
+ #
57
+ def backup!
58
+ f = File.new(path, 'w')
59
+
60
+ f.write(configuration.to_yaml)
61
+
62
+ f.close
63
+ end
64
+
65
+ #
66
+ # Sets the variable before backuping everything.
67
+ #
68
+ # === Parameters
69
+ #
70
+ # @param :name [String | Symbol]
71
+ # @param :value [Any]
72
+ #
73
+ def set(name:, value:)
74
+ configuration[name.to_sym] = value
75
+ end
76
+ alias register set
77
+ end
78
+ end
@@ -2,14 +2,14 @@
2
2
 
3
3
  module Knowledge
4
4
  #
5
- # === Description ===
5
+ # === Description
6
6
  #
7
7
  # If the project has no defined way to manage its config, why not providing it our way?
8
8
  #
9
9
  class Configuration
10
10
  class << self
11
11
  #
12
- # === Description ===
12
+ # === Description
13
13
  #
14
14
  # We're doing black magic (a.k.a metaprogramming) to set variables directly on this class.
15
15
  # When programmers do black magic, they should redefine some basic things that are broken as a side effect.
@@ -18,21 +18,25 @@ module Knowledge
18
18
  #
19
19
  # It allows us to see every variable that has been defined on the class by metaprogramming.
20
20
  #
21
- # === Usage ===
21
+ # === Usage
22
22
  #
23
23
  # @example:
24
24
  # # Do some magic before
25
25
  # Knowledge.config.environment = :development
26
26
  # learner = Knowledge::Learner.new
27
+ #
27
28
  # learner.use name: :default
28
29
  # learner.variables = { foo: :bar }
30
+ #
29
31
  # learner.gather!
32
+ #
30
33
  # # And then inspect it
31
34
  # puts Knowledge::Configuration.inspect
32
35
  # # => #<Knowledge::Configuration:0x000047116740290980 @foo="bar">
36
+ #
33
37
  # # Wonderful, isn't it?
34
38
  #
35
- # === Parameters ===
39
+ # === Parameters
36
40
  #
37
41
  # @return [String] as expected
38
42
  #
@@ -2,13 +2,13 @@
2
2
 
3
3
  module Knowledge
4
4
  #
5
- # === Description ===
5
+ # === Description
6
6
  #
7
7
  # This error class should be considered as the lib's standard error.
8
8
  # All error classes inherit from this one.
9
9
  # The goal is to be able to rescue Knowledge::Error outside the lib and catch them all.
10
10
  #
11
- # === Usage ===
11
+ # === Usage
12
12
  #
13
13
  # You have many examples behind. Just inherit from this class and it's ok.
14
14
  #
@@ -18,28 +18,28 @@ module Knowledge
18
18
  class Error < ::StandardError; end
19
19
 
20
20
  #
21
- # === Description ===
21
+ # === Description
22
22
  #
23
23
  # This error is used when, at some point, we can't find the adapter we're looking for
24
24
  #
25
25
  class AdapterNotFound < Error; end
26
26
 
27
27
  #
28
- # === Description ===
28
+ # === Description
29
29
  #
30
30
  # This error is used when an adapter has no #run method declared
31
31
  #
32
32
  class AdapterRunMethodNotImplemented < Error; end
33
33
 
34
34
  #
35
- # === Description ===
35
+ # === Description
36
36
  #
37
37
  # This error is pretty generic and is meant to be used when we're not able to gather infos in order to set vars.
38
38
  #
39
39
  class LearnError < Error; end
40
40
 
41
41
  #
42
- # === Description ===
42
+ # === Description
43
43
  #
44
44
  # This error is used when we fail registering an adapter.
45
45
  #
@@ -2,19 +2,21 @@
2
2
 
3
3
  module Knowledge
4
4
  #
5
- # === Description ===
5
+ # === Description
6
6
  #
7
7
  # We all need an initializer, here's this lib's initializer.
8
8
  # Its role is to gather all informations and run the enabled adapters.
9
9
  #
10
- # === Usage ===
10
+ # === Usage
11
11
  #
12
12
  # @example:
13
13
  # Knowledge::Initializer.new(adapters: adapters, params: { foo: :bar }, setter: setter, variables: variables).run
14
14
  #
15
+ # # or
16
+ #
15
17
  # Knowledge::Initializer.run(adapters: adapters, params: { foo: :bar }, setter: setter, variables: variables)
16
18
  #
17
- # === Attributes ===
19
+ # === Attributes
18
20
  #
19
21
  # @attr_reader [Array<String | Symbol>] adapters
20
22
  # @attr_reader [Hash] params
@@ -22,15 +24,31 @@ module Knowledge
22
24
  # @attr_reader [Hash] variables
23
25
  #
24
26
  class Initializer
25
- # == Attributes ==================================================================================================
26
- attr_reader :adapters, :params, :setter, :variables
27
+ # == Attributes ====================================================================================================
28
+
29
+ # Adapters that will be used to retrieve variables' values
30
+ attr_reader :adapters
31
+
32
+ # Additionnal parameters to pass to the adapters
33
+ attr_reader :params
34
+
35
+ # Setter used to set variables after having retrieved their values
36
+ attr_reader :setter
37
+
38
+ # Descriptor for the variables to retrieve/fetch
39
+ attr_reader :variables
27
40
 
28
41
  # == Constructor =================================================================================================
42
+
43
+ #
44
+ # Just initializes instance variables with given params.
29
45
  #
30
- # @option [Array<Class>] :adapters
31
- # @option [Hash] :params
32
- # @option [Class] :setter
33
- # @option [Hash] :variables
46
+ # === Parameters
47
+ #
48
+ # @param :adapters [Array<Class>]
49
+ # @param :params [Hash]
50
+ # @param :setter [Class]
51
+ # @param :variables [Hash]
34
52
  #
35
53
  def initialize(adapters:, params:, setter:, variables:)
36
54
  @adapters = adapters
@@ -42,16 +60,16 @@ module Knowledge
42
60
  # == Class methods ===============================================================================================
43
61
  class << self
44
62
  #
45
- # === Description ===
63
+ # === Description
46
64
  #
47
65
  # Instanciates the current class and runs all registered adapters.
48
66
  #
49
- # === Parameters ===
67
+ # === Parameters
50
68
  #
51
- # @option [Hash{Symbol => Class}] :adapters
52
- # @option [Hash] :params
53
- # @option [Class] :setter
54
- # @option [Hash] :variables
69
+ # @param :adapters [Hash{Symbol => Class}]
70
+ # @param :params [Hash]
71
+ # @param :setter [Class]
72
+ # @param :variables [Hash]
55
73
  #
56
74
  def run(adapters:, params:, setter:, variables: {})
57
75
  new(adapters: adapters, params: params, setter: setter, variables: variables).run
@@ -59,8 +77,9 @@ module Knowledge
59
77
  end
60
78
 
61
79
  # == Instance methods ============================================================================================
80
+
62
81
  #
63
- # === Description ===
82
+ # === Description
64
83
  #
65
84
  # Runs all registered adapters.
66
85
  #
@@ -5,20 +5,21 @@ require 'yaml'
5
5
  require 'knowledge/initializer'
6
6
  require 'knowledge/adapters'
7
7
  require 'knowledge/setter'
8
+ require 'knowledge/backupper'
8
9
 
9
10
  module Knowledge
10
11
  #
11
- # === Description ===
12
+ # === Description
12
13
  #
13
14
  # Knowledge is something that needs some learning.
14
15
  #
15
- # === Usage ===
16
+ # === Usage
16
17
  #
17
18
  # @example:
18
19
  #
19
20
  # learner = Knowledge::Learner.new
20
21
  #
21
- # === Attributes ===
22
+ # === Attributes
22
23
  #
23
24
  # @attr_reader [Hash] additionnal_params
24
25
  # @attr_reader [Hash{Symbol => Class}] available_adapters
@@ -28,10 +29,27 @@ module Knowledge
28
29
  #
29
30
  class Learner
30
31
  # == Attributes ====================================================================================================
32
+
33
+ # Config variables setter - can be overridden from outside of the object
31
34
  attr_accessor :setter
32
- attr_reader :additionnal_params, :available_adapters, :enabled_adapters, :variables
35
+
36
+ # Additionnal params to be passed to the adapter
37
+ attr_reader :additionnal_params
38
+
39
+ # List of available adapters
40
+ attr_reader :available_adapters
41
+
42
+ # List of enabled adapters
43
+ attr_reader :enabled_adapters
44
+
45
+ # Descriptor object for the variables to set/fetch
46
+ attr_reader :variables
33
47
 
34
48
  # == Constructor ===================================================================================================
49
+
50
+ #
51
+ # Just sets default value for instance variables.
52
+ #
35
53
  def initialize
36
54
  @additionnal_params = {}
37
55
  @available_adapters = {}
@@ -44,6 +62,31 @@ module Knowledge
44
62
  class << self
45
63
  attr_reader :adapters
46
64
 
65
+ #
66
+ # Registers given adapter as a default one.
67
+ # This method is meant to be used only by libraries adding adapters to the lib.
68
+ #
69
+ # @see Knowledge::Learner#use for more details on how details are used.
70
+ #
71
+ # === Usage
72
+ #
73
+ # @example:
74
+ # # Define your adapter
75
+ # class SuperServiceAdapter < ::Knowledge::Adapters::Base; end
76
+ #
77
+ # # Register your adapter
78
+ # Knowledge::Learner.register_default_adapter(names: %i[super_service service], klass: SuperServiceAdapter)
79
+ #
80
+ # # Now you can use it the same way every other default adapters
81
+ # learner = Knowledge::Learner.new
82
+ #
83
+ # learner.use(name: :super_service)
84
+ #
85
+ # === Parameters
86
+ #
87
+ # @param :names [Array<String | Symbol>] The supported names
88
+ # @param :klass [Class] The actual adapter class
89
+ #
47
90
  def register_default_adapter(names:, klass:)
48
91
  @adapters ||= {}
49
92
 
@@ -52,16 +95,46 @@ module Knowledge
52
95
  end
53
96
 
54
97
  # == Instance methods ==============================================================================================
98
+
99
+ #
100
+ # Gathers all the knowledge and backups it
101
+ #
102
+ # === Usage
103
+ #
104
+ # @example:
105
+ # learner = Knowledge::Learner.new
106
+ #
107
+ # # Do some config (add adapters, define your setter, etc.)
108
+ #
109
+ # learner.backup!
110
+ #
111
+ # === Parameters
112
+ #
113
+ # @param :path [String] Path to the YAML file where to backup the config
55
114
  #
56
- # === Description ===
115
+ def backup!(path:)
116
+ backupper = ::Knowledge::Backupper.new(path: path)
117
+
118
+ ::Knowledge::Initializer.new(
119
+ adapters: enabled_adapters,
120
+ params: additionnal_params,
121
+ setter: backupper,
122
+ variables: variables
123
+ ).run
124
+
125
+ backupper.backup!
126
+ end
127
+
57
128
  #
58
129
  # Gathers all the knowledge and put it into your app.
59
130
  #
60
- # === Usage ===
131
+ # === Usage
61
132
  #
62
133
  # @example:
63
134
  # learner = Knowledge::Learner.new
135
+ #
64
136
  # # Do some config (add adapters, define your setter, etc.)
137
+ #
65
138
  # learner.gather!
66
139
  #
67
140
  def gather!
@@ -74,89 +147,90 @@ module Knowledge
74
147
  end
75
148
 
76
149
  # == Adapters methods ============================================================================================
77
- #
78
- # === Description ===
150
+
79
151
  #
80
152
  # Sets additional params to be passed to the adapter through params option.
81
153
  #
82
- # === Usage ===
154
+ # === Usage
83
155
  #
84
156
  # @example:
85
157
  # learner = Knowledge::Learner.new
158
+ #
86
159
  # learner.add_adapter_param(adapter: :custom, name: :base_path, value: '/base/path')
87
160
  #
88
- # === Parameters ===
161
+ # === Parameters
89
162
  #
90
- # @option [String | Symbol] :adapter
91
- # @option [String | Symbol] :name
92
- # @option [any] :value
163
+ # @param :adapter [String | Symbol] The adapter's name
164
+ # @param :name [String | Symbol] The parameter's name
165
+ # @param :value [any] The parameter's value
93
166
  #
94
167
  def add_adapter_param(adapter:, name:, value:)
95
168
  @additionnal_params[adapter.to_sym] ||= {}
96
169
  @additionnal_params[adapter.to_sym][name] = value
97
170
  end
98
171
 
99
- #
100
- # === Description ===
101
172
  #
102
173
  # Sets additional params to be passed to the adapter through params option.
103
174
  #
104
- # === Usage ===
175
+ # === Usage
105
176
  #
106
177
  # @example:
107
178
  # learner = Knowledge::Learner.new
179
+ #
108
180
  # learner.add_adapter_param(name: :base_path, value: '/base/path')
109
181
  #
110
- # === Parameters ===
182
+ # === Parameters
111
183
  #
112
- # @option [String | Symbol] :adapter
113
- # @option [any] :params
184
+ # @param :adapter [String | Symbol] Adapter's name
185
+ # @param :params [any] Params to pass
114
186
  #
115
187
  def add_adapter_params(adapter:, params:)
116
188
  @additionnal_params[adapter.to_sym] = params
117
189
  end
118
190
 
119
- #
120
- # === Description ===
121
191
  #
122
192
  # Disables an adapter.
123
193
  #
124
- # === Usage ===
194
+ # === Usage
125
195
  #
126
196
  # @example:
127
197
  # learner = Knowledge::Learner.new
198
+ #
128
199
  # learner.register_adapter(name: :my_adapter, klass: MyAdapter, enable: true)
200
+ #
129
201
  # # Somewhere else in the code
130
202
  # learner.disable_adapter(name: :my_adapter) if should_disable_custom_adapter?
131
203
  #
132
- # === Parameters ===
204
+ # === Parameters
133
205
  #
134
- # @option [String | Symbol] :name
206
+ # @param :name [String | Symbol]
135
207
  #
136
208
  def disable_adapter(name:)
137
209
  @enabled_adapters.delete(name.to_sym)
138
210
  end
139
211
 
140
- #
141
- # === Description ===
142
212
  #
143
213
  # Enables an adapter.
144
214
  #
145
- # === Usage ===
215
+ # === Usage
146
216
  #
147
217
  # @example:
148
218
  # learner = Knowledge::Learner.new
219
+ #
220
+ # # Register your adapter
149
221
  # learner.register_adapter(name: :my_adapter, klass: MyAdapter)
222
+ #
223
+ # # Now you can enable it
150
224
  # learner.enable_adapter(name: :my_adapter)
151
225
  #
152
- # === Errors ===
226
+ # === Errors
153
227
  #
154
228
  # @raises [Knowledge::AdapterNotFound] if adapter is not available
155
229
  #
156
- # === Parameters ===
230
+ # === Parameters
157
231
  #
158
- # @option [String | Symbol] :name
159
- # @option [Hash | String | nil] :variables
232
+ # @param :name [String | Symbol]
233
+ # @param :variables [Hash | String | nil]
160
234
  #
161
235
  def enable_adapter(name:, variables: nil)
162
236
  _key, klass = available_adapters.find { |key, _klass| key.to_sym == name.to_sym }
@@ -167,23 +241,22 @@ module Knowledge
167
241
  set_adapter_variables(name: name, variables: variables)
168
242
  end
169
243
 
170
- #
171
- # === Description ===
172
244
  #
173
245
  # Registers an adapter and enable it if asked.
174
246
  #
175
- # === Usage ===
247
+ # === Usage
176
248
  #
177
249
  # @example:
178
250
  # learner = Knowledge::Learner.new
251
+ #
179
252
  # learner.register_adapter(name: :my_adapter, klass: MyAdapter, enable: true)
180
253
  #
181
- # === Parameters ===
254
+ # === Parameters
182
255
  #
183
- # @option [String | Symbol] :name
184
- # @option [Class] :klass
185
- # @option [Boolean] :enable
186
- # @option [Hash | String | nil] :variables
256
+ # @param :name [String | Symbol]
257
+ # @param :klass [Class]
258
+ # @param :enable [Boolean]
259
+ # @param :variables [Hash | String | nil]
187
260
  #
188
261
  def register_adapter(name:, klass:, enable: false, variables: nil)
189
262
  @available_adapters[name.to_sym] = klass
@@ -192,20 +265,19 @@ module Knowledge
192
265
  end
193
266
 
194
267
  #
195
- # === Description ===
196
- #
197
- # Sets variables for a given adapter
268
+ # Sets variables for a given adapter.
198
269
  #
199
- # === Usage ===
270
+ # === Usage
200
271
  #
201
272
  # @example:
202
273
  # learner = Knowledge::Learner.new
274
+ #
203
275
  # learner.set_adapter_variables(name: :default, variables: { foo: :bar })
204
276
  #
205
- # === Attributes ===
277
+ # === Parameters
206
278
  #
207
- # @option [String | Symbol] :name
208
- # @option [Hash | nil] :variables
279
+ # @param :name [String | Symbol]
280
+ # @param :variables [Hash | nil]
209
281
  #
210
282
  def set_adapter_variables(name:, variables: nil)
211
283
  return unless variables
@@ -223,20 +295,19 @@ module Knowledge
223
295
  end
224
296
 
225
297
  #
226
- # === Description ===
298
+ # Sets variables as a hash for a given adapter.
227
299
  #
228
- # Sets variables as a hash for a given adapter
229
- #
230
- # === Usage ===
300
+ # === Usage
231
301
  #
232
302
  # @example
233
303
  # learner = Knowledge::Learner.new
304
+ #
234
305
  # learner.set_adapter_variables_by_hash(name: :default, variables: { foo: :bar })
235
306
  #
236
- # === Attributes ===
307
+ # === Parameters
237
308
  #
238
- # @option [String | Symbol] :name
239
- # @option [Hash] :variables
309
+ # @param :name [String | Symbol]
310
+ # @param :variables [Hash]
240
311
  #
241
312
  def set_adapter_variables_by_hash(name:, variables:)
242
313
  variables = variables[name.to_s] if variables.key?(name.to_s)
@@ -244,22 +315,22 @@ module Knowledge
244
315
  @variables[name.to_sym] = variables
245
316
  end
246
317
 
247
- #
248
- # === Description ===
249
318
  #
250
319
  # Unregisters an adapter and disable it.
251
320
  #
252
- # === Usage ===
321
+ # === Usage
253
322
  #
254
323
  # @example:
255
324
  # learner = Knowledge::Learner.new
325
+ #
256
326
  # learner.register_adapter(name: :my_adapter, klass: MyAdapter)
327
+ #
257
328
  # # somewhere else in the code
258
329
  # learner.unregister_adapter(name: :my_adapter)
259
330
  #
260
- # === Parameters ===
331
+ # === Parameters
261
332
  #
262
- # @option [String | Symbol] :name
333
+ # @param :name [String | Symbol]
263
334
  #
264
335
  def unregister_adapter(name:)
265
336
  disable_adapter(name: name)
@@ -267,20 +338,22 @@ module Knowledge
267
338
  end
268
339
 
269
340
  #
270
- # === Description ===
341
+ # Registers & enables one of the lib's default adapters.
271
342
  #
272
- # Registers & enables one of the lib's adapters.
343
+ # If you're writing a gem to add an adapter to knowledge, please have a look at
344
+ # Knowledge::Learned#register_default_adapter
273
345
  #
274
- # === Usage ===
346
+ # === Usage
275
347
  #
276
348
  # @example:
277
349
  # learner = Knowledge::Learner.new
350
+ #
278
351
  # learner.use(name: :ssm)
279
352
  #
280
- # === Parameters ===
353
+ # === Parameters
281
354
  #
282
- # @option [String | Symbol] name
283
- # @option [Boolean] enable
355
+ # @param :name [String | Symbol]
356
+ # @param :enable [Boolean]
284
357
  #
285
358
  def use(name:, enable: true)
286
359
  adapter = self.class.adapters[name.to_sym]
@@ -291,33 +364,36 @@ module Knowledge
291
364
  end
292
365
 
293
366
  # == Variables config ==============================================================================================
294
- #
295
- # === Description ===
367
+
296
368
  #
297
369
  # Setter for the variables config.
298
370
  #
299
- # === Usage ===
371
+ # === Usage
300
372
  #
301
373
  # @example:
302
374
  # learner = Knowledge::Learner.new
375
+ #
303
376
  # learner.variables = { name: 'value' }
304
377
  #
305
378
  # @example:
306
379
  # learner = Knowledge::Learner.new
380
+ #
307
381
  # learner.use(name: :env)
382
+ #
308
383
  # learner.variables = { name: 'ENV_KEY' }
309
384
  #
310
385
  # @example:
311
386
  # learner = Knowledge::Learner.new
387
+ #
312
388
  # learner.variables = 'path/to/vars/config/file.yml'
313
389
  #
314
- # === Errors ===
390
+ # === Errors
315
391
  #
316
392
  # @raise [Knowledge::LearnError] if parameter isn't a hash or a string
317
393
  #
318
394
  # === Parameters
319
395
  #
320
- # @param [String | Hash] path_or_descriptor
396
+ # @param path_or_descriptor [String | Hash]
321
397
  #
322
398
  def variables=(path_or_descriptor)
323
399
  case path_or_descriptor
@@ -332,14 +408,12 @@ module Knowledge
332
408
 
333
409
  protected
334
410
 
335
- #
336
- # === Description ===
337
411
  #
338
412
  # Opens the config file and sets the variable config.
339
413
  #
340
- # === Parameters ===
414
+ # === Parameters
341
415
  #
342
- # @param [String] path
416
+ # @param path [String]
343
417
  #
344
418
  def fetch_variables_config(path)
345
419
  descriptor = yaml_content(path)
@@ -347,13 +421,11 @@ module Knowledge
347
421
  end
348
422
 
349
423
  #
350
- # === Description ===
424
+ # Loads YAML file content.
351
425
  #
352
- # Loads YAML file content
353
- #
354
- # === Parameters ===
426
+ # === Parameters
355
427
  #
356
- # @param [String] path
428
+ # @param path [String]
357
429
  #
358
430
  # @return [Hash]
359
431
  #
@@ -4,32 +4,38 @@ require 'knowledge/configuration'
4
4
 
5
5
  module Knowledge
6
6
  #
7
- # === Description ===
7
+ # === Description
8
8
  #
9
9
  # If you don't have a defined strategy to manage config for your project, you can use Knowledge's one.
10
- # This is the custom setter
10
+ # This is the custom setter.
11
11
  #
12
- # === Usage ===
12
+ # === Usage
13
13
  #
14
14
  # @example:
15
+ #
15
16
  # Knowledge::Setter.new.set(name: :foo, value: 'bar')
16
17
  #
17
18
  class Setter
18
19
  # == Constructor ===================================================================================================
20
+
21
+ #
22
+ # Just sets the basic configuration object.
23
+ #
19
24
  def initialize
20
25
  @configuration = ::Knowledge::Configuration
21
26
  end
22
27
 
23
28
  # == Instance methods ==============================================================================================
29
+
24
30
  #
25
- # === Description ===
31
+ # === Description
26
32
  #
27
- # Sets the variable by doing black magic on Knowledge::Configuration
33
+ # Sets the variable by doing black magic on Knowledge::Configuration.
28
34
  #
29
- # === Parameters ===
35
+ # === Parameters
30
36
  #
31
- # @option [String | Symbol] :name
32
- # @option [Any] :value
37
+ # @param :name [String | Symbol]
38
+ # @param :value [Any]
33
39
  #
34
40
  def set(name:, value:)
35
41
  @configuration.singleton_class.class_eval { attr_accessor name.to_sym } unless @configuration.respond_to?(name)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Knowledge
4
- VERSION = '0.1.0'
4
+ VERSION = '0.1.1'.freeze
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knowledge
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Johann Wilfrid-Calixte
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-12-01 00:00:00.000000000 Z
11
+ date: 2018-12-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-configurable
@@ -115,6 +115,7 @@ executables: []
115
115
  extensions: []
116
116
  extra_rdoc_files: []
117
117
  files:
118
+ - ".circleci/config.yml"
118
119
  - ".gitignore"
119
120
  - ".rspec"
120
121
  - ".rubocop.yml"
@@ -135,6 +136,7 @@ files:
135
136
  - lib/knowledge/adapters/environment.rb
136
137
  - lib/knowledge/adapters/file.rb
137
138
  - lib/knowledge/adapters/key_value.rb
139
+ - lib/knowledge/backupper.rb
138
140
  - lib/knowledge/configuration.rb
139
141
  - lib/knowledge/exceptions.rb
140
142
  - lib/knowledge/initializer.rb