Sha256: e2c01b3bb1562da0a163b9335d2d17b3fec45270e7eb3e47a9468db6455e1682

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

require 'delegate'

module Interactive
  class Options < SimpleDelegator
    include Interactive
    attr_accessor :options

    def initialize(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"}
    end

    def has_hash?
      @options.any? {|opt| opt.respond_to?(:to_hash) }
    end

    private

    def flatten_ranges(options)
      @options = options.inject([]) do |accum, opt|
        if opt.class == Range
          accum | opt.to_a
        elsif opt.respond_to?(:to_a)
          accum | opt.map.with_index {|item, index| {index.to_s => item} }
        else
          accum << opt
        end
      end
    end

    def wrap_each_option
      @options.map! {|option| Option(option) }
    end

    def first_chars
      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
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
interactive-0.2.0 lib/interactive/options.rb