Class: Bookbinder::File
- Inherits:
-
Object
- Object
- Bookbinder::File
- Defined in:
- lib/bookbinder/file.rb
Instance Attribute Summary collapse
-
#file_type ⇒ Object
Returns the value of attribute file_type.
-
#path ⇒ Object
Returns the value of attribute path.
Instance Method Summary collapse
-
#copy_to(dest_file_system, dest_path) ⇒ Object
Writes the file to another file system.
-
#dirty! ⇒ Object
Indicates that we should write out the document on save.
-
#dirty? ⇒ Boolean
Has the document changed, therefore we should write it out on save?.
-
#document(mode = 'rw') ⇒ Object
Gets a representation of the contents – if json, as a hash, if xml, as a Nokogiri document, etc.
-
#exists? ⇒ Boolean
Proxy through to FileSystem#exists?.
-
#get_file(mode = 'r', &blk) ⇒ Object
Proxy through to FileSystem#get_file.
-
#initialize(path, file_system) ⇒ File
constructor
A new instance of File.
-
#media_type ⇒ Object
Guesses the mime-type (aka content-type, aka media-type) of this file based on its extension.
- #new_xml_document(&blk) ⇒ Object
-
#read ⇒ Object
Proxy through to FileSystem#read.
-
#reset ⇒ Object
Resets state so that the next call to ‘document` will load it fresh from the string.
-
#save ⇒ Object
Modifies the file in this file_system.
-
#set_file(file_io) ⇒ Object
Proxy through to FileSystem#set_file.
-
#write(data) ⇒ Object
Proxy through to FileSystem#write.
Constructor Details
#initialize(path, file_system) ⇒ File
Returns a new instance of File.
6 7 8 9 10 |
# File 'lib/bookbinder/file.rb', line 6 def initialize(path, file_system) @path = path @file_system = file_system @file_type = analyze_file_type(@path) end |
Instance Attribute Details
#file_type ⇒ Object
Returns the value of attribute file_type.
3 4 5 |
# File 'lib/bookbinder/file.rb', line 3 def file_type @file_type end |
#path ⇒ Object
Returns the value of attribute path.
3 4 5 |
# File 'lib/bookbinder/file.rb', line 3 def path @path end |
Instance Method Details
#copy_to(dest_file_system, dest_path) ⇒ Object
Writes the file to another file system.
64 65 66 67 68 69 |
# File 'lib/bookbinder/file.rb', line 64 def copy_to(dest_file_system, dest_path) @file_system.get_file(@path) { |file| dest_file_system.set_file(dest_path, file) } self end |
#dirty! ⇒ Object
Indicates that we should write out the document on save.
32 33 34 |
# File 'lib/bookbinder/file.rb', line 32 def dirty! @dirty = true end |
#dirty? ⇒ Boolean
Has the document changed, therefore we should write it out on save?
39 40 41 |
# File 'lib/bookbinder/file.rb', line 39 def dirty? @dirty ? true : false end |
#document(mode = 'rw') ⇒ Object
Gets a representation of the contents – if json, as a hash, if xml, as a Nokogiri document, etc. If mode includes ‘w’, then the document will be saved eventually.
17 18 19 20 |
# File 'lib/bookbinder/file.rb', line 17 def document(mode = 'rw') dirty! if mode.match(/w/) @document ||= string_to_document end |
#exists? ⇒ Boolean
Proxy through to FileSystem#exists?.
82 83 84 |
# File 'lib/bookbinder/file.rb', line 82 def exists? @file_system.exists?(path) end |
#get_file(mode = 'r', &blk) ⇒ Object
Proxy through to FileSystem#get_file.
103 104 105 |
# File 'lib/bookbinder/file.rb', line 103 def get_file(mode = 'r', &blk) @file_system.get_file(path, mode, &blk) end |
#media_type ⇒ Object
Guesses the mime-type (aka content-type, aka media-type) of this file based on its extension.
75 76 77 |
# File 'lib/bookbinder/file.rb', line 75 def media_type @media_type ||= Bookbinder::MediaType.of(@path) end |
#new_xml_document(&blk) ⇒ Object
23 24 25 26 27 |
# File 'lib/bookbinder/file.rb', line 23 def new_xml_document(&blk) @file_system.write(@path, '') unless dirty? dirty! @document = Bookbinder::DocumentProxy.new.build(&blk) end |
#read ⇒ Object
Proxy through to FileSystem#read.
89 90 91 |
# File 'lib/bookbinder/file.rb', line 89 def read @file_system.read(path) end |
#reset ⇒ Object
Resets state so that the next call to ‘document` will load it fresh from the string. Returns self for easy chaining.
55 56 57 58 59 |
# File 'lib/bookbinder/file.rb', line 55 def reset @document = nil @dirty = false self end |
#save ⇒ Object
Modifies the file in this file_system.
46 47 48 49 |
# File 'lib/bookbinder/file.rb', line 46 def save @file_system.write(@path, document_to_string) if dirty? reset end |
#set_file(file_io) ⇒ Object
Proxy through to FileSystem#set_file.
110 111 112 |
# File 'lib/bookbinder/file.rb', line 110 def set_file(file_io) @file_system.set_file(path, io) end |
#write(data) ⇒ Object
Proxy through to FileSystem#write.
96 97 98 |
# File 'lib/bookbinder/file.rb', line 96 def write(data) @file_system.write(path, data) end |