much-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: b57448869dc7209d8ddd1469c8ba8a24fab607d9a43f03f851e4ae4acdc633da
4
+ data.tar.gz: b3246844ed8b0002c3b184e5fb1a8fa080e8837cd02cfeda41a522892cbc8378
5
+ SHA512:
6
+ metadata.gz: 39f371597e408694f420b6a84dcb04f2a21e48bf83f970e3f418a851926f37263dff8de8e37472971cfc52fb1d8e34bbc685ce43c2e6e115b27d78c6babea62c
7
+ data.tar.gz: b7803e2b769fd7fcbb5a1ceab9f102efedd34265b19e26fdd7f08890a0fd3221e8ff865a3d5aef50c6b85f716854a190eb8e25f44aee177b41e86b039729ebd6
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ ruby "~> 2.5"
6
+
7
+ gemspec
8
+
9
+ gem "pry"
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2021-Present Kelly Redding and Collin Redding
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,114 @@
1
+ # MuchConfig
2
+
3
+ Configuration for Ruby objects.
4
+
5
+ ## Usage
6
+
7
+ Mixin MuchConfig in your e.g. `MyClass` Ruby object. Call the `add_config` DSL method and define `MyClass::Config`:
8
+
9
+ ```ruby
10
+ module MyClass
11
+ include MuchConfig
12
+
13
+ add_config
14
+
15
+ def self.value1
16
+ config.value1
17
+ end
18
+
19
+ class Config
20
+ attr_accessor :value1
21
+
22
+ def initialize
23
+ @value1 = "default value for `value1`"
24
+ end
25
+ end
26
+ end
27
+ ```
28
+
29
+ Configure MyClass using the MuchConfig API:
30
+
31
+ ```ruby
32
+ MyClass.config.value1 = "a custom value"
33
+
34
+ # OR
35
+
36
+ MyClass.configure do |config|
37
+ config.value1 = "a custom value"
38
+ end
39
+ ```
40
+
41
+ ### Define multiple named configs
42
+
43
+ You can define multiple named configs, e.g.:
44
+
45
+ ```ruby
46
+ module MyClass
47
+ include MuchConfig
48
+
49
+ add_config :values
50
+ add_config :settings
51
+
52
+ def self.value1
53
+ values_config.value1
54
+ end
55
+
56
+ def self.setting1
57
+ settings_config.setting1
58
+ end
59
+
60
+ class ValuesConfig
61
+ attr_accessor :value1
62
+
63
+ def initialize
64
+ @value1 = "default value for `value1`"
65
+ end
66
+ end
67
+
68
+ class SettingsConfig
69
+ attr_accessor :setting1
70
+
71
+ def initialize
72
+ @setting1 = "default value for `setting1`"
73
+ end
74
+ end
75
+ end
76
+ ```
77
+
78
+ Configure using the MuchConfig API:
79
+
80
+ ```ruby
81
+ MyClass.values_config.value1 = "a custom value"
82
+ MyClass.settings_config.setting1 = "a custom value"
83
+
84
+ # OR
85
+
86
+ MyClass.configure_values do |config|
87
+ config.value1 = "a custom value"
88
+ end
89
+ MyClass.configure_settings do |config|
90
+ config.setting1 = "a custom value"
91
+ end
92
+ ```
93
+
94
+ ## Installation
95
+
96
+ Add this line to your application's Gemfile:
97
+
98
+ gem "much-config"
99
+
100
+ And then execute:
101
+
102
+ $ bundle
103
+
104
+ Or install it yourself as:
105
+
106
+ $ gem install much-config
107
+
108
+ ## Contributing
109
+
110
+ 1. Fork it
111
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
112
+ 3. Commit your changes (`git commit -am "Added some feature"`)
113
+ 4. Push to the branch (`git push origin my-new-feature`)
114
+ 5. Create new Pull Request
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "much-config/version"
4
+ require "much-mixin"
5
+
6
+ module MuchConfig
7
+ include MuchMixin
8
+
9
+ def self.classify(term)
10
+ # prefer to ActiveSupport if present
11
+ return term.classify if term.respond_to?(:classify)
12
+
13
+ value = term.sub(/^[a-z\d]*/, &:capitalize)
14
+ value.gsub!(%r{(?:_|(/))([a-z\d]*)}i) do
15
+ "#{Regexp.last_match(1)}#{Regexp.last_match(2).capitalize}"
16
+ end
17
+ value.gsub!("/", "::")
18
+ value
19
+ end
20
+
21
+ def self.underscore(cased_term)
22
+ # prefer to ActiveSupport if present
23
+ return cased_term.underscore if cased_term.respond_to?(:underscore)
24
+
25
+ return cased_term unless /[A-Z-]|::/.match?(cased_term)
26
+
27
+ term = cased_term.to_s.gsub("::", "/")
28
+ term.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
29
+ term.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
30
+ term.tr!("-", "_")
31
+ term.downcase!
32
+ term
33
+ end
34
+
35
+ mixin_class_methods do
36
+ def add_config(name = nil, method_name: nil)
37
+ config_method_name, config_class_name, configure_method_name =
38
+ much_config_names(name, method_name)
39
+
40
+ instance_eval(<<~RUBY, __FILE__, __LINE__ + 1)
41
+ def #{config_method_name}
42
+ @#{config_method_name} ||= self::#{config_class_name}.new
43
+ end
44
+
45
+ def #{configure_method_name}
46
+ yield(#{config_method_name}) if block_given?
47
+ end
48
+ RUBY
49
+ end
50
+
51
+ def add_instance_config(name = nil, method_name: nil)
52
+ config_method_name, config_class_name, configure_method_name =
53
+ much_config_names(name, method_name)
54
+
55
+ instance_eval(<<~RUBY, __FILE__, __LINE__ + 1)
56
+ define_method(:#{config_method_name}) do
57
+ @#{config_method_name} ||= self.class::#{config_class_name}.new
58
+ end
59
+
60
+ define_method(:#{configure_method_name}) do |&block|
61
+ block.call(#{config_method_name}) if block
62
+ end
63
+ RUBY
64
+ end
65
+
66
+ private
67
+
68
+ def much_config_names(name, method_name)
69
+ name_prefix = name.nil? ? "" : "#{MuchConfig.underscore(name.to_s)}_"
70
+ config_method_name = (method_name || "#{name_prefix}config").to_s
71
+ config_class_name = "#{MuchConfig.classify(name_prefix)}Config"
72
+
73
+ name_suffix = name.nil? ? "" : "_#{MuchConfig.underscore(name.to_s)}"
74
+ configure_method_name = "configure#{name_suffix}"
75
+
76
+ [config_method_name, config_class_name, configure_method_name]
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MuchConfig
4
+ VERSION = "0.1.0"
5
+ end
data/log/.keep ADDED
File without changes
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # frozen_string_literal: true
3
+
4
+ lib = File.expand_path("../lib", __FILE__)
5
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
+ require "much-config/version"
7
+
8
+ Gem::Specification.new do |gem|
9
+ gem.name = "much-config"
10
+ gem.version = MuchConfig::VERSION
11
+ gem.authors = ["Kelly Redding", "Collin Redding"]
12
+ gem.email = ["kelly@kellyredding.com", "collin.redding@me.com"]
13
+ gem.summary = "Configuration for Ruby objects."
14
+ gem.description = "Configuration for Ruby objects."
15
+ gem.homepage = "https://github.com/redding/much-config"
16
+ gem.license = "MIT"
17
+
18
+ gem.files = `git ls-files | grep "^[^.]"`.split($INPUT_RECORD_SEPARATOR)
19
+
20
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
21
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
22
+ gem.require_paths = ["lib"]
23
+
24
+ gem.required_ruby_version = "~> 2.5"
25
+
26
+ gem.add_development_dependency("much-style-guide", ["~> 0.6.4"])
27
+ gem.add_development_dependency("assert", ["~> 2.19.6"])
28
+
29
+ gem.add_dependency("much-mixin", ["~> 0.2.4"])
30
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file is automatically required when you run `assert`; put any test
4
+ # helpers here.
5
+
6
+ # Add the root dir to the load path.
7
+ $LOAD_PATH.unshift(File.expand_path("../..", __FILE__))
8
+
9
+ # Require pry for debugging (`binding.pry`).
10
+ require "pry"
11
+
12
+ require "test/support/factory"
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "assert/factory"
4
+
5
+ module Factory
6
+ extend Assert::Factory
7
+ end
data/test/system/.keep ADDED
File without changes
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "assert"
4
+ require "much-config"
5
+
6
+ require "much-mixin"
7
+
8
+ module MuchConfig
9
+ class UnitTests < Assert::Context
10
+ desc "MuchConfig"
11
+ subject{ unit_class }
12
+
13
+ let(:unit_class){ MuchConfig }
14
+
15
+ should "be configured as expected" do
16
+ assert_that(subject).includes(MuchMixin)
17
+ end
18
+ end
19
+
20
+ class ReceiverTests < UnitTests
21
+ desc "receiver"
22
+ subject{ receiver_class }
23
+
24
+ setup do
25
+ class receiver_class::Config
26
+ attr_accessor :value
27
+ end
28
+
29
+ class receiver_class::AnotherConfig
30
+ include MuchConfig
31
+
32
+ add_instance_config :sub, method_name: :sub
33
+
34
+ attr_accessor :another_value
35
+
36
+ class SubConfig
37
+ attr_accessor :sub_value
38
+ end
39
+ end
40
+ end
41
+
42
+ let(:receiver_class) do
43
+ Class.new do
44
+ include MuchConfig
45
+
46
+ add_config
47
+ add_config :another
48
+ end
49
+ end
50
+
51
+ should have_imeths :config, :another_config
52
+
53
+ should "know its attributes" do
54
+ assert_that(subject.config).is_instance_of(subject::Config)
55
+ subject.configure{ |config| config.value = "VALUE 1" }
56
+ assert_that(subject.config.value).equals("VALUE 1")
57
+
58
+ assert_that(subject.another_config).is_instance_of(subject::AnotherConfig)
59
+ subject.configure_another{ |config| config.another_value = "VALUE 2" }
60
+ assert_that(subject.another_config.another_value).equals("VALUE 2")
61
+
62
+ assert_that(subject.another_config.sub)
63
+ .is_instance_of(subject::AnotherConfig::SubConfig)
64
+ subject.another_config.configure_sub do |sub|
65
+ sub.sub_value = "VALUE 3"
66
+ end
67
+ assert_that(subject.another_config.sub.sub_value).equals("VALUE 3")
68
+ end
69
+ end
70
+ end
data/tmp/.keep ADDED
File without changes
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: much-config
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kelly Redding
8
+ - Collin Redding
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2021-06-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: much-style-guide
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: 0.6.4
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: 0.6.4
28
+ - !ruby/object:Gem::Dependency
29
+ name: assert
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: 2.19.6
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: 2.19.6
42
+ - !ruby/object:Gem::Dependency
43
+ name: much-mixin
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: 0.2.4
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: 0.2.4
56
+ description: Configuration for Ruby objects.
57
+ email:
58
+ - kelly@kellyredding.com
59
+ - collin.redding@me.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - Gemfile
65
+ - LICENSE
66
+ - README.md
67
+ - lib/much-config.rb
68
+ - lib/much-config/version.rb
69
+ - log/.keep
70
+ - much-config.gemspec
71
+ - test/helper.rb
72
+ - test/support/factory.rb
73
+ - test/system/.keep
74
+ - test/unit/much-config_tests.rb
75
+ - tmp/.keep
76
+ homepage: https://github.com/redding/much-config
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '2.5'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubygems_version: 3.1.6
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Configuration for Ruby objects.
99
+ test_files:
100
+ - test/helper.rb
101
+ - test/support/factory.rb
102
+ - test/system/.keep
103
+ - test/unit/much-config_tests.rb