dry-configurable 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 39217854850d56cee715e92520f5861a2523bf80
4
+ data.tar.gz: 5ddd51d321b5b85646d4b85c74e8bdbd446c2bab
5
+ SHA512:
6
+ metadata.gz: 4e550fbe606b298cdb69cca90629cf83feb5aaada297f617803b6fa255a430f47be9562044f8e522aea8c9c0ec55f09857c03804e8209f57e3dbd625fdcd3698
7
+ data.tar.gz: ec8339404b10d09b36fb1e06d46087590df1fda88e2a9758082ac50eb2072abf3f63e0a0988390b4d9e9bdc58d54322b3657da8cc31b359f41b855cbddcf45fc
@@ -0,0 +1,8 @@
1
+ .DS_Store
2
+ coverage
3
+ /.bundle
4
+ vendor/bundle
5
+ bin/
6
+ tmp/
7
+ .idea/
8
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --require spec_helper
3
+ --order random
@@ -0,0 +1,12 @@
1
+ # Generated by `rubocop --auto-gen-config`
2
+ inherit_from: .rubocop_todo.yml
3
+
4
+ Metrics/LineLength:
5
+ Max: 120
6
+
7
+ Style/Documentation:
8
+ Enabled: false
9
+
10
+ Lint/HandleExceptions:
11
+ Exclude:
12
+ - rakelib/*.rake
@@ -0,0 +1,6 @@
1
+ # This configuration was generated by `rubocop --auto-gen-config`
2
+ # on 2015-06-10 18:54:14 +0100 using RuboCop version 0.26.0.
3
+ # The point is for the user to remove these configuration records
4
+ # one by one as the offenses are removed from the code base.
5
+ # Note that changes in the inspected code, or installation of new
6
+ # versions of RuboCop, may require this file to be generated again.
@@ -0,0 +1,22 @@
1
+ language: ruby
2
+ sudo: false
3
+ cache: bundler
4
+ bundler_args: --without console
5
+ script:
6
+ - bundle exec rspec
7
+ - bundle exec rubocop
8
+ rvm:
9
+ - 2.0
10
+ - 2.1
11
+ - 2.2
12
+ - rbx-2
13
+ - jruby
14
+ - ruby-head
15
+ - jruby-head
16
+ env:
17
+ global:
18
+ - JRUBY_OPTS='--dev -J-Xmx1024M'
19
+ matrix:
20
+ allow_failures:
21
+ - rvm: ruby-head
22
+ - rvm: jruby-head
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :tools do
6
+ gem 'rubocop'
7
+ gem 'guard'
8
+ gem 'guard-rspec'
9
+ gem 'guard-rubocop'
10
+ end
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Ruby Object Mapper
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ 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, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+ s
@@ -0,0 +1,33 @@
1
+ # dry-configurable <a href="https://gitter.im/dryrb/chat" target="_blank">![Join the chat at https://gitter.im/dryrb/chat](https://badges.gitter.im/Join%20Chat.svg)</a>
2
+
3
+ <a href="https://rubygems.org/gems/dry-configurable" target="_blank">![Gem Version](https://badge.fury.io/rb/dry-configurable.svg)</a>
4
+ <a href="https://travis-ci.org/dryrb/dry-configurable" target="_blank">![Build Status](https://travis-ci.org/dryrb/dry-configurable.svg?branch=master)</a>
5
+ <a href="http://inch-ci.org/github/dryrb/dry-configurable" target="_blank">![Inline docs](http://inch-ci.org/github/dryrb/dry-configurable.svg?branch=master&style=flat)</a>
6
+
7
+ ## Synopsis
8
+
9
+ ```ruby
10
+ class App
11
+ extend Dry::Configurable
12
+
13
+ # Pass a block for nested configuration (works to any depth)
14
+ setting :database do
15
+ # Can pass a default value
16
+ setting :dsn, 'sqlite:memory'
17
+ end
18
+ # Defaults to nil if no default value is given
19
+ setting :adapter
20
+ end
21
+
22
+ App.configure do |config|
23
+ config.database.dsn = 'jdbc:sqlite:memory'
24
+ end
25
+
26
+ App.config.database.dsn
27
+ # => 'jdbc:sqlite:memory'
28
+ App.config.adapter # => nil
29
+ ```
30
+
31
+ ## License
32
+
33
+ See `LICENSE` file.
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler/gem_tasks'
3
+
4
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'lib'))
5
+
6
+ require 'rspec/core'
7
+ require 'rspec/core/rake_task'
8
+
9
+ task default: :spec
10
+
11
+ desc 'Run all specs in spec directory'
12
+ RSpec::Core::RakeTask.new(:spec)
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ require File.expand_path('../lib/dry/configurable/version', __FILE__)
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = 'dry-configurable'
6
+ spec.version = Dry::Configurable::VERSION
7
+ spec.authors = ['Andy Holland']
8
+ spec.email = ['andyholland1991@aol.com']
9
+ spec.summary = 'A mixin to add configuration functionality to your classes'
10
+ spec.homepage = 'https://github.com/dryrb/dry-configurable'
11
+ spec.license = 'MIT'
12
+
13
+ spec.files = `git ls-files -z`.split("\x0")
14
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
15
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
16
+ spec.require_paths = ['lib']
17
+
18
+ spec.add_runtime_dependency 'thread_safe'
19
+
20
+ spec.add_development_dependency 'bundler'
21
+ spec.add_development_dependency 'rake'
22
+ spec.add_development_dependency 'rspec'
23
+ end
@@ -0,0 +1,86 @@
1
+ require 'thread_safe'
2
+ require 'dry/configurable/config'
3
+ require 'dry/configurable/version'
4
+
5
+ module Dry
6
+ # A simple configuration mixin
7
+ #
8
+ # @example
9
+ #
10
+ # class App
11
+ # extend Dry::Configurable
12
+ #
13
+ # setting :database do
14
+ # setting :dsn, 'sqlite:memory'
15
+ # end
16
+ # end
17
+ #
18
+ # App.confiure do |config|
19
+ # config.database.dsn = 'jdbc:sqlite:memory'
20
+ # end
21
+ #
22
+ # App.config.database.dsn
23
+ # # => "jdbc:sqlite:memory'"
24
+ #
25
+ # @api public
26
+ module Configurable
27
+ # @private
28
+ def self.extended(base)
29
+ base.instance_variable_set(:@_config_mutex, Mutex.new)
30
+ base.instance_variable_set(:@_settings_mutex, Mutex.new)
31
+ end
32
+ # Return configuration
33
+ #
34
+ # @return [Dry::Configurable::Config]
35
+ #
36
+ # @api public
37
+ def config
38
+ @_config_mutex.synchronize do
39
+ return @_config if defined?(@_config)
40
+ @_config = Config.new(*_settings.keys).new(*_settings.values) unless _settings.empty?
41
+ end
42
+ end
43
+ # Return configuration
44
+ #
45
+ # @yield [Dry::Configuration::Config]
46
+ #
47
+ # @return [Dry::Configurable::Config]
48
+ #
49
+ # @api public
50
+ def configure
51
+ yield(config) if block_given?
52
+ end
53
+ # Add a setting to the configuration
54
+ #
55
+ # @param [Mixed] key
56
+ # The accessor key for the configuration value
57
+ # @param [Mixed] default
58
+ # The default config value
59
+ #
60
+ # @yield
61
+ # If a block is given, it will be evaluated in the context of
62
+ # and new configuration class, and bound as the default value
63
+ #
64
+ # @return [Dry::Configurable::Config]
65
+ #
66
+ # @api public
67
+ def setting(key, default = nil, &block)
68
+ default = _config_for(&block) if block_given?
69
+ _settings[key] = default
70
+ end
71
+
72
+ private
73
+
74
+ # @private
75
+ def _settings
76
+ @_settings_mutex.synchronize { @_settings ||= ThreadSafe::Cache.new }
77
+ end
78
+
79
+ # @private
80
+ def _config_for(&block)
81
+ config_klass = Class.new { extend Dry::Configurable }
82
+ config_klass.instance_eval(&block)
83
+ config_klass.config
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,7 @@
1
+ module Dry
2
+ module Configurable
3
+ # @private
4
+ class Config < Struct
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,6 @@
1
+ module Dry
2
+ module Configurable
3
+ # @api public
4
+ VERSION = '0.1.0'.freeze
5
+ end
6
+ end
@@ -0,0 +1,18 @@
1
+ begin
2
+ require 'rubocop/rake_task'
3
+
4
+ Rake::Task[:default].enhance [:rubocop]
5
+
6
+ RuboCop::RakeTask.new do |task|
7
+ task.options << '--display-cop-names'
8
+ end
9
+
10
+ namespace :rubocop do
11
+ desc 'Generate a configuration file acting as a TODO list.'
12
+ task :auto_gen_config do
13
+ exec 'bundle exec rubocop --auto-gen-config'
14
+ end
15
+ end
16
+
17
+ rescue LoadError
18
+ end
@@ -0,0 +1,91 @@
1
+ RSpec.describe Dry::Configurable do
2
+ describe 'settings' do
3
+ context 'without default value' do
4
+ let(:configuration) do
5
+ Class.new do
6
+ extend Dry::Configurable
7
+
8
+ setting :dsn
9
+ end
10
+ end
11
+
12
+ it 'returns nil' do
13
+ expect(configuration.config.dsn).to be(nil)
14
+ end
15
+ end
16
+
17
+ context 'with default value' do
18
+ let(:configuration) do
19
+ Class.new do
20
+ extend Dry::Configurable
21
+
22
+ setting :dsn, 'sqlite:memory'
23
+ end
24
+ end
25
+
26
+ it 'returns the default value' do
27
+ expect(configuration.config.dsn).to eq('sqlite:memory')
28
+ end
29
+ end
30
+
31
+ context 'nested configuration' do
32
+ let(:configuration) do
33
+ Class.new do
34
+ extend Dry::Configurable
35
+
36
+ setting :database do
37
+ setting :dsn, 'sqlite:memory'
38
+ end
39
+ end
40
+ end
41
+
42
+ it 'returns the default value' do
43
+ expect(configuration.config.database.dsn).to eq('sqlite:memory')
44
+ end
45
+ end
46
+ end
47
+
48
+ describe 'configuration' do
49
+ context 'without nesting' do
50
+ let(:configuration) do
51
+ Class.new do
52
+ extend Dry::Configurable
53
+
54
+ setting :dsn, 'sqlite:memory'
55
+ end
56
+ end
57
+
58
+ before do
59
+ configuration.configure do |config|
60
+ config.dsn = 'jdbc:sqlite:memory'
61
+ end
62
+ end
63
+
64
+ it 'updates the config value' do
65
+ expect(configuration.config.dsn).to eq('jdbc:sqlite:memory')
66
+ end
67
+ end
68
+
69
+ context 'with nesting' do
70
+ let(:configuration) do
71
+ Class.new do
72
+ extend Dry::Configurable
73
+
74
+ setting :database do
75
+ setting :dsn, 'sqlite:memory'
76
+ end
77
+ end
78
+ end
79
+
80
+ before do
81
+ configuration.configure do |config|
82
+ config.database.dsn = 'jdbc:sqlite:memory'
83
+ end
84
+ end
85
+
86
+ it 'updates the config value' do
87
+ expect(configuration.config.database.dsn).to eq('jdbc:sqlite:memory')
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,3 @@
1
+ # encoding: utf-8
2
+
3
+ require 'dry/configurable'
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dry-configurable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Andy Holland
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thread_safe
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - andyholland1991@aol.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .rspec
78
+ - .rubocop.yml
79
+ - .rubocop_todo.yml
80
+ - .travis.yml
81
+ - Gemfile
82
+ - LICENSE
83
+ - README.md
84
+ - Rakefile
85
+ - dry-configurable.gemspec
86
+ - lib/dry/configurable.rb
87
+ - lib/dry/configurable/config.rb
88
+ - lib/dry/configurable/version.rb
89
+ - rakelib/rubocop.rake
90
+ - spec/integration/configurable_spec.rb
91
+ - spec/spec_helper.rb
92
+ homepage: https://github.com/dryrb/dry-configurable
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.4.6
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: A mixin to add configuration functionality to your classes
116
+ test_files:
117
+ - spec/integration/configurable_spec.rb
118
+ - spec/spec_helper.rb