lib/interactive/options.rb in interactive-0.2.0 vs lib/interactive/options.rb in interactive-0.3.0

- old
+ new

@@ -1,24 +1,29 @@ require 'delegate' +require 'interactive' module Interactive - class Options < SimpleDelegator - include Interactive - attr_accessor :options + module_function + def Options(options=[]) + options.empty? ? EmptyOptions.new([]) : NonEmptyOptions.new(options) + end + class NonEmptyOptions < SimpleDelegator + include Interactive def initialize(options) - flatten_ranges(options) + @options = options + flatten_ranges(@options) wrap_each_option super(@options) end def shortcuts_string "[#{first_chars_without_last_slash(first_chars)}]" end def shortcuts_meanings - options.inject("") { |accum, opt| "#{accum} #{opt.shortcut_value} -- #{opt.value}\n"} + @options.inject("") { |accum, opt| "#{accum} #{opt.shortcut_value} -- #{opt.value}\n"} end def has_hash? @options.any? {|opt| opt.respond_to?(:to_hash) } end @@ -40,13 +45,33 @@ def wrap_each_option @options.map! {|option| Option(option) } end def first_chars - options.inject("") { |accum, opt| "#{accum}#{ opt.shortcut_value}/" } + @options.inject("") { |accum, opt| "#{accum}#{ opt.shortcut_value}/" } end def first_chars_without_last_slash(first_chars) first_chars[0..first_chars.length-2] + end + end + + + class EmptyOptions < SimpleDelegator + def initialize(options) + @options = options + super(@options) + end + + def shortcuts_string + '' + end + + def shortcuts_meanings + '' + end + + def has_hash? + false end end end