Permalink
Browse files

Allow string_validation_options to be passed a block.

This gives extra flexibility.
  • Loading branch information...
1 parent 8796d0b commit 906fa8dd3236195b8139e1049261fcf4861c6b3f @myronmarston myronmarston committed Oct 23, 2012
Showing with 16 additions and 4 deletions.
  1. +5 −4 lib/interpol/configuration.rb
  2. +11 −0 spec/unit/interpol/configuration_spec.rb
@@ -374,8 +374,8 @@ def initialize(type, options = {})
yield self
end
- def string_validation_options(options)
- @string_validation_options = options
+ def string_validation_options(options = nil, &block)
+ @string_validation_options_block = block || Proc.new { options }
end
def parse(&block)
@@ -389,8 +389,9 @@ def matches_options?(options)
end
def type_validation_options_for(type, options)
- return type unless @string_validation_options
- Array(type) + [@string_validation_options.merge('type' => 'string')]
+ return type unless @string_validation_options_block
+ string_options = @string_validation_options_block.call(options)
+ Array(type) + [string_options.merge('type' => 'string')]
end
def parse_value(value)
@@ -400,6 +400,17 @@ def assert_expected_endpoint
end
end
+ it 'allows a block to be passed for string_validation_options' do
+ parser = ParamParser.new("foo", "bar" => 3) do |p|
+ p.string_validation_options do |opts|
+ opts.merge("a" => 2)
+ end
+ end
+
+ options = parser.type_validation_options_for('foo', 'b' => 3)
+ options.last.should eq("type" => "string", "b" => 3, "a" => 2)
+ end
+
RSpec::Matchers.define :have_errors_for do |value|
match do |schema|
validate(schema)

0 comments on commit 906fa8d

Please sign in to comment.