Class: Roll::Environment::Index
- Inherits:
-
Object
- Object
- Roll::Environment::Index
- Includes:
- Enumerable
- Defined in:
- lib/roll/environment.rb
Overview
Index tracks the name and location of each library in an environment. – TODO: Using a hash table means un-order, fix? ++
Instance Method Summary collapse
-
#each(&block) ⇒ Object
Look through the environment table.
-
#file ⇒ Object
Environment file (full-path).
-
#initialize(name = nil) ⇒ Index
constructor
Instantiate environment.
-
#name ⇒ Object
Current ledger name.
-
#reload ⇒ Object
Load the environment file.
- #reset(index) ⇒ Object
-
#save ⇒ Object
Save environment file.
-
#size ⇒ Object
Number of entries.
- #to_h ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(name = nil) ⇒ Index
Instantiate environment.
86 87 88 89 90 |
# File 'lib/roll/environment.rb', line 86 def initialize(name=nil) @name = name || Environment.current @table = Hash.new{ |h,k| h[k] = [] } reload end |
Instance Method Details
#each(&block) ⇒ Object
Look through the environment table.
120 121 122 |
# File 'lib/roll/environment.rb', line 120 def each(&block) @table.each(&block) end |
#file ⇒ Object
Environment file (full-path).
98 99 100 |
# File 'lib/roll/environment.rb', line 98 def file @file ||= ::Config.find_config('roll', name, 'index').first end |
#name ⇒ Object
Current ledger name.
93 94 95 |
# File 'lib/roll/environment.rb', line 93 def name @name end |
#reload ⇒ Object
Load the environment file.
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/roll/environment.rb', line 103 def reload if file && File.exist?(file) File.readlines(file).each do |line| line = line.strip next if line.empty? name, path = *line.split(/\s+/) @table[name.strip] << path.strip end end end |
#reset(index) ⇒ Object
115 116 117 |
# File 'lib/roll/environment.rb', line 115 def reset(index) @table = index end |
#save ⇒ Object
Save environment file.
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/roll/environment.rb', line 147 def save out = to_s #max = @table.map{ |name, paths| name.size }.max #@table.map do |name, paths| # paths.each do |path| # out << "%-#{max}s %s\n" % [name, path] # end #end file = File.join(::Config::CONFIG_HOME, 'roll', name, 'index') if File.exist?(file) data = File.read(file) if out != data File.open(file, 'w'){ |f| f << out } #puts "updated: #{name}" true else #puts "current: #{name}" false end else dir = File.dirname(file) FileUtils.mkdir_p(dir) unless File.exist?(dir) File.open(file, 'w'){ |f| f << out } #puts "created: #{name}" true end @file = file end |
#size ⇒ Object
Number of entries.
125 126 127 |
# File 'lib/roll/environment.rb', line 125 def size @table.size end |
#to_h ⇒ Object
130 131 132 |
# File 'lib/roll/environment.rb', line 130 def to_h @table.dup end |
#to_s ⇒ Object
135 136 137 138 139 140 141 142 143 144 |
# File 'lib/roll/environment.rb', line 135 def to_s out = "" max = @table.map{ |name, paths| name.size }.max @table.map do |name, paths| paths.each do |path| out << "%-#{max}s %s\n" % [name, path] end end out end |