Class: HTAuth::File
- Inherits:
-
Object
- Object
- HTAuth::File
- Defined in:
- lib/htauth/file.rb
Direct Known Subclasses
Constant Summary collapse
- ALTER =
"alter"
- CREATE =
"create"
- STDOUT_FLAG =
"-"
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
Class Method Summary collapse
-
.open(filename, mode = ALTER) ⇒ Object
open a file yielding the the file object for use.
Instance Method Summary collapse
-
#contents ⇒ Object
return what should be the contents of the file.
-
#dirty! ⇒ Object
mark the file as dirty.
-
#dirty? ⇒ Boolean
return whether or not an alteration to the file has happened.
-
#initialize(filename, mode = ALTER) ⇒ File
constructor
Create or Alter a password file.
-
#load_entries ⇒ Object
load up entries, keep items in the same order and do not trim out any items in the file, like commented out lines or empty space.
-
#save! ⇒ Object
update the original file with the new contents.
Constructor Details
#initialize(filename, mode = ALTER) ⇒ File
Create or Alter a password file.
Altering a non-existent file is an error. Creating an existing file results in a truncation and overwrite of the existing file.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/htauth/file.rb', line 34 def initialize(filename, mode = ALTER) @filename = filename @mode = mode @dirty = false raise FileAccessError, "Invalid mode #{mode}" unless [ ALTER, CREATE ].include?(mode) if (filename != STDOUT_FLAG) and (mode == ALTER) and (not ::File.exist?(filename)) then raise FileAccessError, "Could not open passwd file #{filename} for reading." end begin @entries = {} @lines = [] load_entries if (@mode == ALTER) and (filename != STDOUT_FLAG) rescue => e raise FileAccessError, e. end end |
Instance Attribute Details
#file ⇒ Object (readonly)
Returns the value of attribute file.
12 13 14 |
# File 'lib/htauth/file.rb', line 12 def file @file end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
11 12 13 |
# File 'lib/htauth/file.rb', line 11 def filename @filename end |
Class Method Details
.open(filename, mode = ALTER) ⇒ Object
open a file yielding the the file object for use. The file is saved when the block exists, if the file has had alterations made.
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/htauth/file.rb', line 17 def open(filename, mode = ALTER) f = self.new(filename, mode) if block_given? begin yield f ensure f.save! if f and f.dirty? end end return f end |
Instance Method Details
#contents ⇒ Object
return what should be the contents of the file
82 83 84 85 86 87 88 |
# File 'lib/htauth/file.rb', line 82 def contents c = StringIO.new @lines.each do |l| c.puts l if l end c.string end |
#dirty! ⇒ Object
mark the file as dirty
60 61 62 |
# File 'lib/htauth/file.rb', line 60 def dirty! @dirty = true end |
#dirty? ⇒ Boolean
return whether or not an alteration to the file has happened
55 56 57 |
# File 'lib/htauth/file.rb', line 55 def dirty? @dirty end |
#load_entries ⇒ Object
load up entries, keep items in the same order and do not trim out any items in the file, like commented out lines or empty space
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/htauth/file.rb', line 92 def load_entries @lines = IO.readlines(@filename) @lines.each_with_index do |line,idx| if entry_klass.is_entry?(line) then entry = entry_klass.from_line(line) v = { 'entry' => entry, 'line_index' => idx } @entries[entry.key] = v end end end |
#save! ⇒ Object
update the original file with the new contents
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/htauth/file.rb', line 65 def save! begin case filename when STDOUT_FLAG $stdout.write(contents) else ::File.open(@filename,"w") do |f| f.write(contents) end end @dirty = false rescue => e raise FileAccessError, "Error saving file #{@filename} : #{e.}" end end |