Class: XMP::Handler::File

Inherits:
Object
  • Object
show all
Defined in:
lib/xmp/handler/file.rb

Constant Summary collapse

SUPPORTED =
[String, Pathname, ::File]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*extensions, &callback) ⇒ File

Returns a new instance of File.



8
9
10
11
# File 'lib/xmp/handler/file.rb', line 8

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

Instance Attribute Details

#callbackObject (readonly)

Returns the value of attribute callback.



6
7
8
# File 'lib/xmp/handler/file.rb', line 6

def callback
  @callback
end

#extensionsObject (readonly)

Returns the value of attribute extensions.



6
7
8
# File 'lib/xmp/handler/file.rb', line 6

def extensions
  @extensions
end

Instance Method Details

#call(object) ⇒ Object

Raises:



13
14
15
16
17
18
19
# File 'lib/xmp/handler/file.rb', line 13

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