Sha256: 18756b74b069001ace9cc5ef559c25179816ee34c6f779645cd86c69da09ee69

Contents?: true

Size: 937 Bytes

Versions: 1

Compression:

Stored size: 937 Bytes

Contents

module R10K
  # Detect whether a given feature is present or absent
  class Feature

    # @attribute [r] name
    #   @return [Symbol] The name of this feature
    attr_reader :name

    # @param name [Symbol] The name of this feature
    # @param opts [Hash]
    # @param block [Proc] An optional block to detect if this feature is available
    #
    # @option opts [String, Array<String>] :libraries One or more libraries to
    #   require to make sure this feature is present.
    def initialize(name, opts = {}, &block)
      @name      = name
      @libraries = Array(opts.delete(:libraries))
      @block     = block || lambda { true }
    end

    # @return [true, false] Is this feature available?
    def available?
      @libraries.all? { |lib| library_available?(lib) } && !!@block.call
    end

    private

    def library_available?(lib)
      require lib
      true
    rescue ScriptError
      false
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
r10k-1.5.1 lib/r10k/feature.rb