diff --git a/lib/between/key.rb b/lib/between/key.rb index 8b38871..46d1cef 100644 --- a/lib/between/key.rb +++ b/lib/between/key.rb @@ -9,20 +9,25 @@ def initialize name, context, data, options = nil @context = context @data = data + @default = options && options[:default] @source = (options && options[:from]) || name @target = name.intern - end - - def get - @data[@source] || @data[@source.to_s] + @value = options && options[:value] end def exists? - @data.include?(@source) || @data.include?(@source.to_s) + !!(@value || @data.include?(@source) || + @data.include?(@source.to_s) || @default) end def set - @context.set @target, get + @context.set @target, value + + value + end + + def value + @value ||= @data[@source] || @data[@source.to_s] || @default end end end diff --git a/test/between_parser_test.rb b/test/between_parser_test.rb index 839f5d9..b4ad114 100644 --- a/test/between_parser_test.rb +++ b/test/between_parser_test.rb @@ -40,5 +40,26 @@ p.key :foo, :from => :baz end + + it "can specify an explicit override value" do + ctx = mock { expects(:set).with :foo, "bar" } + p = Between::Parser.new ctx, "foo" => "baz" + + p.key :foo, :value => "bar" + end + + it "can specify a default value" do + ctx = mock { expects(:set).with :foo, "bar" } + p = Between::Parser.new ctx + + p.key :foo, :default => "bar" + end + + it "returns the value set on the context" do + ctx = stub :set + p = Between::Parser.new ctx, "foo" => "bar" + + assert_equal "bar", p.key(:foo) + end end end