Class: ClWiki::File
- Inherits:
-
Object
- Object
- ClWiki::File
- Defined in:
- lib/cl_wiki/file.rb
Constant Summary collapse
- FILE_EXT =
'.txt'
Instance Attribute Summary collapse
-
#client_last_read_mod_time ⇒ Object
Returns the value of attribute client_last_read_mod_time.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#mod_time_at_last_read ⇒ Object
readonly
Returns the value of attribute mod_time_at_last_read.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#owner ⇒ Object
readonly
Returns the value of attribute owner.
Instance Method Summary collapse
- #apply_metadata ⇒ Object
- #content ⇒ Object
- #content=(new_content) ⇒ Object
- #content_encrypted? ⇒ Boolean
- #default_content ⇒ Object
- #delete ⇒ Object
- #do_not_encrypt_content! ⇒ Object
- #encrypt_content! ⇒ Object
- #file_exists? ⇒ Boolean
- #full_path_and_name ⇒ Object
- #has_default_content? ⇒ Boolean
-
#initialize(page_name, wiki_root_path, auto_create: true, owner: PublicUser.new) ⇒ File
constructor
A new instance of File.
- #read_file ⇒ Object
- #read_metadata(lines) ⇒ Object
- #write_to_file(content, check_mod_time = true) ⇒ Object
Constructor Details
#initialize(page_name, wiki_root_path, auto_create: true, owner: PublicUser.new) ⇒ File
Returns a new instance of File.
13 14 15 16 17 18 19 20 21 |
# File 'lib/cl_wiki/file.rb', line 13 def initialize(page_name, wiki_root_path, auto_create: true, owner: PublicUser.new) @wiki_root_path = wiki_root_path @owner = owner @name = ::File.basename(ClWiki::Util.convert_to_native_path(page_name)) @metadata = Metadata.new if auto_create file_exists? ? read_file : write_to_file(default_content, false) end end |
Instance Attribute Details
#client_last_read_mod_time ⇒ Object
Returns the value of attribute client_last_read_mod_time.
11 12 13 |
# File 'lib/cl_wiki/file.rb', line 11 def client_last_read_mod_time @client_last_read_mod_time end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
10 11 12 |
# File 'lib/cl_wiki/file.rb', line 10 def @metadata end |
#mod_time_at_last_read ⇒ Object (readonly)
Returns the value of attribute mod_time_at_last_read.
10 11 12 |
# File 'lib/cl_wiki/file.rb', line 10 def mod_time_at_last_read @mod_time_at_last_read end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
10 11 12 |
# File 'lib/cl_wiki/file.rb', line 10 def name @name end |
#owner ⇒ Object (readonly)
Returns the value of attribute owner.
10 11 12 |
# File 'lib/cl_wiki/file.rb', line 10 def owner @owner end |
Instance Method Details
#apply_metadata ⇒ Object
83 84 85 86 |
# File 'lib/cl_wiki/file.rb', line 83 def @mod_time_at_last_read = Time.parse(@metadata['mtime']) if @metadata.has? 'mtime' ensure_same_owner! end |
#content ⇒ Object
43 44 45 |
# File 'lib/cl_wiki/file.rb', line 43 def content @contents end |
#content=(new_content) ⇒ Object
47 48 49 |
# File 'lib/cl_wiki/file.rb', line 47 def content=(new_content) write_to_file(new_content) end |
#content_encrypted? ⇒ Boolean
88 89 90 |
# File 'lib/cl_wiki/file.rb', line 88 def content_encrypted? @metadata['encrypted'] == 'true' end |
#default_content ⇒ Object
27 28 29 |
# File 'lib/cl_wiki/file.rb', line 27 def default_content "Describe #{@name} here." end |
#delete ⇒ Object
31 32 33 |
# File 'lib/cl_wiki/file.rb', line 31 def delete ::File.delete(full_path_and_name) if file_exists? end |
#do_not_encrypt_content! ⇒ Object
98 99 100 |
# File 'lib/cl_wiki/file.rb', line 98 def do_not_encrypt_content! @metadata['encrypted'] = 'false' end |
#encrypt_content! ⇒ Object
92 93 94 95 96 |
# File 'lib/cl_wiki/file.rb', line 92 def encrypt_content! raise "Owner <#{@owner.name}> cannot encrypt" unless @owner.can_encrypt? @metadata['encrypted'] = 'true' end |
#file_exists? ⇒ Boolean
35 36 37 |
# File 'lib/cl_wiki/file.rb', line 35 def file_exists? FileTest.exist?(full_path_and_name) end |
#full_path_and_name ⇒ Object
39 40 41 |
# File 'lib/cl_wiki/file.rb', line 39 def full_path_and_name ::File.(@name + FILE_EXT, @wiki_root_path) end |
#has_default_content? ⇒ Boolean
23 24 25 |
# File 'lib/cl_wiki/file.rb', line 23 def has_default_content? @contents.to_s == default_content end |
#read_file ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/cl_wiki/file.rb', line 64 def read_file @mod_time_at_last_read = ::File.mtime(full_path_and_name) raw_bytes = ::File.binread(full_path_and_name) @metadata, raw_content = Metadata.split_file_contents(raw_bytes) @contents = if content_encrypted? @owner.lockbox.decrypt_str(raw_content) else raw_content.force_encoding('UTF-8') end end |
#read_metadata(lines) ⇒ Object
79 80 81 |
# File 'lib/cl_wiki/file.rb', line 79 def (lines) @metadata = Metadata.new(lines) end |
#write_to_file(content, check_mod_time = true) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/cl_wiki/file.rb', line 51 def write_to_file(content, check_mod_time = true) do_mod_time_check if check_mod_time file_contents = String.new.tap do |s| s << @metadata.to_s s << (content_encrypted? ? @owner.lockbox.encrypt(content) : content) end ::File.binwrite(full_path_and_name, file_contents) ::File.utime(@metadata['mtime'], @metadata['mtime'], full_path_and_name) read_file end |