Class: JSONdb::FileOps
- Inherits:
-
Object
- Object
- JSONdb::FileOps
- Includes:
- Logger
- Defined in:
- lib/jsondb/file_ops.rb
Instance Attribute Summary collapse
-
#contents ⇒ Object
Returns the value of attribute contents.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#filetype ⇒ Object
readonly
Returns the value of attribute filetype.
-
#folder ⇒ Object
readonly
Returns the value of attribute folder.
-
#new_file ⇒ Object
readonly
Returns the value of attribute new_file.
-
#writeable ⇒ Object
readonly
Returns the value of attribute writeable.
Instance Method Summary collapse
- #destroy ⇒ Object
-
#initialize(folder, filename, filetype, json = true) ⇒ FileOps
constructor
A new instance of FileOps.
- #read ⇒ Object
- #write ⇒ Object
- #write_line(line) ⇒ Object
Methods included from Logger
#allowed_log_level?, #log, #log_enabled?, #log_this?
Constructor Details
#initialize(folder, filename, filetype, json = true) ⇒ FileOps
Returns a new instance of FileOps.
11 12 13 14 15 16 17 |
# File 'lib/jsondb/file_ops.rb', line 11 def initialize(folder, filename, filetype, json = true) @folder = folder @filename = File.(File.join(@folder, filename)) @filetype = filetype @contents = "" @json = json end |
Instance Attribute Details
#contents ⇒ Object
Returns the value of attribute contents.
9 10 11 |
# File 'lib/jsondb/file_ops.rb', line 9 def contents @contents end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
7 8 9 |
# File 'lib/jsondb/file_ops.rb', line 7 def filename @filename end |
#filetype ⇒ Object (readonly)
Returns the value of attribute filetype.
7 8 9 |
# File 'lib/jsondb/file_ops.rb', line 7 def filetype @filetype end |
#folder ⇒ Object (readonly)
Returns the value of attribute folder.
7 8 9 |
# File 'lib/jsondb/file_ops.rb', line 7 def folder @folder end |
#new_file ⇒ Object (readonly)
Returns the value of attribute new_file.
7 8 9 |
# File 'lib/jsondb/file_ops.rb', line 7 def new_file @new_file end |
#writeable ⇒ Object (readonly)
Returns the value of attribute writeable.
7 8 9 |
# File 'lib/jsondb/file_ops.rb', line 7 def writeable @writeable end |
Instance Method Details
#destroy ⇒ Object
73 74 75 76 77 78 79 80 |
# File 'lib/jsondb/file_ops.rb', line 73 def destroy if File.exists?(@filename) File.delete(@filename) log("File '#{@filename}' deleted.", :info) else log("File '#{@filename}' does not exists.", :error) end end |
#read ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/jsondb/file_ops.rb', line 19 def read begin if File.exists?(@filename) file = File.open(@filename, 'r') raw = file.read @new_file = false log("File '#{@filename}' readed.", :debug) else raw = JSONdb.constants.default_file_content[@filetype] @new_file = true end if @json == true @contents = JSON.parse(raw) else @contents = raw end file.close if file log("File '#{@filename}' parsed.", :debug) return true rescue log("File '#{@filename}' could not read the file.", :error) return false end end |
#write ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/jsondb/file_ops.rb', line 44 def write begin file = File.open(@filename, 'w') if @json == true file.write(JSON.pretty_generate(@contents)) else file.write(@contents) end file.close @new_file = false log("File '#{@filename}' content updated.", :debug) return true rescue log("File '#{@filename}' could not open and write the file.", :error) return false end end |
#write_line(line) ⇒ Object
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/jsondb/file_ops.rb', line 62 def write_line(line) begin file = File.open(@filename, 'a+') file.write("#{line}\n") file.close @new_file = false rescue log("Couldn't write line on file '#{@filename}'.", :error) end end |