Sha256: 37f7101630a8c832102f3683edbf2e8eb9e063a6cffa017ab70138b7f39a43b6

Contents?: true

Size: 606 Bytes

Versions: 3

Compression:

Stored size: 606 Bytes

Contents

require 'pathname'

module XMP::Handler
  class File
    SUPPORTED = [String, Pathname, ::File]
    attr_reader :extensions, :callback

    def initialize(*extensions, &callback)
      @extensions = extensions
      @callback   = callback
    end

    def call(object)
      return unless SUPPORTED.any? { |c| c === object }
      return unless path = Pathname(object) and extensions.include? path.extname.downcase
      return callback.call(object) if object.is_a? IO
      return path.open(&callback)  if path.exist? and path.readable?
      raise XMP::Error, "cannot read file #{path}"
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
xmp-2.0.0 lib/xmp/handler/file.rb
xmp-1.0.1 lib/xmp/handler/file.rb
xmp-1.0.0 lib/xmp/handler/file.rb