pifano_config 0.1.0 → 0.2.0
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 +4 -4
- data/changelog.md +47 -0
- data/lib/pifano/config.rb +94 -26
- data/lib/pifano_config/version.rb +4 -1
- data/lib/pifano_config.rb +3 -0
- data/license.md +21 -0
- data/readme.md +31 -0
- metadata +11 -15
- data/CHANGELOG.md +0 -22
- data/README.md +0 -92
- data/lib/pifano/option_builder.rb +0 -17
- data/lib/pifano/option_factory.rb +0 -38
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8389a6716f4d2a09bf392dd77755023ae24b08785a132686d03dfd90c2ac339a
|
4
|
+
data.tar.gz: 6d165e8f16da592041d33877f8ea1dcd573f2a1cc427b64ed36bc80b6926d2c0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d5fc333691f0ae8f8f5314f5255c0a9edc7539c6db8b7f56ed0f1d5b772d646af9e682b122a1091466121f5efa854532c9a3a8be4ea47d831dcc02e78dc269b0
|
7
|
+
data.tar.gz: b07a4508e21ac677c6f9cb102dd51993da118ead8199b66c509497cb2575acfdd803aca3f52466b31bfa58fe41d9e9cdea0bab65873a755b32d55a7ac08ecd93
|
data/changelog.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
All notable changes to this project will be documented in this file.
|
4
|
+
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
|
+
|
8
|
+
## [Unreleased]
|
9
|
+
|
10
|
+
## [0.2.0] - 2024-01-22
|
11
|
+
|
12
|
+
### Added
|
13
|
+
|
14
|
+
- Added simple getters for configuration values. Now you can get the value of a configuration key by calling `[]`. Ex.
|
15
|
+
|
16
|
+
```rb
|
17
|
+
@config.evironment(:dev)[:option]
|
18
|
+
|
19
|
+
# => "value"
|
20
|
+
```
|
21
|
+
|
22
|
+
- Added return values for `#environment` method. Now you can get the current values of the environment by calling `#environment` without any arguments. Ex.
|
23
|
+
|
24
|
+
```rb
|
25
|
+
@config.environment(:dev)
|
26
|
+
|
27
|
+
# => { option1: "value1", option2: "value2" }
|
28
|
+
```
|
29
|
+
|
30
|
+
## Removed
|
31
|
+
|
32
|
+
- Remove `OptionBuilder` class. It was not needed.
|
33
|
+
- Remove `OptionFactory` class. It was not needed.
|
34
|
+
|
35
|
+
## [0.1.0] - 2023-12-05
|
36
|
+
|
37
|
+
### Added
|
38
|
+
|
39
|
+
- Initial release. Versioning will start at 0.1.0.
|
40
|
+
|
41
|
+
### Changed
|
42
|
+
|
43
|
+
### Removed
|
44
|
+
|
45
|
+
### Fixed
|
46
|
+
|
47
|
+
### Security
|
data/lib/pifano/config.rb
CHANGED
@@ -1,31 +1,99 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
# Released under the MIT License.
|
4
|
+
# Copyright, 2023, by Aristóteles Coutinho.
|
4
5
|
|
5
6
|
module Pifano
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
7
|
+
# This class is used to set the options for a specific environment.
|
8
|
+
#
|
9
|
+
# @public Since `stable-v1`.
|
10
|
+
#
|
11
|
+
class Config
|
12
|
+
# @!attribute [r] options
|
13
|
+
# @returns [Hash] The options.
|
14
|
+
attr_reader :options
|
15
|
+
|
16
|
+
# @!attribute [r] env
|
17
|
+
# @returns [Symbol] The environment.
|
18
|
+
attr_reader :env
|
19
|
+
|
20
|
+
# Initialize the configuration.
|
21
|
+
#
|
22
|
+
def initialize
|
23
|
+
@options = {}
|
24
|
+
@env = nil
|
25
|
+
end
|
26
|
+
|
27
|
+
# Set the options for a specific environment.
|
28
|
+
#
|
29
|
+
# @parameter env [Symbol] The environment to set the options.
|
30
|
+
# @yields {...} When a block is given, the block is yielded with the option.
|
31
|
+
#
|
32
|
+
def on(env)
|
33
|
+
raise ArgumentError, 'Block is required.' unless block_given?
|
34
|
+
|
35
|
+
@env = env
|
36
|
+
yield self
|
37
|
+
@env = nil
|
38
|
+
end
|
39
|
+
|
40
|
+
# Get the options for a specific environment.
|
41
|
+
#
|
42
|
+
# @parameter env_name [Symbol] The environment to get the options.
|
43
|
+
# @yields {...} When a block is given, the block is yielded with the option.
|
44
|
+
#
|
45
|
+
# @returns [Hash] The options.
|
46
|
+
#
|
47
|
+
def environment(env_name)
|
48
|
+
return {} unless @options.key?(env_name)
|
49
|
+
|
50
|
+
@env = env_name
|
51
|
+
yield self if block_given?
|
52
|
+
@env = nil
|
53
|
+
@options[env_name]
|
54
|
+
end
|
55
|
+
|
56
|
+
# Set the option for a specific environment.
|
57
|
+
#
|
58
|
+
# @parameter key [Symbol] The option name.
|
59
|
+
# @parameter value [String] The option value.
|
60
|
+
#
|
61
|
+
# @returns [Nil] The option value.
|
62
|
+
#
|
63
|
+
def []=(key, value)
|
64
|
+
@options[@env] ||= {}
|
65
|
+
@options[@env][key] = value
|
66
|
+
end
|
67
|
+
|
68
|
+
# Get the option for a specific environment.
|
69
|
+
|
70
|
+
# @parameter env [Symbol] The environment to get the option.
|
71
|
+
# @parameter key [Symbol] The option name.
|
72
|
+
#
|
73
|
+
# @returns [String] The option value.
|
74
|
+
#
|
75
|
+
def [](env, key) = @options[env][key]
|
76
|
+
end
|
77
|
+
|
78
|
+
# This module is used to include the `config` method in the class.
|
79
|
+
#
|
80
|
+
# @public Since `stable-v1`.
|
81
|
+
#
|
82
|
+
module Mixin
|
83
|
+
def self.included(klass)
|
84
|
+
klass.extend(ClassMethods)
|
85
|
+
end
|
86
|
+
|
87
|
+
# Extend the class methods.
|
88
|
+
#
|
89
|
+
module ClassMethods
|
90
|
+
# Get the configuration.
|
91
|
+
#
|
92
|
+
# @returns [Config] The configuration.
|
93
|
+
#
|
94
|
+
def config
|
95
|
+
@config ||= Config.new
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
31
99
|
end
|
data/lib/pifano_config.rb
CHANGED
data/license.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# MIT License
|
2
|
+
|
3
|
+
Copyright, 2023, by Aristóteles Coutinho.
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/readme.md
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# Pifano::Config
|
2
|
+
|
3
|
+
This is a simple configurable gem that allows you to easily create a configuration file for your gem or application.
|
4
|
+
|
5
|
+
<p align="center">
|
6
|
+
<img src="pifano_config.jpeg" width="400" height="400" />
|
7
|
+
</p>
|
8
|
+
|
9
|
+
# Usage
|
10
|
+
|
11
|
+
Please see the [project documentation](https://aristotelesbr.github.io/pifano_config/index.html) for more details.
|
12
|
+
|
13
|
+
- [Getting Started](https://aristotelesbr.github.io/pifano_config/guides/getting-started/index.html) - This guide show you how to use the `pífano config`.
|
14
|
+
|
15
|
+
## Contributing
|
16
|
+
|
17
|
+
We welcome contributions to this project.
|
18
|
+
|
19
|
+
1. Fork it.
|
20
|
+
2. Create your feature branch (`git checkout -b my-new-feature`).
|
21
|
+
3. Commit your changes (`git commit -am 'Add some feature'`).
|
22
|
+
4. Push to the branch (`git push origin my-new-feature`).
|
23
|
+
5. Create new Pull Request.
|
24
|
+
|
25
|
+
### Developer Certificate of Origin
|
26
|
+
|
27
|
+
This project uses the [Developer Certificate of Origin](https://developercertificate.org/). All contributors to this project must agree to this document to have their contributions accepted.
|
28
|
+
|
29
|
+
### Contributor Covenant
|
30
|
+
|
31
|
+
This project is governed by the [Contributor Covenant](https://www.contributor-covenant.org/). All contributors and participants agree to abide by its terms.
|
metadata
CHANGED
@@ -1,40 +1,36 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pifano_config
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aristóteles Coutinho
|
8
8
|
autorequire:
|
9
|
-
bindir:
|
9
|
+
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-01-22 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description:
|
14
|
-
Create simple configurations for your project by environment like playing a Pífano.
|
15
|
-
The Pífano is a traditional musical instrument of the Brazilian Northeast, made of wood and leather. It is a kind of flute, with a sharp and piercing sound. It is used in the forró, a typical dance of the region.
|
13
|
+
description:
|
16
14
|
email:
|
17
|
-
- aristotelesbr@gmail.com
|
18
15
|
executables: []
|
19
16
|
extensions: []
|
20
17
|
extra_rdoc_files: []
|
21
18
|
files:
|
22
|
-
-
|
23
|
-
- README.md
|
19
|
+
- changelog.md
|
24
20
|
- lib/pifano/config.rb
|
25
|
-
- lib/pifano/option_builder.rb
|
26
|
-
- lib/pifano/option_factory.rb
|
27
21
|
- lib/pifano_config.rb
|
28
22
|
- lib/pifano_config/version.rb
|
23
|
+
- license.md
|
24
|
+
- readme.md
|
29
25
|
homepage: https://rubygems.org/gems/pifano_config
|
30
26
|
licenses:
|
31
27
|
- MIT
|
32
28
|
metadata:
|
33
29
|
allowed_push_host: https://rubygems.org/gems/pifano_config
|
34
|
-
homepage_uri: https://rubygems.org/gems/pifano_config
|
35
|
-
source_code_uri: https://github.com/aristotelesbr/pifano_config
|
36
30
|
changelog_uri: https://github.com/aristotelesbr/pifano_config/blob/master/CHANGELOG.md
|
31
|
+
homepage_uri: https://rubygems.org/gems/pifano_config
|
37
32
|
rubygems_mfa_required: 'true'
|
33
|
+
source_code_uri: https://github.com/aristotelesbr/pifano_config
|
38
34
|
post_install_message:
|
39
35
|
rdoc_options: []
|
40
36
|
require_paths:
|
@@ -43,14 +39,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
43
39
|
requirements:
|
44
40
|
- - ">="
|
45
41
|
- !ruby/object:Gem::Version
|
46
|
-
version:
|
42
|
+
version: '3.0'
|
47
43
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
44
|
requirements:
|
49
45
|
- - ">="
|
50
46
|
- !ruby/object:Gem::Version
|
51
47
|
version: '0'
|
52
48
|
requirements: []
|
53
|
-
rubygems_version: 3.
|
49
|
+
rubygems_version: 3.5.3
|
54
50
|
signing_key:
|
55
51
|
specification_version: 4
|
56
52
|
summary: Simple configuration for your project by enviroment.
|
data/CHANGELOG.md
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
# Changelog
|
2
|
-
|
3
|
-
All notable changes to this project will be documented in this file.
|
4
|
-
|
5
|
-
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
6
|
-
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
|
-
|
8
|
-
## [Unreleased]
|
9
|
-
|
10
|
-
## [0.1.0] - 2023-12-05
|
11
|
-
|
12
|
-
### Added
|
13
|
-
|
14
|
-
- Initial release. Versioning will start at 0.1.0.
|
15
|
-
|
16
|
-
### Changed
|
17
|
-
|
18
|
-
### Removed
|
19
|
-
|
20
|
-
### Fixed
|
21
|
-
|
22
|
-
### Security
|
data/README.md
DELETED
@@ -1,92 +0,0 @@
|
|
1
|
-
# PifanoConfig
|
2
|
-
|
3
|
-
This is a simple configurable gem that allows you to easily create a configuration file for your gem or application.
|
4
|
-
|
5
|
-
## Installation
|
6
|
-
|
7
|
-
Add this line to your application's `Gemfile`:
|
8
|
-
|
9
|
-
gem 'pifano_config'
|
10
|
-
|
11
|
-
And then execute:
|
12
|
-
|
13
|
-
$ bundle
|
14
|
-
|
15
|
-
Or install it yourself as:
|
16
|
-
|
17
|
-
$ gem install pifano_config
|
18
|
-
|
19
|
-
## Usage
|
20
|
-
|
21
|
-
### Creating a configuration file
|
22
|
-
|
23
|
-
To create a configuration file, you need to create a class and include the `PifanoConfig::Mixin` module.
|
24
|
-
|
25
|
-
~~~ruby
|
26
|
-
|
27
|
-
class MyAppConfig
|
28
|
-
include Pifano::Mixin
|
29
|
-
|
30
|
-
# Define your configuration options here
|
31
|
-
config.on(:development) do |config|
|
32
|
-
# Define your configuration options for the development environment here
|
33
|
-
config[:my_option] = 'pifano dev'
|
34
|
-
|
35
|
-
# You can also define nested configurations
|
36
|
-
config[:database] = {
|
37
|
-
adapter: 'sqlite3',
|
38
|
-
database: 'db/development.sqlite3'
|
39
|
-
}
|
40
|
-
end
|
41
|
-
|
42
|
-
config.on(:production) do |config|
|
43
|
-
# Define your configuration options for the production environment here
|
44
|
-
config[:my_option] = 'pifano prod'
|
45
|
-
|
46
|
-
# You can also define nested configurations
|
47
|
-
config[:database] = {
|
48
|
-
adapter: 'postgresql',
|
49
|
-
host: 'localhost',
|
50
|
-
database: 'my_app_production',
|
51
|
-
username: 'my_app',
|
52
|
-
}
|
53
|
-
end
|
54
|
-
end
|
55
|
-
~~~
|
56
|
-
|
57
|
-
### Using a configuration file
|
58
|
-
|
59
|
-
After creating your configuration class, you can use it to access your configuration options.
|
60
|
-
|
61
|
-
~~~ruby
|
62
|
-
# app/models/my_model.rb
|
63
|
-
|
64
|
-
class MyModel
|
65
|
-
def self.adapter
|
66
|
-
MyAppConfig.config.environment(APP_ENV).database[:adapter]
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
MyModel.adapter
|
71
|
-
# => 'sqlite3' or 'postgresql' depends on the APP_ENV value
|
72
|
-
~~~
|
73
|
-
|
74
|
-
### Setting the environment
|
75
|
-
|
76
|
-
You can set the environment using the `APP_ENV` environment variable.
|
77
|
-
|
78
|
-
~~~bash
|
79
|
-
|
80
|
-
$ APP_ENV=development irb
|
81
|
-
|
82
|
-
irb(main):001:0> MyApp.config.environment(APP_ENV).my_option
|
83
|
-
=> 'pifano dev'
|
84
|
-
|
85
|
-
irb(main):002:0> MyApp.config.environment(APP_ENV).database[:adapter]
|
86
|
-
|
87
|
-
=> 'sqlite3'
|
88
|
-
~~~
|
89
|
-
|
90
|
-
## Contributing
|
91
|
-
|
92
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/aristotelesbr/pifano_config.
|
@@ -1,17 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'pifano/option_factory'
|
4
|
-
|
5
|
-
module Pifano
|
6
|
-
class OptionBuilder
|
7
|
-
attr_reader :config_hash
|
8
|
-
|
9
|
-
def initialize = @config_hash = {}
|
10
|
-
|
11
|
-
def []=(key, value)
|
12
|
-
@config_hash[key] = value
|
13
|
-
end
|
14
|
-
|
15
|
-
def build = OptionFactory.build(@config_hash)
|
16
|
-
end
|
17
|
-
end
|
@@ -1,38 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'thread'
|
4
|
-
|
5
|
-
module Pifano
|
6
|
-
module OptionFactory
|
7
|
-
@struct_cache = {}
|
8
|
-
@mutex = ::Mutex.new
|
9
|
-
|
10
|
-
def self.build(hash)
|
11
|
-
return unless hash.is_a?(::Hash)
|
12
|
-
|
13
|
-
struct_key = hash.keys.sort.join('_').to_sym
|
14
|
-
struct = get_or_create_struct(struct_key, hash.keys)
|
15
|
-
|
16
|
-
struct.new(hash)
|
17
|
-
end
|
18
|
-
|
19
|
-
def self.get_or_create_struct(struct_key, keys)
|
20
|
-
@mutex.synchronize do
|
21
|
-
@struct_cache[struct_key] ||= create_struct(keys)
|
22
|
-
end
|
23
|
-
end
|
24
|
-
private_class_method :get_or_create_struct
|
25
|
-
|
26
|
-
def self.create_struct(keys)
|
27
|
-
Struct.new(*keys, keyword_init: true) do
|
28
|
-
def initialize(hash)
|
29
|
-
hash.each do |k, v|
|
30
|
-
self[k] = v.is_a?(::Hash) ? OptionFactory.build(v) : v
|
31
|
-
end
|
32
|
-
freeze
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
private_class_method :create_struct
|
37
|
-
end
|
38
|
-
end
|