From 06e355cf3e24d7819d52ecf5bd8333ec192542be Mon Sep 17 00:00:00 2001 From: Arkadiy Zabazhanov Date: Sat, 29 Jun 2019 01:16:25 +0700 Subject: [PATCH] Get rid of activesupport dependency (#230) --- .codacy.yml | 2 -- config.gemspec | 1 - lib/config.rb | 56 +++++++++++++++------------------ lib/config/configuration.rb | 36 +++++++++++++++++++++ lib/config/validation/schema.rb | 13 ++++---- spec/configuration_spec.rb | 14 +++++++++ spec/spec_helper.rb | 2 +- 7 files changed, 82 insertions(+), 42 deletions(-) delete mode 100644 .codacy.yml create mode 100644 lib/config/configuration.rb create mode 100644 spec/configuration_spec.rb diff --git a/.codacy.yml b/.codacy.yml deleted file mode 100644 index 8de15c6c..00000000 --- a/.codacy.yml +++ /dev/null @@ -1,2 +0,0 @@ -exclude_paths: - - 'spec/app/**/*' diff --git a/config.gemspec b/config.gemspec index 1408a53a..0d589f68 100644 --- a/config.gemspec +++ b/config.gemspec @@ -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' diff --git a/lib/config.rb b/lib/config.rb index dfbba30e..59b0590c 100644 --- a/lib/config.rb +++ b/lib/config.rb @@ -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' @@ -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 @@ -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 @@ -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 diff --git a/lib/config/configuration.rb b/lib/config/configuration.rb new file mode 100644 index 00000000..f1bec017 --- /dev/null +++ b/lib/config/configuration.rb @@ -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 diff --git a/lib/config/validation/schema.rb b/lib/config/validation/schema.rb index a9e8ec69..94f45160 100644 --- a/lib/config/validation/schema.rb +++ b/lib/config/validation/schema.rb @@ -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 diff --git a/spec/configuration_spec.rb b/spec/configuration_spec.rb new file mode 100644 index 00000000..13f73b8e --- /dev/null +++ b/spec/configuration_spec.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 1bcd3bad..b7476d2a 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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