Class: Groundskeeper::Document
- Inherits:
-
Object
- Object
- Groundskeeper::Document
- Defined in:
- lib/groundskeeper/document.rb
Overview
Wraps files.
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
-
#append_to_file(expression, value) ⇒ Object
Inserts the provided value after the expression in place in the file.
- #exists? ⇒ Boolean
-
#gsub_file(expression, value) ⇒ Object
Replaces the expression with the provided value in place in the file.
-
#initialize(path) ⇒ Document
constructor
A new instance of Document.
-
#match(expression_with_capture) ⇒ Object
Returns the first captured expression or nil if it is not found.
-
#prepend_file(value) ⇒ Object
Inserts the provided value at the beginning of the file.
- #read ⇒ Object
- #write_file(content) ⇒ Object
Constructor Details
#initialize(path) ⇒ Document
Returns a new instance of Document.
8 9 10 |
# File 'lib/groundskeeper/document.rb', line 8 def initialize(path) @path = File.(path) end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
6 7 8 |
# File 'lib/groundskeeper/document.rb', line 6 def path @path end |
Instance Method Details
#append_to_file(expression, value) ⇒ Object
Inserts the provided value after the expression in place in the file.
32 33 34 35 36 37 |
# File 'lib/groundskeeper/document.rb', line 32 def append_to_file(expression, value) content = read.force_encoding("utf-8") match = content.match(expression)[0].force_encoding("utf-8") content.gsub! expression, match + value write_file content end |
#exists? ⇒ Boolean
12 13 14 |
# File 'lib/groundskeeper/document.rb', line 12 def exists? File.exist? path end |
#gsub_file(expression, value) ⇒ Object
Replaces the expression with the provided value in place in the file.
25 26 27 28 29 |
# File 'lib/groundskeeper/document.rb', line 25 def gsub_file(expression, value) content = read content.gsub! expression, value write_file content end |
#match(expression_with_capture) ⇒ Object
Returns the first captured expression or nil if it is not found.
17 18 19 20 21 22 |
# File 'lib/groundskeeper/document.rb', line 17 def match(expression_with_capture) content = read match = content.match(expression_with_capture) match[1] if match end |
#prepend_file(value) ⇒ Object
Inserts the provided value at the beginning of the file.
40 41 42 43 |
# File 'lib/groundskeeper/document.rb', line 40 def prepend_file(value) content = read write_file(value + content) end |
#read ⇒ Object
49 50 51 52 53 |
# File 'lib/groundskeeper/document.rb', line 49 def read return "" unless exists? File.binread(path) end |
#write_file(content) ⇒ Object
45 46 47 |
# File 'lib/groundskeeper/document.rb', line 45 def write_file(content) File.open(path, "wb") { |file| file.write(content) } end |