Skip to content

Commit

Permalink
Get rid of activesupport dependency (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
pyromaniac authored and pkuczynski committed Jun 28, 2019
1 parent b1cff98 commit 06e355c
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 42 deletions.
2 changes: 0 additions & 2 deletions .codacy.yml

This file was deleted.

1 change: 0 additions & 1 deletion config.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ Gem::Specification.new do |s|
s.require_paths = ['lib']
s.required_ruby_version = '>= 2.4.0'

s.add_dependency 'activesupport', '>= 4.2'
s.add_dependency 'deep_merge', '~> 1.2', '>= 1.2.1'
s.add_dependency 'dry-schema', '~> 1.0'

Expand Down
56 changes: 25 additions & 31 deletions lib/config.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require 'active_support/core_ext/module/attribute_accessors'

require 'config/compatibility'
require 'config/options'
require 'config/configuration'
require 'config/version'
require 'config/integrations/rails/engine' if defined?(::Rails)
require 'config/sources/yaml_source'
Expand All @@ -11,30 +10,25 @@

module Config
extend Config::Validation::Schema

# Ensures the setup only gets run once
@@_ran_once = false

mattr_accessor :const_name, :use_env, :env_prefix, :env_separator,
:env_converter, :env_parse_values, :fail_on_missing
@@const_name = 'Settings'
@@use_env = false
@@env_prefix = @@const_name
@@env_separator = '.'
@@env_converter = :downcase
@@env_parse_values = true
@@fail_on_missing = false

# deep_merge options
mattr_accessor :knockout_prefix, :merge_nil_values, :overwrite_arrays, :merge_hash_arrays
@@knockout_prefix = nil
@@merge_nil_values = true
@@overwrite_arrays = true
@@merge_hash_arrays = false
extend Config::Configuration.new(
# general options
const_name: 'Settings',
use_env: false,
env_prefix: 'Settings',
env_separator: '.',
env_converter: :downcase,
env_parse_values: true,
fail_on_missing: false,
# deep_merge options
knockout_prefix: nil,
merge_nil_values: true,
overwrite_arrays: true,
merge_hash_arrays: false
)

def self.setup
yield self if @@_ran_once == false
@@_ran_once = true
yield self unless @_ran_once
@_ran_once = true
end

# Create a populated Options instance from a settings file. If a second file is given, then the sections of that
Expand All @@ -48,7 +42,7 @@ def self.load_files(*files)
end

config.load!
config.load_env! if @@use_env
config.load_env! if use_env
config
end

Expand All @@ -61,13 +55,13 @@ def self.load_and_set_settings(*files)

def self.setting_files(config_root, env)
[
File.join(config_root, "settings.yml").to_s,
File.join(config_root, "settings", "#{env}.yml").to_s,
File.join(config_root, "environments", "#{env}.yml").to_s,
File.join(config_root, 'settings.yml').to_s,
File.join(config_root, 'settings', "#{env}.yml").to_s,
File.join(config_root, 'environments', "#{env}.yml").to_s,

File.join(config_root, "settings.local.yml").to_s,
File.join(config_root, "settings", "#{env}.local.yml").to_s,
File.join(config_root, "environments", "#{env}.local.yml").to_s
File.join(config_root, 'settings.local.yml').to_s,
File.join(config_root, 'settings', "#{env}.local.yml").to_s,
File.join(config_root, 'environments', "#{env}.local.yml").to_s
].freeze
end

Expand Down
36 changes: 36 additions & 0 deletions lib/config/configuration.rb
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
13 changes: 6 additions & 7 deletions lib/config/validation/schema.rb
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
14 changes: 14 additions & 0 deletions spec/configuration_spec.rb
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
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def self.reset
self.knockout_prefix = nil
self.overwrite_arrays = true
self.schema = nil
class_variable_set(:@@_ran_once, false)
instance_variable_set(:@_ran_once, false)
end
end
end
Expand Down

0 comments on commit 06e355c

Please sign in to comment.