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(&block) ⇒ Object
-
#get ⇒ Array<String>
Return the list.
-
#include?(item) ⇒ Boolean
Is item in the list?.
-
#initialize(path, **opts) {|| ... } ⇒ 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, **opts) {|| ... } ⇒ ItemFile
Returns a new instance of ItemFile.
14 15 16 17 18 19 |
# File 'lib/svctl/item_file.rb', line 14 def initialize(path, **opts, &block) @path = path @lock_path = File.join(File.dirname(path), ".#{File.basename(path)}.lock") @opened = false open(**opts, &block) if block end |
Instance Attribute Details
#items ⇒ Object (readonly, protected)
Returns the value of attribute items.
85 86 87 |
# File 'lib/svctl/item_file.rb', line 85 def items @items end |
#lock_path ⇒ Object (readonly, protected)
Returns the value of attribute lock_path.
85 86 87 |
# File 'lib/svctl/item_file.rb', line 85 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
58 59 60 61 |
# File 'lib/svctl/item_file.rb', line 58 def <<(item) must_be_open! items << item unless items.include?(item) end |
#delete(item) ⇒ Object
Remove item from the list
65 66 67 68 |
# File 'lib/svctl/item_file.rb', line 65 def delete(item) must_be_open! items.delete(item) end |
#each(&block) ⇒ Object
44 45 46 47 |
# File 'lib/svctl/item_file.rb', line 44 def each(&block) must_be_open! items.each(&block) end |
#get ⇒ Array<String>
Return the list
39 40 41 42 |
# File 'lib/svctl/item_file.rb', line 39 def get must_be_open! items end |
#include?(item) ⇒ Boolean
Is item in the list?
51 52 53 54 |
# File 'lib/svctl/item_file.rb', line 51 def include?(item) must_be_open! items.include?(item) end |
#must_be_open! ⇒ Object (protected)
87 88 89 |
# File 'lib/svctl/item_file.rb', line 87 def must_be_open! fail 'file list not open' unless open? end |
#open {|| ... } ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/svctl/item_file.rb', line 22 def open sync do @items = [] parse yield(self) save end nil end |
#open? ⇒ Boolean
33 34 35 |
# File 'lib/svctl/item_file.rb', line 33 def open? @opened end |
#parse ⇒ Object (protected)
103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/svctl/item_file.rb', line 103 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 end |
#save ⇒ Object
Save the list
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/svctl/item_file.rb', line 71 def save must_be_open! if items.empty? unlink_if_exists(path) return end regenerate_file(path, 0644) do |new| items.each { |item| new.puts(item) } end end |
#sync ⇒ Object (protected)
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/svctl/item_file.rb', line 91 def sync Filelock(lock_path) do @opened = true begin yield ensure @opened = false end end end |