Sha256: e9483dd998e96cb327003ade6e27bf8716180c790c48ce17c21acb9be294604d

Contents?: true

Size: 1.2 KB

Versions: 2

Compression:

Stored size: 1.2 KB

Contents

module BerkeleyLibrary
  module Util
    # TODO: Move this to `berkeley_library-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)
        # TODO: is it possible/desirable to loosen this? how strict is libxml2?
        obj.is_a?(IO) || obj.is_a?(StringIO)
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
berkeley_library-tind-0.5.1 lib/berkeley_library/util/files.rb
berkeley_library-tind-0.5.0 lib/berkeley_library/util/files.rb