Class: SvCtl::ItemFile

Inherits:
Object
  • Object
show all
Includes:
OsCtl::Lib::Utils::File
Defined in:
lib/svctl/item_file.rb

Overview

Manage a file-based list of strings

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) {|| ... } ⇒ ItemFile

Returns a new instance of ItemFile.

Parameters:

  • path (String)

    path to a file where the list is stored

Yield Parameters:



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

#itemsObject (readonly, protected)

Returns the value of attribute items.



90
91
92
# File 'lib/svctl/item_file.rb', line 90

def items
  @items
end

#lock_pathObject (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

#pathString (readonly)

Returns:

  • (String)


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

Parameters:

  • item (String)


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

Parameters:

  • item (String)


69
70
71
72
# File 'lib/svctl/item_file.rb', line 69

def delete(item)
  must_be_open!
  items.delete(item)
end

#eachObject



48
49
50
51
# File 'lib/svctl/item_file.rb', line 48

def each(&)
  must_be_open!
  items.each(&)
end

#getArray<String>

Return the list

Returns:

  • (Array<String>)


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?

Parameters:

  • item (String)

Returns:

  • (Boolean)


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

Yield Parameters:



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

Returns:

  • (Boolean)


37
38
39
# File 'lib/svctl/item_file.rb', line 37

def open?
  @opened
end

#parseObject (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

#saveObject

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

#syncObject (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