Sha256: 0e0d853bdeb93477d46d613fdd55fdf5b3f4989ce695e80640130950e9d36505

Contents?: true

Size: 1.04 KB

Versions: 32

Compression:

Stored size: 1.04 KB

Contents

module Main
  class Mode < ::String
    class Error < ::StandardError; end
    class Duplicate < Error; end
    class Ambiguous < 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
            raise Ambiguous, "ambiguous mode: #{ m } = (#{ candidates.sort.join ' or ' })?"
        end
      end
    end

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

Version data entries

32 entries across 31 versions & 2 rubygems

Version Path
main-6.2.2 lib/main/mode.rb
main-6.2.1 lib/main/mode.rb
main-6.2.0 lib/main/mode.rb
asana2flowdock-1.0.0 vendor/bundle/ruby/1.9.1/gems/main-6.0.0/lib/main/mode.rb
asana2flowdock-1.0.0 vendor/bundle/ruby/1.9.1/gems/main-6.1.0/lib/main/mode.rb
main-6.1.0 lib/main/mode.rb
main-6.0.0 lib/main/mode.rb
main-5.3.2 lib/main/mode.rb
main-5.3.1 lib/main/mode.rb
main-5.2.0 lib/main/mode.rb
main-5.1.1 lib/main/mode.rb
main-5.1.0 lib/main/mode.rb
main-5.0.1 lib/main/mode.rb
main-5.0.0 lib/main/mode.rb
main-4.8.1 lib/main/mode.rb
main-4.8.0 lib/main/mode.rb
main-4.7.7 lib/main/mode.rb
main-4.7.6 lib/main/mode.rb
main-4.7.5 lib/main/mode.rb
main-4.7.4 lib/main/mode.rb