Sha256: 8d491ff8e10334603fc7c82b6b843f515d3e824c470d9d98269b6a510d3c8a3a

Contents?: true

Size: 1.09 KB

Versions: 4

Compression:

Stored size: 1.09 KB

Contents

module BerkeleyLibrary
  module Util
    module Files
      class << self
        include Files
      end

      def file_exists?(path)
        (path.respond_to?(:exist?) && path.exist?) ||
          (path.respond_to?(:to_str) && File.exist?(path))
      end

      def parent_exists?(path)
        path.respond_to?(:parent) && path.parent.exist? ||
          path.respond_to?(:to_str) && Pathname.new(path).parent.exist?
      end

      # Returns true if `obj` is close enough to an IO object for Nokogiri
      # to parse as one.
      #
      # @param obj [Object] the object that might be an IO
      # @see https://github.com/sparklemotion/nokogiri/blob/v1.11.1/lib/nokogiri/xml/sax/parser.rb#L81 Nokogiri::XML::SAX::Parser#parse
      def reader_like?(obj)
        obj.respond_to?(:read) && obj.respond_to?(:close)
      end

      # Returns true if `obj` is close enough to an IO object for Nokogiri
      # to write to.
      #
      # @param obj [Object] the object that might be an IO
      def writer_like?(obj)
        obj.respond_to?(:write) && obj.respond_to?(:close)
      end

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
berkeley_library-util-0.1.5 lib/berkeley_library/util/files.rb
berkeley_library-util-0.1.4 lib/berkeley_library/util/files.rb
berkeley_library-util-0.1.3 lib/berkeley_library/util/files.rb
berkeley_library-util-0.1.2 lib/berkeley_library/util/files.rb