Class: SvCtl::ItemFile
- Inherits:
-
Object
- Object
- SvCtl::ItemFile
- Includes:
- OsCtl::Lib::Utils::File
- Defined in:
- lib/svctl/item_file.rb
Overview
Manage a file-based list of strings
Instance Attribute Summary collapse
-
#items ⇒ Object
readonly
protected
Returns the value of attribute items.
-
#lock_path ⇒ Object
readonly
protected
Returns the value of attribute lock_path.
- #path ⇒ String readonly
Instance Method Summary collapse
-
#<<(item) ⇒ Object
Add item to the list.
-
#delete(item) ⇒ Object
Remove item from the list.
- #each ⇒ Object
-
#get ⇒ Array<String>
Return the list.
-
#include?(item) ⇒ Boolean
Is item in the list?.
-
#initialize(path) {|| ... } ⇒ ItemFile
constructor
A new instance of ItemFile.
- #must_be_open! ⇒ Object protected
- #open {|| ... } ⇒ Object
- #open? ⇒ Boolean
- #parse ⇒ Object protected
-
#save ⇒ Object
Save the list.
- #sync ⇒ Object protected
Constructor Details
#initialize(path) {|| ... } ⇒ ItemFile
Returns a new instance of ItemFile.
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/svctl/item_file.rb', line 14 def initialize(path, **, &block) @path = path @lock_path = File.join(File.dirname(path), ".#{File.basename(path)}.lock") @opened = false # rubocop:disable Security/Open # rubocop thinks this is Kernel#open open(**, &block) if block # rubocop:enable Security/Open end |
Instance Attribute Details
#items ⇒ Object (readonly, protected)
Returns the value of attribute items.
90 91 92 |
# File 'lib/svctl/item_file.rb', line 90 def items @items end |
#lock_path ⇒ Object (readonly, protected)
Returns the value of attribute lock_path.
90 91 92 |
# File 'lib/svctl/item_file.rb', line 90 def lock_path @lock_path end |
#path ⇒ String (readonly)
10 11 12 |
# File 'lib/svctl/item_file.rb', line 10 def path @path end |
Instance Method Details
#<<(item) ⇒ Object
Add item to the list
62 63 64 65 |
# File 'lib/svctl/item_file.rb', line 62 def <<(item) must_be_open! items << item unless items.include?(item) end |
#delete(item) ⇒ Object
Remove item from the list
69 70 71 72 |
# File 'lib/svctl/item_file.rb', line 69 def delete(item) must_be_open! items.delete(item) end |
#each ⇒ Object
48 49 50 51 |
# File 'lib/svctl/item_file.rb', line 48 def each(&) must_be_open! items.each(&) end |
#get ⇒ Array<String>
Return the list
43 44 45 46 |
# File 'lib/svctl/item_file.rb', line 43 def get must_be_open! items end |
#include?(item) ⇒ Boolean
Is item in the list?
55 56 57 58 |
# File 'lib/svctl/item_file.rb', line 55 def include?(item) must_be_open! items.include?(item) end |
#must_be_open! ⇒ Object (protected)
92 93 94 |
# File 'lib/svctl/item_file.rb', line 92 def must_be_open! raise 'file list not open' unless open? end |
#open {|| ... } ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/svctl/item_file.rb', line 26 def open sync do @items = [] parse yield(self) save end nil end |
#open? ⇒ Boolean
37 38 39 |
# File 'lib/svctl/item_file.rb', line 37 def open? @opened end |
#parse ⇒ Object (protected)
108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/svctl/item_file.rb', line 108 def parse File.open(path) do |f| f.each_line do |line| stripped = line.strip next if stripped.empty? items << stripped end end rescue Errno::ENOENT # ignore end |
#save ⇒ Object
Save the list
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/svctl/item_file.rb', line 75 def save must_be_open! if items.empty? unlink_if_exists(path) return end regenerate_file(path, 0o644) do |new| items.each { |item| new.puts(item) } end end |
#sync ⇒ Object (protected)
96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/svctl/item_file.rb', line 96 def sync Filelock(lock_path) do @opened = true begin yield ensure @opened = false end end end |