Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ConfigModule 2.0 #41

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,9 @@ Style/StringLiteralsInInterpolation:

Layout/AccessModifierIndentation:
EnforcedStyle: outdent

Style/EnforcedStyleForMultiline:
EnforcedStyle: consistent_comma

Naming/RescuedExceptionsVariableName:
PreferredName: error
2 changes: 1 addition & 1 deletion config_module.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
46 changes: 46 additions & 0 deletions lib/config_module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/config_module/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module ConfigModule
VERSION = "1.2.4".freeze
VERSION = "2.0.0"
end
32 changes: 32 additions & 0 deletions uspec/config_setup_spec.rb
Original file line number Diff line number Diff line change
@@ -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