Skip to content

Commit

Permalink
Options#initialize now maintains funcdef parity
Browse files Browse the repository at this point in the history
Parity with OpenStruct#initialize and OptionParser#initialize
  • Loading branch information
guns committed Sep 15, 2011
1 parent 704623f commit 609b75a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
18 changes: 13 additions & 5 deletions lib/haus/options.rb
Expand Up @@ -12,12 +12,20 @@ class Haus
# Options#logger provides default logger
#
class Options < OptionParser
def initialize
@ostruct = OpenStruct.new
@ostruct.path = File.expand_path '../../..', __FILE__ # HAUS_PATH
@ostruct.debug = !!ENV['DEBUG']
def initialize arg = nil, width = nil, indent = nil
# Send first arg to @ostruct or self
hash = arg if arg.is_a? Hash
banner = arg unless arg.is_a? Hash

@ostruct = OpenStruct.new hash
@ostruct.path = File.expand_path '../../..', __FILE__
@ostruct.debug = !!ENV['DEBUG']
@ostruct.logger = Haus::Logger.new
super

params = [banner]
params.push width if width
params.push indent if indent
super *params
end

def path= arg
Expand Down
12 changes: 12 additions & 0 deletions lib/haus/test/options.test.rb
Expand Up @@ -33,6 +33,18 @@ class Haus::OptionsSpec < MiniTest::Spec
puts Haus::Options.new.debug == true
end.first.must_equal "true\ntrue\n"
end

it 'must accept one optional Hash argument like OpenStruct' do
o = Haus::Options.new :funcdef => :parity
o.funcdef.must_equal :parity
end

it 'must accept up to three arguments like OptionParser' do
o = Haus::Options.new 'Hello World', 42, '****'
o.banner.must_equal 'Hello World'
o.summary_width.must_equal 42
o.summary_indent.must_equal '****'
end
end

describe :path do
Expand Down

0 comments on commit 609b75a

Please sign in to comment.