Skip to content

Commit

Permalink
Add explicit and default values for keys.
Browse files Browse the repository at this point in the history
  • Loading branch information
jbarnette committed Mar 5, 2012
1 parent ce47d1f commit 51b049f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
17 changes: 11 additions & 6 deletions lib/between/key.rb
Expand Up @@ -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
21 changes: 21 additions & 0 deletions test/between_parser_test.rb
Expand Up @@ -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

0 comments on commit 51b049f

Please sign in to comment.