Class: Roll::Locals
Constant Summary collapse
- DIR =
XDG.config_home, 'roll', 'locals'
Class Method Summary collapse
-
.list ⇒ Object
List of available environments.
Instance Method Summary collapse
- #append(path, depth = 3) ⇒ Object
- #delete(path) ⇒ Object
- #each(&block) ⇒ Object
- #file ⇒ Object
-
#initialize(name = nil) ⇒ Locals
constructor
A new instance of Locals.
- #name ⇒ Object
- #reload ⇒ Object
- #save ⇒ Object
- #size ⇒ Object
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
.list ⇒ Object
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.(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 |
#file ⇒ Object
35 36 37 |
# File 'lib/roll/locals.rb', line 35 def file @file ||= File.join(DIR, name) end |
#name ⇒ Object
30 31 32 |
# File 'lib/roll/locals.rb', line 30 def name @name end |
#reload ⇒ Object
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 |
#save ⇒ Object
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 |
#size ⇒ Object
64 65 66 |
# File 'lib/roll/locals.rb', line 64 def size @table.size end |