Class: Roll::Ledger

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

Overview

Ledger class

Instance Method Summary collapse

Constructor Details

#initializeLedger

Returns a new instance of Ledger.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/roll/ledger.rb', line 13

def initialize
  @index = Hash.new{|h,k| h[k] = []}

  @environment = Environment.new

  @environment.each do |name, paths|
    paths.each do |path|
      unless File.directory?(path)
        warn "roll: invalid path for #{name} -- #{path}"
        next
      end
      lib = Library.new(path, name)
      @index[name] << lib if lib.active?
    end
  end

  @load_stack   = []
  @load_monitor = Hash.new{ |h,k| h[k]=[] }
end

Instance Method Details

#[](name) ⇒ Object



39
40
41
# File 'lib/roll/ledger.rb', line 39

def [](name)
  @index[name]
end

#[]=(name, value) ⇒ Object



44
45
46
# File 'lib/roll/ledger.rb', line 44

def []=(name, value)
  @index[name] = value
end

#acquire(file, opts = {}) ⇒ Object

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)


137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/roll/ledger.rb', line 137

def acquire(file, opts={})
  if file.index(':') # a specific library
    name, file = file.split(':')
    lib = Library.open(name)
  else # try the current library
    cur = load_stack.last
    if cur && cur.include?(file)
      lib = cur
    elsif !file.index('/') # is this a library name?
      if cur = Library.instance(file)
        lib  = cur
        file = lib.default # default file to load
      end
    end
  end
  if opts[:load]
    lib ? lib.load(file) : original_load(file)
  else
    lib ? lib.require(file) : original_require(file)
  end
end

#constrain(lib) ⇒ Object



160
161
162
163
164
165
166
167
168
169
# File 'lib/roll/ledger.rb', line 160

def constrain(lib)
  cmp = self[lib.name]
  if Array === cmp
    self[lib.name] = lib
  else
    if lib.version != cmp.version
      raise VersionError
    end
  end
end

#each(&block) ⇒ Object



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

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

#enironmentObject



34
35
36
# File 'lib/roll/ledger.rb', line 34

def enironment
  @environment
end

#include?(name) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/roll/ledger.rb', line 49

def include?(name)
  @index.include?(name)
end

#load(path, wrap = nil) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/roll/ledger.rb', line 111

def load(path, wrap=nil)
  lib, file = *match(path, false)
  if lib && file
    constrain(lib)
    lib.load_absolute(file, wrap)
  else
    begin
      original_load(path, wrap)
    rescue LoadError => load_error
      raise clean_backtrace(load_error)
    end
  end
end

#load_monitorObject

NOTE: Not used yet.



74
75
76
# File 'lib/roll/ledger.rb', line 74

def load_monitor
  @load_monitor
end

#load_stackObject



69
70
71
# File 'lib/roll/ledger.rb', line 69

def load_stack
  @load_stack
end

#namesObject



54
55
56
# File 'lib/roll/ledger.rb', line 54

def names
  @index.keys
end

#require(path) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/roll/ledger.rb', line 84

def require(path)
  #begin
  #  original_require(path)
  #rescue LoadError => load_error
  #  lib, file = *match(path)
  #  if lib && file
  #    constrain(lib)
  #    lib.require_absolute(file)
  #  else
  #    raise clean_backtrace(load_error)
  #  end
  #end
  lib, file = *match(path)
  if lib && file
    constrain(lib)
    lib.require_absolute(file)
  else
    begin
      original_require(path)
    rescue LoadError => load_error
      raise clean_backtrace(load_error)
    end
  end

end

#sizeObject



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

def size
  @index.size
end