Roll  Roll::Ledger

[Validate]
Generated with WebRI Redfish 1.2.1

Ledger class

Public Class Methods

new() click to toggle source
    # File lib/roll/ledger.rb, line 13
13:     def initialize
14:       @index = Hash.new{|h,k| h[k] = []}
15: 
16:       @environment = Environment.new
17: 
18:       @environment.each do |name, paths|
19:         paths.each do |path|
20:           unless File.directory?(path)
21:             warn "roll: invalid path for #{name} -- #{path}"
22:             next
23:           end
24:           lib = Library.new(path, name)
25:           @index[name] << lib if lib.active?
26:         end
27:       end
28: 
29:       @load_stack   = []
30:       @load_monitor = Hash.new{ |h,k| h[k]=[] }
31:     end

Public Instance Methods

[](name) click to toggle source
    # File lib/roll/ledger.rb, line 39
39:     def [](name)
40:       @index[name]
41:     end
[]=(name, value) click to toggle source
    # File lib/roll/ledger.rb, line 44
44:     def []=(name, value)
45:       @index[name] = value
46:     end
acquire(file, opts={}) click to toggle source

Use acquire to use Roll-style loading. This first looks for a specific library via ’:’. If ’:’ is not present it then tries the current library. Failing that it fallsback to Ruby itself.

  acquire('facets:string/margin')

To “load” the library, rather than “require” it set the :load option to true.

  acquire('facets:string/margin', :load=>true)
     # File lib/roll/ledger.rb, line 137
137:     def acquire(file, opts={})
138:       if file.index(':') # a specific library
139:         name, file = file.split(':')
140:         lib = Library.open(name)
141:       else # try the current library
142:         cur = load_stack.last
143:         if cur && cur.include?(file)
144:           lib = cur
145:         elsif !file.index('/') # is this a library name?
146:           if cur = Library.instance(file)
147:             lib  = cur
148:             file = lib.default # default file to load
149:           end
150:         end
151:       end
152:       if opts[:load]
153:         lib ? lib.load(file) : original_load(file)
154:       else
155:         lib ? lib.require(file) : original_require(file)
156:       end
157:     end
constrain(lib) click to toggle source
     # File lib/roll/ledger.rb, line 160
160:     def constrain(lib)
161:       cmp = self[lib.name]
162:       if Array === cmp
163:         self[lib.name] = lib
164:       else
165:         if lib.version != cmp.version
166:           raise VersionError
167:         end
168:       end
169:     end
each(&block) click to toggle source
    # File lib/roll/ledger.rb, line 59
59:     def each(&block)
60:       @index.each(&block)
61:     end
enironment() click to toggle source
    # File lib/roll/ledger.rb, line 34
34:     def enironment
35:       @environment
36:     end
include?(name) click to toggle source
    # File lib/roll/ledger.rb, line 49
49:     def include?(name)
50:       @index.include?(name)
51:     end
load(path, wrap=nil) click to toggle source
     # File lib/roll/ledger.rb, line 111
111:     def load(path, wrap=nil)
112:       lib, file = *match(path, false)
113:       if lib && file
114:         constrain(lib)
115:         lib.load_absolute(file, wrap)
116:       else
117:         begin
118:           original_load(path, wrap)
119:         rescue LoadError => load_error
120:           raise clean_backtrace(load_error)
121:         end
122:       end
123:     end
load_monitor() click to toggle source

NOTE: Not used yet.

    # File lib/roll/ledger.rb, line 74
74:     def load_monitor
75:       @load_monitor
76:     end
load_stack() click to toggle source
    # File lib/roll/ledger.rb, line 69
69:     def load_stack
70:       @load_stack
71:     end
names() click to toggle source
    # File lib/roll/ledger.rb, line 54
54:     def names
55:       @index.keys
56:     end
require(path) click to toggle source
     # File lib/roll/ledger.rb, line 84
 84:     def require(path)
 85:       #begin
 86:       #  original_require(path)
 87:       #rescue LoadError => load_error
 88:       #  lib, file = *match(path)
 89:       #  if lib && file
 90:       #    constrain(lib)
 91:       #    lib.require_absolute(file)
 92:       #  else
 93:       #    raise clean_backtrace(load_error)
 94:       #  end
 95:       #end
 96:       lib, file = *match(path)
 97:       if lib && file
 98:         constrain(lib)
 99:         lib.require_absolute(file)
100:       else
101:         begin
102:           original_require(path)
103:         rescue LoadError => load_error
104:           raise clean_backtrace(load_error)
105:         end
106:       end
107: 
108:     end
size() click to toggle source
    # File lib/roll/ledger.rb, line 64
64:     def size
65:       @index.size
66:     end

Private Instance Methods

clean_backtrace(error) click to toggle source
     # File lib/roll/ledger.rb, line 243
243:     def clean_backtrace(error)
244:       if $DEBUG
245:         error
246:       else
247:         bt = error.backtrace
248:         bt = bt.reject{ |e| /roll/ =~ e }
249:         error.set_backtrace(bt)
250:         error
251:       end
252:     end
match(path, suffix=true) click to toggle source

Find require matches.

     # File lib/roll/ledger.rb, line 174
174:     def match(path, suffix=true)
175:       path = path.to_s
176: 
177:       return nil if /^\// =~ path  # absolute path
178: 
179:       if path.index(':') # a specified library
180:         name, path = path.split(':')
181:         lib  = Library.open(name)
182:         if lib.active?
183:           file = lib.find(path, suffix)
184:           return lib, file
185:         end
186:       end
187: 
188:       matches = []
189: 
190:       # try the load stack first
191:       load_stack.reverse_each do |lib|
192:         if file = lib.find(path, suffix)
193:           return [lib, file] unless $VERBOSE
194:           matches << [lib, file]
195:         end
196:       end
197: 
198:       # TODO: Perhaps the selected and unselected should be kept in separate lists?
199:       unselected, selected = *@index.partition{ |name, libs| Array === libs }
200: 
201:       selected.each do |(name, lib)|
202:         if file = lib.find(path, suffix)
203:           #matches << [lib, file]
204:           #return matches.first unless $VERBOSE
205:           return [lib, file] unless $VERBOSE
206:           matches << [lib, file]
207:         end
208:       end
209: 
210:       unselected.each do |(name, libs)|
211:         pos = []
212:         libs.each do |lib|
213:           if file = lib.find(path, suffix)
214:             pos << [lib, file]
215:           end
216:         end
217:         unless pos.empty?
218:           latest = pos.sort{ |a,b| b[0].version <=> a[0].version }.first
219:           return latest unless $VERBOSE
220:           matches << latest
221:           #return matches.first unless $VERBOSE
222:         end
223:       end
224: 
225:       matches.uniq!
226: 
227:       if matches.size > 1
228:         warn_multiples(path, matches)
229:       end
230: 
231:       matches.first
232:     end
warn_multiples(path, matches) click to toggle source
     # File lib/roll/ledger.rb, line 235
235:     def warn_multiples(path, matches)
236:       warn "multiple matches for same request -- #{path}"
237:       matches.each do |lib, file|
238:         warn "  #{file}"
239:       end
240:     end

Disabled; run with --debug to generate this.