pifano_config 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 53b166a47b78898a520e8e9b6a146c3b1b748b650099dd4c4412308cdb87a8a5
4
+ data.tar.gz: f3b9aa3ee6a13a52fdbaa3f3944b005e5a663bbb57f953d3ff49b5f361bbfb3a
5
+ SHA512:
6
+ metadata.gz: d32163c5e1339d4892e2b8e449e68ba87040b5cafe165931696cf658cb5f38823fb7b85e13a09040a9ad333e393487913ed9d69a57afbe6eb7da48e434531125
7
+ data.tar.gz: f7d5bc1bbdf572745c2a8a074a67e529e3aa2661b38b6ccac43dfb3016a9a243ba6b7adb53757b44516ff36bfc9a49f228db6bcb0ad395a79ece32ac7ee6cd8c
data/CHANGELOG.md ADDED
@@ -0,0 +1,22 @@
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 ADDED
@@ -0,0 +1,92 @@
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.
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pifano/option_builder'
4
+
5
+ module Pifano
6
+ class Config
7
+ def initialize(builder: OptionBuilder.new)
8
+ @builder = builder
9
+ @options = {}
10
+ end
11
+
12
+ def on(env)
13
+ yield @builder if block_given?
14
+ @options[env] = @builder.build
15
+ end
16
+
17
+ def environment(env) = @options[env]
18
+ end
19
+
20
+ module Mixin
21
+ def self.included(klass)
22
+ klass.extend(ClassMethods)
23
+ end
24
+
25
+ module ClassMethods
26
+ def config
27
+ @config ||= Config.new
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,17 @@
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
@@ -0,0 +1,38 @@
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
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PifanoConfig
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'pifano_config/version'
4
+
5
+ require 'pifano/config'
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pifano_config
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Aristóteles Coutinho
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-12-10 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |2
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.
16
+ email:
17
+ - aristotelesbr@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - CHANGELOG.md
23
+ - README.md
24
+ - lib/pifano/config.rb
25
+ - lib/pifano/option_builder.rb
26
+ - lib/pifano/option_factory.rb
27
+ - lib/pifano_config.rb
28
+ - lib/pifano_config/version.rb
29
+ homepage: https://rubygems.org/gems/pifano_config
30
+ licenses:
31
+ - MIT
32
+ metadata:
33
+ 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
+ changelog_uri: https://github.com/aristotelesbr/pifano_config/blob/master/CHANGELOG.md
37
+ rubygems_mfa_required: 'true'
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 2.6.0
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubygems_version: 3.4.22
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: Simple configuration for your project by enviroment.
57
+ test_files: []