diff --git a/.rubocop.yml b/.rubocop.yml index b87c721..56035f3 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -48,3 +48,9 @@ Style/StringLiteralsInInterpolation: Layout/AccessModifierIndentation: EnforcedStyle: outdent + +Style/EnforcedStyleForMultiline: + EnforcedStyle: consistent_comma + +Naming/RescuedExceptionsVariableName: + PreferredName: error \ No newline at end of file diff --git a/config_module.gemspec b/config_module.gemspec index fe004f7..c5157ae 100644 --- a/config_module.gemspec +++ b/config_module.gemspec @@ -22,5 +22,5 @@ Gem::Specification.new do |gem| gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) gem.require_paths = ["lib"] - gem.add_development_dependency "uspec", "~> 0.1.0" + gem.add_development_dependency "uspec", "~> 0.2.1" end diff --git a/lib/config_module.rb b/lib/config_module.rb index 032dbce..a3d86f5 100644 --- a/lib/config_module.rb +++ b/lib/config_module.rb @@ -46,4 +46,50 @@ def method_missing name, *args, &block def respond_to_missing? name, include_all __config_module_helper.respond_to_missing_handler(name, include_all) || super end + +module_function + + def setup &block + options = { + method_name: "config", + path: "./config/settings.yml", + } + + setup_dsl = Class.new do + def method_name new_name + options[:method_name] = new_name + end + + def path new_path + options[:path] = new_path + end + + def namespaces new_namespaces + options[:namespaces] = new_namespaces.flatten + end + + def options + @options ||= {} + end + end + + if block_given? + options.merge! setup_dsl.new.tap { |dsl| dsl.instance_eval(&block) }.options + end + + Module.new do + @options = options + + define_method options[:method_name] do + __config_module_helper.config + end + + def self.extended child + child.extend ConfigModule + helper = child.send(:__config_module_helper) + helper.config_file = @options[:path] + helper.namespaces = @options[:namespaces] + end + end + end end diff --git a/lib/config_module/version.rb b/lib/config_module/version.rb index 9d19c9b..6b7adff 100644 --- a/lib/config_module/version.rb +++ b/lib/config_module/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module ConfigModule - VERSION = "1.2.4".freeze + VERSION = "2.0.0" end diff --git a/uspec/config_setup_spec.rb b/uspec/config_setup_spec.rb new file mode 100644 index 0000000..1a75706 --- /dev/null +++ b/uspec/config_setup_spec.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +require_relative 'spec_helper' +require_relative 'example_config' + +spec 'able to set options using setup' do + c = ConfigModule.setup do + method_name :new_name + path 'new_path' + end + + expected = {method_name: :new_name, path: 'new_path'} + actual = c.instance_variable_get(:@options) + + actual == expected || actual +end + +spec 'setup method returns a module' do + c = ConfigModule.setup + c.class == Module +end + + +spec 'setup module can extend for full effect' do + m = Module.new + c = ConfigModule.setup do + path 'config/example.yml' + end + m.extend c + + m.is_a? ConfigModule +end