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, **opts) {|| ... } ⇒ 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
# 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

#itemsObject (readonly, protected)

Returns the value of attribute items.



85
86
87
# File 'lib/svctl/item_file.rb', line 85

def items
  @items
end

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

#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)


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

Parameters:

  • item (String)


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

#getArray<String>

Return the list

Returns:

  • (Array<String>)


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?

Parameters:

  • item (String)

Returns:

  • (Boolean)


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

Yield Parameters:



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

Returns:

  • (Boolean)


33
34
35
# File 'lib/svctl/item_file.rb', line 33

def open?
  @opened
end

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

#saveObject

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

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