Sha256: 35e74a32bfa7ae92a2602ff0c9f79145c060c48f2c6c2686fdce64de1c1bd8bd

Contents?: true

Size: 1.17 KB

Versions: 7

Compression:

Stored size: 1.17 KB

Contents

require 'delegate'

module Interactive
  module_function

  def Option(option)
    if option.respond_to?(:to_hash)
      @option = HashNumberedOption.new(option)
    elsif option.to_s.match(/^\d+$/)
      @option = WholeNumberOption.new(option)
    else
      @option = WordOption.new(option)
    end

    @option
  end

  class WholeNumberOption < SimpleDelegator
    def initialize(option)
      @option = option
      super(@option)
    end

    def shortcut_value
      @option
    end

    def query_method_name
      "whole_number_#{shortcut_value}?"
    end

    def value
      @option
    end
  end

  class WordOption < SimpleDelegator
    def initialize(option)
      @option = option
      super(@option)
    end

    def shortcut_value
      @option.to_s[0]
    end

    def query_method_name
      "#{@option}?"
    end

    def value
      @option
    end
  end

  class HashNumberedOption < SimpleDelegator
    def initialize(option)
      @option = option
      super(@option)
    end

    def shortcut_value
      @option.keys.first
    end

    def query_method_name
      "whole_number_#{shortcut_value}?"
    end

    def value
      @option[shortcut_value]
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
interactive-0.6.2 lib/interactive/option.rb
interactive-0.6.1 lib/interactive/option.rb
interactive-0.6.0 lib/interactive/option.rb
interactive-0.5.0 lib/interactive/option.rb
interactive-0.4.0 lib/interactive/option.rb
interactive-0.3.0 lib/interactive/option.rb
interactive-0.2.0 lib/interactive/option.rb