-
-
Notifications
You must be signed in to change notification settings - Fork 235
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get rid of activesupport dependency (#230)
- Loading branch information
1 parent
b1cff98
commit 06e355c
Showing
7 changed files
with
82 additions
and
42 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module Config | ||
# The main configuration backbone | ||
class Configuration < Module | ||
# Accepts configuration options, | ||
# initializing a module that can be used to extend | ||
# the necessary class with the provided config methods | ||
def initialize(**attributes) | ||
attributes.each do |name, default| | ||
define_reader(name, default) | ||
define_writer(name) | ||
end | ||
end | ||
|
||
private | ||
|
||
def define_reader(name, default) | ||
variable = :"@#{name}" | ||
|
||
define_method(name) do | ||
if instance_variable_defined?(variable) | ||
instance_variable_get(variable) | ||
else | ||
default | ||
end | ||
end | ||
end | ||
|
||
def define_writer(name) | ||
variable = :"@#{name}" | ||
|
||
define_method("#{name}=") do |value| | ||
instance_variable_set(variable, value) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,20 @@ | ||
require 'dry-schema' | ||
require 'config/validation/schema' | ||
|
||
module Config | ||
module Validation | ||
module Schema | ||
|
||
mattr_writer :schema | ||
@@schema = nil | ||
# Assigns schema configuration option | ||
def schema=(value) | ||
@schema = value | ||
end | ||
|
||
def schema(&block) | ||
if block_given? | ||
@@schema = Dry::Schema.define(&block) | ||
@schema = Dry::Schema.define(&block) | ||
else | ||
@@schema | ||
@schema | ||
end | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
require 'spec_helper' | ||
|
||
describe Config::Configuration do | ||
subject do | ||
Module.new do | ||
extend Config::Configuration.new(hello: 'world') | ||
end | ||
end | ||
|
||
it 'extends a module with additional methods' do | ||
expect(subject.hello).to eq('world') | ||
expect { subject.hello = 'dlrow' }.to change { subject.hello }.to('dlrow') | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters