Module: Win32::CaptureIE::Util

Included in:
BitMap
Defined in:
lib/win32/capture_ie/util.rb

Overview

:nodoc:

Class Method Summary collapse

Class Method Details

.filename_typeObject



10
11
12
13
14
15
# File 'lib/win32/capture_ie/util.rb', line 10

def filename_type
  r = []
  r << ::String
  r << ::Pathname if defined?(::Pathname)
  r
end

.guess_format(writable, default = "bmp") ⇒ Object



29
30
31
32
33
34
35
# File 'lib/win32/capture_ie/util.rb', line 29

def guess_format(writable, default="bmp")
  r = with_filename_or_writable(writable, {
    :if_writable => lambda { "" },
    :if_filename => lambda { File.extname(writable).downcase.sub(/\A\./, "") },
  })
  (r == "") ? default : r
end

.maybe_enable_binmode(obj) ⇒ Object



6
7
8
# File 'lib/win32/capture_ie/util.rb', line 6

def maybe_enable_binmode(obj)
  obj.binmode if obj.respond_to?(:binmode)
end

.with_filename_or_writable(duck, opts) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/win32/capture_ie/util.rb', line 17

def with_filename_or_writable(duck, opts)
  if filename_type.any?{|t| t === duck }
    maybe_enable_binmode(duck)
    opts[:if_filename].call(duck)
  elsif duck.respond_to?(:write)
    opts[:if_writable].call(duck)
  else
    raise ArgumentError, "`#{duck.inspect}' (#{duck.class}) " +
      "should be writable object or filename (String or Pathname)."
  end
end

.with_output_stream(duck, &block) ⇒ Object



37
38
39
40
41
42
# File 'lib/win32/capture_ie/util.rb', line 37

def with_output_stream(duck, &block)
  with_filename_or_writable(duck, {
    :if_writable => lambda { block.call(duck) },
    :if_filename => lambda { open(duck, "wb", &block) },
  })
end