Class: Roll::Locals

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/roll/locals.rb

Constant Summary collapse

DIR =
XDG.config_home, 'roll', 'locals'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil) ⇒ Locals

Returns a new instance of Locals.



24
25
26
27
# File 'lib/roll/locals.rb', line 24

def initialize(name=nil)
  @name = name || self.class.current
  reload
end

Class Method Details

.listObject

List of available environments.



17
18
19
20
21
# File 'lib/roll/locals.rb', line 17

def self.list
  Dir[File.join(DIR, '*')].map do |file|
    File.basename(file)
  end
end

Instance Method Details

#append(path, depth = 3) ⇒ Object



69
70
71
72
73
74
# File 'lib/roll/locals.rb', line 69

def append(path, depth=3)
  path  = File.expand_path(path)
  depth = (depth || 3).to_i
  @table = @table.reject{ |(p, d)| path == p }
  @table.push([path, depth])
end

#delete(path) ⇒ Object



77
78
79
# File 'lib/roll/locals.rb', line 77

def delete(path)
  @table.reject!{ |p,d| path == p }
end

#each(&block) ⇒ Object



59
60
61
# File 'lib/roll/locals.rb', line 59

def each(&block)
  @table.each(&block)
end

#fileObject



35
36
37
# File 'lib/roll/locals.rb', line 35

def file
  @file ||= File.join(DIR, name)
end

#nameObject



30
31
32
# File 'lib/roll/locals.rb', line 30

def name
  @name
end

#reloadObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/roll/locals.rb', line 40

def reload
  t = []
  if File.exist?(file)
    lines = File.readlines(file)
    lines.each do |line|
      line = line.strip
      path, depth = *line.split(/\s+/)
      next if line =~ /^\s*$/  # blank
      next if line =~ /^\#/    # comment
      dir, depth = *line.split(/\s+/)
      t << [path, (depth || 3).to_i]
    end
  else
    t = []
  end
  @table = t
end

#saveObject



82
83
84
85
86
87
88
89
90
91
# File 'lib/roll/locals.rb', line 82

def save
  out = @table.map do |(path, depth)|
    "#{path}   #{depth}"
  end
  dir = File.dirname(file)
  FileUtils.mkdir_p(dir) unless File.exist?(dir)
  File.open(file, 'w') do |f|
    f <<  out.join("\n")
  end
end

#sizeObject



64
65
66
# File 'lib/roll/locals.rb', line 64

def size
  @table.size
end