Class: Spreet::Document
- Inherits:
-
Object
- Object
- Spreet::Document
- Defined in:
- lib/spreet/document.rb
Constant Summary collapse
- @@handlers =
{}
- @@associations =
{}
Instance Attribute Summary collapse
-
#sheets ⇒ Object
readonly
Returns the value of attribute sheets.
Class Method Summary collapse
- .extract_handler(file, handler_name = nil) ⇒ Object
- .read(file, options = {}) ⇒ Object
- .register_handler(klass, name, options = {}) ⇒ Object
Instance Method Summary collapse
-
#initialize(option = {}) ⇒ Document
constructor
A new instance of Document.
- #write(file, options = {}) ⇒ Object
Constructor Details
Instance Attribute Details
#sheets ⇒ Object (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, ={}) handler = extract_handler(file, .delete(:format)) return handler.read(file, ) 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, ={}) 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, ={}) handler = self.class.extract_handler(file, .delete(:format)) handler.write(self, file, ) end |