Class: Spreet::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/spreet/document.rb

Constant Summary collapse

@@handlers =
{}
@@associations =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(option = {}) ⇒ Document

Returns a new instance of Document.



8
9
10
# File 'lib/spreet/document.rb', line 8

def initialize(option={})
  @sheets = Sheets.new(self)
end

Instance Attribute Details

#sheetsObject (readonly)

Returns the value of attribute sheets.



4
5
6
# File 'lib/spreet/document.rb', line 4

def sheets
  @sheets
end

Class Method Details

.extract_handler(file, handler_name = nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/spreet/document.rb', line 36

def extract_handler(file, handler_name=nil)
  file_path = Pathname.new(file)
  extension = file_path.extname.to_s[1..-1]
  if !handler_name and extension.size > 0
    handler_name = extension.to_sym
  end
  if @@handlers[handler_name]
    return @@handlers[handler_name]
  else
    raise ArgumentError.new("No corresponding handler (#{handler_name.inspect}). Available: #{@@handlers.keys.collect{|k| k.inspect}.join(', ')}.")
  end
end

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



31
32
33
34
# File 'lib/spreet/document.rb', line 31

def read(file, options={})
  handler = extract_handler(file, options.delete(:format))
  return handler.read(file, options)
end

.register_handler(klass, name, options = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/spreet/document.rb', line 19

def register_handler(klass, name, options={})
  if klass.respond_to?(:read) or klass.respond_to?(:write)
    if name.is_a?(Symbol)
      @@handlers[name] = klass # options.merge(:class=>klass)
    elsif
      raise ArgumentError.new("Name is invalid. Symbol expected, #{name.class.name} got.")
    end
  else
    raise ArgumentError.new("Handler do not support :read or :write method.")
  end
end

Instance Method Details

#write(file, options = {}) ⇒ Object



12
13
14
15
# File 'lib/spreet/document.rb', line 12

def write(file, options={})
  handler = self.class.extract_handler(file, options.delete(:format))
  handler.write(self, file, options)
end