summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/ayk/options.rb25
-rw-r--r--spec/options.rb58
2 files changed, 57 insertions, 26 deletions
diff --git a/lib/ayk/options.rb b/lib/ayk/options.rb
index 13b3e40..5650609 100644
--- a/lib/ayk/options.rb
+++ b/lib/ayk/options.rb
@@ -48,10 +48,8 @@ module AYK
def option doc, key, value, other = {}, &block
ks = key.to_sym
raise OptionsError.new("Overriding existing option #{key}") if pedantic? and @hash.has_key? ks
- trigger = block || other[:trigger]
- convert = {:doc => doc.to_s, :value => value}
- convert[:trigger] = trigger if trigger
- @hash[ks] = other.merge(convert)
+ other[:trigger] = block if block
+ @hash[ks] = other.merge( :doc=>doc.to_s, :value=>value )
end
alias o option
#
@@ -73,14 +71,14 @@ module AYK
# this block should behave like Proc.new { |new_value,prev_value| ... }
def trigger key, &block
ks = key.to_sym
- raise OptionError.new("Setting trigger for unknown option : #{key}") if pedantic? and not @hash.has_key? ks
+ raise OptionsError.new("Setting trigger for unknown option : #{key}") if pedantic? and not @hash.has_key? ks
@hash[ks][:trigger] = block
end
#
# To avoid lookup on the parent, we can set a default to the internal Hash.
# Parameters as in {Options#o}, but without the +key+.
def default doc, value, other={}
- @hash.default = other.merge(:doc => doc, :value => value)
+ @hash.default = other.merge(:doc=>doc, :value=>value)
end
#
# Try to retrieve the corresponding Hash for the passed keys, will try to
@@ -97,7 +95,7 @@ module AYK
nil
end
elsif sub_options = get(key)
- sub_options.get(*keys)
+ sub_options[*keys]
end
end
private :get
@@ -132,19 +130,6 @@ module AYK
self[meth]
end
end
-# #
-# # @param [Array] keys
-# # @param [Object] value
-# def set_value(keys, value)
-# got = get(*keys)
-# return got[:value] = value if got
-# raise(IndexError, "There is no option available for %p" % [keys])
-# end
-# def merge!(hash)
-# hash.each_pair do |key, value|
-# set_value(key.to_s.split('.'), value)
-# end
-# end
#
def to_hash; @hash end
def each █ @hash.each(&block) end
diff --git a/spec/options.rb b/spec/options.rb
index b6ce9a4..25741db 100644
--- a/spec/options.rb
+++ b/spec/options.rb
@@ -9,25 +9,71 @@ describe AYK::Options do
before(:all) do
@opt = AYK::Options.new 'Spec tests'
end
- it "set option key1" do
+ it "should set option key1" do
@opt.o("key 1 doc", :key1, 666) { puts 'trigger' }
end
+ it "should raise override existing key" do
+ lambda { @opt.o("key 1 doc", :key1, 666) { puts 'trigger' } }.should raise_error AYK::OptionsError
+ end
+ it "should set sub options" do
+ @opt.sub(:sub) do
+ o 'var 1', :var1, 69
+ o 'var 2', :var2, 999
+ end
+ @opt[:sub][:var1].should eql 69
+ @opt[:sub][:var2].should eql 999
+ end
+ it "should access subeys through [,]" do
+ @opt[:sub,:var1].should eql 69
+ @opt[:sub,:var2].should eql 999
+ end
+ it "should raise calling sub on existing option" do
+ lambda { @opt.sub :key1 }.should raise_error AYK::OptionsError
+ end
+ it "should raise unknown option when setting trigger" do
+ lambda { @opt.trigger :unknonwn }.should raise_error AYK::OptionsError
+ end
it "Options.get is private" do
lambda{ @opt.get(:key1) }.should raise_error NoMethodError
end
- it "get option key1 through method missing" do
+ it "should set a trigger on assignment" do
+ @opt[:sub][:var1].should eql 69
+ @opt[:sub].trigger :var1 do raise Exception.new "trigger" end
+ lambda{ @opt[:sub][:var1] = 96}.should raise_error Exception
+ @opt[:sub][:var1].should eql 69
+ begin
+ @opt[:sub][:var1] = 96
+ rescue Exception
+ $!.message.should eql "trigger"
+ end
+ @opt[:sub][:var1].should eql 69
+ end
+ it "should get option key1 through method missing" do
@opt.key1.should eql 666
end
- it "get option key1 through []" do
+ it "should get option key1 through []" do
@opt[:key1].should eql 666
end
- it "get option nokey through method missing" do
+ it "should get option nokey through method missing" do
@opt.nokey.should eql nil
end
- it "get option nokey through []" do
+ it "should get option nokey through []" do
@opt[:nokey].should eql nil
end
- it "nice init" do
+ it "should return default valuei through method missing" do
+ @opt.default 'default value', 'none'
+ @opt.nokey.should eql 'none'
+ end
+ it "should return default value through []" do
+ @opt.default 'default value', 'none'
+ @opt[:nokey].should eql 'none'
+ end
+ it "should assign new value through []=" do
+ @opt[:sub][:var2].should eql 999
+ @opt[:sub][:var2] = 666
+ @opt[:sub][:var2].should eql 666
+ end
+ it "should nice init" do
opts = AYK::Options.new('Spec nice') do |opt|
opt.dsl do
o 'var doc', :var, 666