Class: Treat::Workers::Formatters::Readers::Autoselect

Inherits:
Object
  • Object
show all
Defined in:
lib/treat/workers/formatters/readers/autoselect.rb

Constant Summary collapse

ExtensionRegexp =
/^.*?\.([a-zA-Z0-9]{2,5})$/
ImageExtensions =
['gif', 'jpg', 'jpeg', 'png']
DefaultOptions =
{
  :default_to => 'document'
}

Class Method Summary collapse

Class Method Details

.detect_format(filename, default_to = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/treat/workers/formatters/readers/autoselect.rb', line 20

def self.detect_format(filename, default_to = nil)
  
  default_to ||= DefaultOptions[:default_to]
  ext = filename.scan(ExtensionRegexp)
  ext = (ext.is_a?(Array) && ext[0] && ext[0][0]) ? ext[0][0] : ''
  
  format = ImageExtensions.include?(ext) ? 'image' : ext
  format = 'html' if format == 'htm'
  format = 'yaml' if format == 'yml'

  format = default_to if format.to_s == ''
  
  begin
    Treat::Workers::Formatters::Readers.const_get(format.cc)
  rescue Treat::Exception
    format = default_to
  end
  
  format.intern

end

.read(document, options = {}) ⇒ Object

Choose a reader to use.

Options:

- (Symbol) :default_to => format to default to.


13
14
15
16
17
18
# File 'lib/treat/workers/formatters/readers/autoselect.rb', line 13

def self.read(document, options = {})
  options = DefaultOptions.merge(options)
  fmt = detect_format(document.file, options[:default_to])
  Treat::Workers::Formatters::Readers.
  const_get(fmt.cc).read(document,options)
end