-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduced new desc dsl with a block instead of option Hash
- Loading branch information
Dieter Späth
committed
Sep 17, 2014
1 parent
fc70676
commit aeadfc2
Showing
5 changed files
with
235 additions
and
7 deletions.
There are no files selected for viewing
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,112 @@ | ||
module Grape | ||
module Util | ||
module StrictHashConfiguration | ||
extend ActiveSupport::Concern | ||
|
||
module DSL | ||
extend ActiveSupport::Concern | ||
|
||
module ClassMethods | ||
def settings | ||
config_context.to_hash | ||
end | ||
|
||
def configure(&block) | ||
config_context.instance_exec(&block) | ||
end | ||
end | ||
end | ||
|
||
class SettingsContainer | ||
def initialize | ||
@settings = {} | ||
end | ||
|
||
def to_hash | ||
@settings.to_hash | ||
end | ||
end | ||
|
||
def self.config_class(*args) | ||
new_config_class = Class.new(SettingsContainer) | ||
|
||
args.each do |setting_name| | ||
str = if setting_name.respond_to? :values | ||
nested_settings_methods(setting_name) | ||
else | ||
simple_settings_methods(setting_name) | ||
end | ||
|
||
new_config_class.class_eval str | ||
end | ||
|
||
new_config_class | ||
end | ||
|
||
def self.simple_settings_methods(setting_name) | ||
<<-EVAL | ||
def #{setting_name}(new_value) | ||
@settings[:#{setting_name}] = new_value | ||
end | ||
EVAL | ||
end | ||
|
||
def self.nested_settings_methods(setting_name) | ||
StringIO.new.tap do |new_str| | ||
|
||
setting_name.each_with_object(new_str) do |(key, value), str| | ||
str.puts <<-EVAL | ||
def #{key}_context | ||
@#{key}_context ||= Grape::Util::StrictHashConfiguration.config_class(*#{value.inspect}).new | ||
end | ||
def #{key}(&block) | ||
#{key}_context.instance_exec(&block) | ||
end | ||
EVAL | ||
end | ||
|
||
new_str.puts <<-EVAL | ||
def to_hash | ||
@settings.to_hash.merge( | ||
#{setting_name.keys.map { |key| "#{key}: #{key}_context.to_hash" }.join(",\n")} | ||
) | ||
end | ||
EVAL | ||
|
||
end.string | ||
end | ||
|
||
def self.module(*args) | ||
new_module = Module.new do | ||
extend ActiveSupport::Concern | ||
include DSL | ||
end | ||
|
||
new_module.tap do |mod| | ||
|
||
class_mod = create_class_mod(args) | ||
|
||
mod.const_set(:ClassMethods, class_mod) | ||
|
||
end | ||
end | ||
|
||
def self.create_class_mod(args) | ||
new_module = Module.new do | ||
def config_context | ||
@config_context ||= config_class.new | ||
end | ||
end | ||
|
||
new_module.tap do |class_mod| | ||
new_config_class = config_class(*args) | ||
|
||
class_mod.send(:define_method, :config_class) do | ||
@config_context ||= new_config_class | ||
end | ||
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
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,39 @@ | ||
require 'spec_helper' | ||
module Grape | ||
module Util | ||
describe 'StrictHashConfiguration' do | ||
subject do | ||
Class.new do | ||
include Grape::Util::StrictHashConfiguration.module(:config1, :config2, config3: [:config4], config5: [config6: [:config7, :config8]]) | ||
end | ||
end | ||
|
||
it 'set nested configs' do | ||
subject.configure do | ||
config1 'alpha' | ||
config2 'beta' | ||
|
||
config3 do | ||
config4 'gamma' | ||
end | ||
|
||
local_var = 8 | ||
|
||
config5 do | ||
config6 do | ||
config7 7 | ||
config8 local_var | ||
end | ||
end | ||
end | ||
|
||
expect(subject.settings).to eq(config1: 'alpha', | ||
config2: 'beta', | ||
config3: { config4: 'gamma' }, | ||
config5: { config6: { config7: 7, config8: 8 } } | ||
) | ||
end | ||
|
||
end | ||
end | ||
end |