Sha256: 95895eab896763c546684da19da7518d50fc0e2815b5aced95ecb5eb5100cd30

Contents?: true

Size: 1.5 KB

Versions: 10

Compression:

Stored size: 1.5 KB

Contents

require 'engineyard/error'

module EY
  module Collection
    class Abstract < Array
      COLLAB_MESSAGE = <<-MSG
\nThis error is due to having access to another account's resources via the collaboration feature.
Specify --account ACCOUNT_NAME to resolve this ambiguity.
      MSG

      def named(name, account_name=nil)
        candidates = find_all do |x|
          if account_name
            x.name.downcase == name.downcase && x.account.name.downcase == account_name.downcase
          else
            x.name.downcase == name.downcase
          end
        end
        if candidates.size > 1
          raise ambiguous_error(name, candidates.map {|e| e.name}, COLLAB_MESSAGE )
        end
        candidates.first
      end

      def match_one(name_part)
        named(name_part) || find_by_unambiguous_substring(name_part)
      end

      def match_one!(name_part)
        match_one(name_part) or raise invalid_error(name_part)
      end

    private

      def find_by_unambiguous_substring(name_part)
        candidates = find_all{|e| e.name.downcase[name_part.downcase] }
        if candidates.size > 1
          raise ambiguous_error(name_part, candidates.map {|e| e.name})
        end
        candidates.first
      end

      class << self
        attr_accessor :invalid_error, :ambiguous_error
      end

      def invalid_error(*args, &blk)
        self.class.invalid_error.new(*args, &blk)
      end

      def ambiguous_error(*args, &blk)
        self.class.ambiguous_error.new(*args, &blk)
      end

    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
engineyard-1.4.0 lib/engineyard/collection/abstract.rb
engineyard-1.3.33 lib/engineyard/collection/abstract.rb
engineyard-1.3.32 lib/engineyard/collection/abstract.rb
engineyard-1.3.31 lib/engineyard/collection/abstract.rb
engineyard-1.3.30 lib/engineyard/collection/abstract.rb
engineyard-1.3.29 lib/engineyard/collection/abstract.rb
engineyard-1.3.28 lib/engineyard/collection/abstract.rb
engineyard-1.3.25 lib/engineyard/collection/abstract.rb
engineyard-1.3.22 lib/engineyard/collection/abstract.rb
engineyard-1.3.21 lib/engineyard/collection/abstract.rb