File: spreadsheet.rb

package info (click to toggle)
ruby-roo 3.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 8,216 kB
  • sloc: ruby: 6,529; xml: 88; makefile: 6
file content (39 lines) | stat: -rw-r--r-- 1,106 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
require 'uri'

module Roo
  class Spreadsheet
    class << self
      def open(path, options = {})
        path      = path.respond_to?(:path) ? path.path : path
        extension = extension_for(path, options)

        begin
          Roo::CLASS_FOR_EXTENSION.fetch(extension).new(path, options)
        rescue KeyError
          raise ArgumentError,
                "Can't detect the type of #{path} - please use the :extension option to declare its type."
        end
      end

      def extension_for(path, options)
        case (extension = options.delete(:extension))
        when ::Symbol
          options[:file_warning] = :ignore
          extension
        when ::String
          options[:file_warning] = :ignore
          extension.tr('.', '').downcase.to_sym
        else
          parsed_path =
            if path =~ /\A#{::Roo::Utils::URI_PARSER.make_regexp}\z/
              # path is 7th match
              Regexp.last_match[7]
            else
              path
            end
          ::File.extname(parsed_path).tr('.', '').downcase.to_sym
        end
      end
    end
  end
end