Sha256: 4707d41feffe3b32af897100087d324d710542b78e458ec221410decf1a7cb50

Contents?: true

Size: 917 Bytes

Versions: 2

Compression:

Stored size: 917 Bytes

Contents

class Mode < ::String
  class Error < ::StandardError; end
  class Duplicate < Error; end
  class Ambigous < Error
    include Main::Softspoken
  end

  class List < ::Array
    def initialize *a, &b
      super
    ensure
      self.fields = []
    end
    def add klass
      mode_name = Mode.new klass.mode_name
      raise Duplicate, mode_name if has_key? mode_name
      self[mode_name] = klass
    end
    def find_by_mode m, options = {}
      quiet = options['quiet'] || options[:quiet]
      each_pair do |mode, klass|
        return mode if mode == m
      end
      candidates = []
      each_pair do |mode, klass|
        candidates << mode if mode.index(m) == 0
      end
      case candidates.size
        when 0
          nil
        when 1
          candidates.first
        else
          quiet ? nil : raise(Ambigous, m)
      end
    end
  end

  def self.list *a, &b
    List.new *a, &b
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
main-2.1.0 lib/main/mode.rb
main-2.0.0 lib/main/mode.rb