Sha256: d43b99b313e3f2c52831a186ed6a3c774df513a21de6a5cfb2cef660dc098ee5

Contents?: true

Size: 965 Bytes

Versions: 17

Compression:

Stored size: 965 Bytes

Contents

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

Version data entries

17 entries across 17 versions & 4 rubygems

Version Path
ThiagoLelis-backgroundjob-1.0.4 lib/main/mode.rb
ahoward-main-2.9.0 lib/main/mode.rb
vibes-bj-1.2.2 spec/rails_root/vendor/plugins/bj/lib/main/mode.rb
vibes-bj-1.2.1 spec/rails_root/vendor/plugins/bj/lib/main/mode.rb
main-2.9.2 lib/main/mode.rb
main-2.9.0 lib/main/mode.rb
main-2.2.0 lib/main/mode.rb
main-2.4.0 lib/main/mode.rb
main-2.3.0 lib/main/mode.rb
main-2.7.0 lib/main/mode.rb
main-2.8.1 lib/main/mode.rb
main-2.8.3 lib/main/mode.rb
main-2.6.0 lib/main/mode.rb
main-2.8.4 lib/main/mode.rb
main-2.5.0 lib/main/mode.rb
main-2.8.2 lib/main/mode.rb
main-2.8.0 lib/main/mode.rb