k-configurable 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +123 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/k-configurable.gemspec +34 -0
- data/lib/k/configurable.rb +79 -0
- data/lib/k/configurable/version.rb +7 -0
- metadata +98 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c902e0cd7fa3ae73408d2a3a24e05f6a3a714c5d
|
4
|
+
data.tar.gz: a669bac3b208038ed8776b2803e3b8bee05f73a5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 56ac5d2b2ef12792a70b0fb395fd4a4eff1677f750ec27c1901352a2698497e583d67458e0eb867151c426819a46ffe31c7738e101ede09fd136e5b2fc9511f2
|
7
|
+
data.tar.gz: a13c1ed2866e56fe31ae39b270b1a573afefe8377e61f0f1ba3697734f1de4715c26237ef5f8b86ff22995587a68253df1c6ad14a172963e203624655b2983a2
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Kai Kuchenbecker
|
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
|
13
|
+
all 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
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
# K-Configurable
|
2
|
+
|
3
|
+
Make your classes configurable with this nifty little gem. Instead of
|
4
|
+
implementing this over and over again, or copying it over and over again into
|
5
|
+
every project where I need this, I decided to extract this tiny gem.
|
6
|
+
Feel free to use it.
|
7
|
+
|
8
|
+
There are other gems that do something similiar with a different syntax, e.g.:
|
9
|
+
|
10
|
+
1. [dry-configurable](https://github.com/dry-rb/dry-configurable)
|
11
|
+
2. [Configurable](https://github.com/thinkerbot/configurable)
|
12
|
+
3. [simply_configurable](https://github.com/daws/simply_configurable)
|
13
|
+
4. [ruby-configurable](https://bitbucket.org/krbullock/configurable)
|
14
|
+
5. [block_configurable](https://github.com/artemshitov/block_configurable)
|
15
|
+
|
16
|
+
Without have looked too much at those, dry-configurable is probably the best
|
17
|
+
choice since the guys over at dry have quite a few mini gems they maintain and
|
18
|
+
use. Also, there are many, many more than those up there.
|
19
|
+
|
20
|
+
## Installation
|
21
|
+
|
22
|
+
Add this line to your application's Gemfile:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
gem 'k-configurable'
|
26
|
+
```
|
27
|
+
|
28
|
+
And then execute:
|
29
|
+
|
30
|
+
$ bundle
|
31
|
+
|
32
|
+
Or install it yourself as:
|
33
|
+
|
34
|
+
$ gem install k-configurable
|
35
|
+
|
36
|
+
## Usage
|
37
|
+
|
38
|
+
Include the `K::Configurable` module in the class definition body of the class
|
39
|
+
you would like to make configurable. For example, maybe you need to wrap a
|
40
|
+
database connection with an adapter:
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
class DatabaseAdapter
|
44
|
+
# make host, port, user and password configurable
|
45
|
+
include K::Configurable[:host, :port, :user, :password]
|
46
|
+
|
47
|
+
# ...
|
48
|
+
|
49
|
+
# use configurable attributes to open a connection
|
50
|
+
# which may look something like this:
|
51
|
+
def connection
|
52
|
+
config = self.class.configuration
|
53
|
+
Connection.open(
|
54
|
+
"#{config.user}:#{config.password}@#{config.host}:#{config.port}"
|
55
|
+
)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# configure it
|
60
|
+
DatabaseAdapter.configure do |config|
|
61
|
+
config.host = 'localhost'
|
62
|
+
config.port = '12345'
|
63
|
+
config.user = 'foo'
|
64
|
+
config.password = File.read('/run/secrets/dbpassword')
|
65
|
+
end
|
66
|
+
```
|
67
|
+
|
68
|
+
The configuration attributes are accessible via the `configuration` class method
|
69
|
+
since I don't want them to pollute the class interface. If you however find it
|
70
|
+
tedious to always have to refer to `configuration` and don't mind having those
|
71
|
+
attribute setter and getter methods on your classes interface, then simply add
|
72
|
+
some forwardables like this:
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
class DatabaseAdapter
|
76
|
+
include K::Configurable[:host, :port, :user, :password]
|
77
|
+
extend SingleForwardable
|
78
|
+
def_single_delegators :configuration, :host, :port, :user, :password
|
79
|
+
end
|
80
|
+
```
|
81
|
+
|
82
|
+
Be careful about name collisions though. E.g., `name` would be a very bad
|
83
|
+
attribute to delegate.
|
84
|
+
|
85
|
+
### Configurable with Defaults
|
86
|
+
|
87
|
+
You can specify defaults like this:
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
class DatabaseAdapter
|
91
|
+
include K::Configurable[:user, :password, host: 'localhost', port: 12345]
|
92
|
+
end
|
93
|
+
```
|
94
|
+
|
95
|
+
Defaults are implemented using named parameters, therefore you have to mind all
|
96
|
+
the same rules you do when invoking any method in Ruby. I.e., you cannot mix
|
97
|
+
named parameters and normal parameters, they have to be strictly ordered, normal
|
98
|
+
parameters first, then named parameters.
|
99
|
+
|
100
|
+
### Define methods on configuration
|
101
|
+
|
102
|
+
**Do NOT use**
|
103
|
+
|
104
|
+
It is possible to define methods during configuration definition, however, I do
|
105
|
+
not like the way it is implemented and really have used it only in one instance
|
106
|
+
yet. So use at own risk it may change without further notice. If you absolutely
|
107
|
+
want to use it, it is at your own risk.. See the tests on how it works.
|
108
|
+
|
109
|
+
## Development
|
110
|
+
|
111
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
112
|
+
|
113
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
114
|
+
|
115
|
+
## Contributing
|
116
|
+
|
117
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/kaikuchn/k-configurable.
|
118
|
+
|
119
|
+
|
120
|
+
## License
|
121
|
+
|
122
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
123
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "configurable"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'k/configurable/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'k-configurable'
|
9
|
+
spec.version = K::Configurable::VERSION
|
10
|
+
spec.authors = ['Kai Kuchenbecker']
|
11
|
+
spec.email = ['kuchenbecker.k@gmail.com']
|
12
|
+
|
13
|
+
spec.summary = %q{Make your classes configurable.}
|
14
|
+
spec.homepage = 'https://github.com/kaikuchn/configurable.git'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
if spec.respond_to?(:metadata)
|
18
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
19
|
+
else
|
20
|
+
raise 'RubyGems 2.0 or newer is required to protect against ' \
|
21
|
+
'public gem pushes.'
|
22
|
+
end
|
23
|
+
|
24
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
25
|
+
f.match(%r{^(test|spec|features)/})
|
26
|
+
end
|
27
|
+
spec.bindir = 'exe'
|
28
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
29
|
+
spec.require_paths = ['lib']
|
30
|
+
|
31
|
+
spec.add_development_dependency 'bundler', '~> 1.14'
|
32
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
33
|
+
spec.add_development_dependency 'minitest', '~> 5.0'
|
34
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'k/configurable/version'
|
4
|
+
|
5
|
+
module K
|
6
|
+
module Configurable
|
7
|
+
def self.included(_base)
|
8
|
+
raise 'Cannot include Configurable, include Configurable::[] instead'
|
9
|
+
end
|
10
|
+
|
11
|
+
# Use `include Configurable [:foo, :bar]` to make your class/module
|
12
|
+
# configurable with configuration options :foo and :bar. If you want your
|
13
|
+
# configuration object to be able to respond to some custom methods you can
|
14
|
+
# extend it by passing a block. In that case you have to call `[]` like this
|
15
|
+
# include(Configurable.[](:foo) do
|
16
|
+
# def bazinga
|
17
|
+
# ...
|
18
|
+
# end
|
19
|
+
# end)
|
20
|
+
# Because MRI cannot parse `Configurable[...] {}`. Also keep in mind that
|
21
|
+
# the do-end block would get bound to `include` without the parens, you
|
22
|
+
# could also use {}-block instead, which binds tighter.
|
23
|
+
def self.[](*attributes, **defaults, &block)
|
24
|
+
Module.new do
|
25
|
+
@__attributes = (attributes + defaults.keys).uniq
|
26
|
+
@__defaults = defaults
|
27
|
+
@__block = block || -> {}
|
28
|
+
|
29
|
+
def self.included(base)
|
30
|
+
base.instance_variable_set(:@__configurable_attributes, @__attributes)
|
31
|
+
base.instance_variable_set(:@__configurable_defaults, @__defaults)
|
32
|
+
base.instance_variable_set(:@__configurable_block, @__block)
|
33
|
+
base.extend(ClassMethods)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
module ClassMethods
|
39
|
+
def configuration
|
40
|
+
@configuration ||= anonymous_configuration_class.new(
|
41
|
+
*__configurable_defaults
|
42
|
+
)
|
43
|
+
end
|
44
|
+
|
45
|
+
def configure
|
46
|
+
yield(configuration)
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def anonymous_configuration_class
|
52
|
+
block = __configurable_block
|
53
|
+
Struct.new(*__configurable_attributes) do |klass|
|
54
|
+
klass.class_exec(&block)
|
55
|
+
|
56
|
+
def to_params
|
57
|
+
Hash[
|
58
|
+
members
|
59
|
+
.map { |attr| [attr, public_send(attr)] }
|
60
|
+
.reject { |_, value| value.nil? }
|
61
|
+
]
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def __configurable_attributes
|
67
|
+
@__configurable_attributes
|
68
|
+
end
|
69
|
+
|
70
|
+
def __configurable_defaults
|
71
|
+
@__configurable_defaults.values_at(*__configurable_attributes)
|
72
|
+
end
|
73
|
+
|
74
|
+
def __configurable_block
|
75
|
+
@__configurable_block
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: k-configurable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kai Kuchenbecker
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-05-31 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.14'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.14'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- kuchenbecker.k@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".travis.yml"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- bin/console
|
69
|
+
- bin/setup
|
70
|
+
- k-configurable.gemspec
|
71
|
+
- lib/k/configurable.rb
|
72
|
+
- lib/k/configurable/version.rb
|
73
|
+
homepage: https://github.com/kaikuchn/configurable.git
|
74
|
+
licenses:
|
75
|
+
- MIT
|
76
|
+
metadata:
|
77
|
+
allowed_push_host: https://rubygems.org
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 2.6.11
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: Make your classes configurable.
|
98
|
+
test_files: []
|