Class: Roll::Library
- Inherits:
-
Object
- Object
- Roll::Library
- Defined in:
- lib/roll/ledger.rb,
lib/roll/library.rb
Overview
Library class
Constant Summary collapse
- SUFFIXES =
SUFFIXES = [”, ‘.rb’, ‘.rbw’, ‘.so’, ‘.bundle’, ‘.dll’, ‘.sl’, ‘.jar’]
['', '.rb', '.rbw', '.so', '.bundle', '.dll', '.sl', '.jar']
- SUFFIX_PATTERN =
"{#{SUFFIXES.join(',')}}"
Class Method Summary collapse
-
.[](name, constraint = nil) ⇒ Object
A shortcut for #instance.
-
.environment ⇒ Object
Current environment.
-
.instance(name, constraint = nil) ⇒ Object
Get an instance of a library by name, or name and version.
- .ledger ⇒ Object
-
.list ⇒ Object
List of library names.
- .load(path, wrap = nil) ⇒ Object
-
.load_monitor ⇒ Object
NOTE: Not used yet.
- .load_stack ⇒ Object
-
.open(name, constraint = nil) {|lib| ... } ⇒ Object
Same as #instance but will raise and error if the library is not found.
- .require(path) ⇒ Object
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Compare by version.
- #active? ⇒ Boolean
-
#bindir ⇒ Object
Location of executable.
-
#bindir? ⇒ Boolean
Is there a
bin/
location?. -
#confdir ⇒ Object
Location of library system configuration files.
-
#confdir? ⇒ Boolean
Is there a
etc/
location?metadata.name. -
#datadir ⇒ Object
Location of library shared data directory.
-
#datadir? ⇒ Boolean
Is there a
data/
location?. -
#find(file, suffix = true) ⇒ Object
Standard loadpath search.
-
#include?(file, suffix = true) ⇒ Boolean
Does this library have a matching
file
? If so, the full-path of the file is returned. -
#initialize(location, name = nil) ⇒ Library
constructor
A new instance of Library.
-
#inspect ⇒ Object
Inspection.
- #load(file, wrap = nil) ⇒ Object
- #load_absolute(file, wrap = nil) ⇒ Object
- #loadpath ⇒ Object
- #location ⇒ Object
-
#metadata ⇒ Object
Access to secondary metadata.
- #name ⇒ Object
- #released ⇒ Object
- #require(file) ⇒ Object
-
#require_absolute(file) ⇒ Object
NOT SURE ABOUT USING THIS.
- #requires ⇒ Object
- #to_s ⇒ Object
- #verify ⇒ Object
- #version ⇒ Object
Constructor Details
#initialize(location, name = nil) ⇒ Library
Returns a new instance of Library.
74 75 76 77 |
# File 'lib/roll/library.rb', line 74 def initialize(location, name=nil) @location = location @name = name end |
Class Method Details
.[](name, constraint = nil) ⇒ Object
A shortcut for #instance.
57 58 59 |
# File 'lib/roll/library.rb', line 57 def self.[](name, constraint=nil) instance(name, constraint) end |
.environment ⇒ Object
Current environment
267 268 269 |
# File 'lib/roll/ledger.rb', line 267 def environment ledger.environment end |
.instance(name, constraint = nil) ⇒ Object
Get an instance of a library by name, or name and version. Libraries are singleton, so once loaded the same object is always returned.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/roll/library.rb', line 26 def self.instance(name, constraint=nil) name = name.to_s #raise "no library -- #{name}" unless ledger.include?(name) return nil unless ledger.include?(name) library = ledger[name] if Library===library if constraint # TODO: it's okay if constraint fits current raise VersionConflict, "previously selected version -- #{ledger[name].version}" else library end else # library is an array of versions if constraint compare = Version.constraint_lambda(constraint) library = library.select(&compare).max else library = library.max end unless library raise VersionError, "no library version -- #{name} #{constraint}" end #ledger[name] = library #library.activate return library end end |
.ledger ⇒ Object
262 263 264 |
# File 'lib/roll/ledger.rb', line 262 def ledger @ledger ||= Ledger.new end |
.list ⇒ Object
List of library names.
272 273 274 |
# File 'lib/roll/ledger.rb', line 272 def list ledger.names end |
.load(path, wrap = nil) ⇒ Object
282 283 284 |
# File 'lib/roll/ledger.rb', line 282 def load(path, wrap=nil) ledger.load(path, wrap) end |
.load_monitor ⇒ Object
NOTE: Not used yet.
292 293 294 |
# File 'lib/roll/ledger.rb', line 292 def load_monitor ledger.load_monitor end |
.load_stack ⇒ Object
287 288 289 |
# File 'lib/roll/ledger.rb', line 287 def load_stack ledger.load_stack end |
.open(name, constraint = nil) {|lib| ... } ⇒ Object
Same as #instance but will raise and error if the library is not found. This can also take a block to yield on the library.
64 65 66 67 68 69 70 71 |
# File 'lib/roll/library.rb', line 64 def self.open(name, constraint=nil) #:yield: lib = instance(name, constraint) unless lib raise LoadError, "no library -- #{name}" end yield(lib) if block_given? lib end |
.require(path) ⇒ Object
277 278 279 |
# File 'lib/roll/ledger.rb', line 277 def require(path) ledger.require(path) end |
Instance Method Details
#<=>(other) ⇒ Object
Compare by version.
251 252 253 |
# File 'lib/roll/library.rb', line 251 def <=>(other) version <=> other.version end |
#active? ⇒ Boolean
95 96 97 |
# File 'lib/roll/library.rb', line 95 def active? @active ||= .active end |
#bindir ⇒ Object
Location of executable. This is alwasy bin/. This is a fixed convention, unlike lib/ which needs to be more flexable.
272 |
# File 'lib/roll/library.rb', line 272 def bindir ; File.join(location, 'bin') ; end |
#bindir? ⇒ Boolean
Is there a bin/
location?
275 |
# File 'lib/roll/library.rb', line 275 def bindir? ; File.exist?(bindir) ; end |
#confdir ⇒ Object
Location of library system configuration files. This is alwasy the etc/
directory.
279 |
# File 'lib/roll/library.rb', line 279 def confdir ; File.join(location, 'etc') ; end |
#confdir? ⇒ Boolean
Is there a etc/
location?metadata.name
282 |
# File 'lib/roll/library.rb', line 282 def confdir? ; File.exist?(confdir) ; end |
#datadir ⇒ Object
Location of library shared data directory. This is always the data/
directory.
286 |
# File 'lib/roll/library.rb', line 286 def datadir ; File.join(location, 'data') ; end |
#datadir? ⇒ Boolean
Is there a data/
location?
289 |
# File 'lib/roll/library.rb', line 289 def datadir? ; File.exist?(datadir) ; end |
#find(file, suffix = true) ⇒ Object
Standard loadpath search.
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/roll/library.rb', line 135 def find(file, suffix=true) if suffix SUFFIXES.each do |ext| loadpath.each do |lpath| f = File.join(location, lpath, file + ext) return f if File.file?(f) end end else loadpath.each do |lpath| f = File.join(location, lpath, file) return f if File.file?(f) end end nil end |
#include?(file, suffix = true) ⇒ Boolean
Does this library have a matching file
? If so, the full-path of the file is returned.
Unlike #find, this also matches within the library directory itself, eg. lib/foo/*
. It is used by #aquire.
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/roll/library.rb', line 158 def include?(file, suffix=true) if suffix SUFFIXES.each do |ext| loadpath.each do |lpath| f = File.join(location, lpath, name, file + ext) return f if File.file?(f) f = File.join(location, lpath, file + ext) return f if File.file?(f) end end else loadpath.each do |lpath| f = File.join(location, lpath, name, file) return f if File.file?(f) f = File.join(location, lpath, file) return f if File.file?(f) end end nil end |
#inspect ⇒ Object
Inspection.
238 239 240 241 242 243 244 |
# File 'lib/roll/library.rb', line 238 def inspect if @version %[#<Library #{name}/#{@version} @location="#{location}">] else %[#<Library #{name} @location="#{location}">] end end |
#load(file, wrap = nil) ⇒ Object
214 215 216 217 218 219 220 221 |
# File 'lib/roll/library.rb', line 214 def load(file, wrap=nil) if path = include?(file, false) load_absolute(path, wrap) else load_error = LoadError.new("no such file to load -- #{name}:#{file}") clean_backtrace(load_error) end end |
#load_absolute(file, wrap = nil) ⇒ Object
224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/roll/library.rb', line 224 def load_absolute(file, wrap=nil) #Library.load_monitor[file] << caller if $LOAD_MONITOR Library.load_stack << self begin success = original_load(file, wrap) #rescue LoadError => load_error # raise clean_backtrace(load_error) ensure Library.load_stack.pop end success end |
#loadpath ⇒ Object
100 101 102 |
# File 'lib/roll/library.rb', line 100 def loadpath @loadpath ||= .loadpath end |
#location ⇒ Object
80 81 82 |
# File 'lib/roll/library.rb', line 80 def location @location end |
#metadata ⇒ Object
Access to secondary metadata.
292 293 294 |
# File 'lib/roll/library.rb', line 292 def ||= Metadata.new(location) end |
#name ⇒ Object
85 86 87 |
# File 'lib/roll/library.rb', line 85 def name @name ||= .name end |
#released ⇒ Object
110 111 112 |
# File 'lib/roll/library.rb', line 110 def released @released ||= .released end |
#require(file) ⇒ Object
190 191 192 193 194 195 196 197 |
# File 'lib/roll/library.rb', line 190 def require(file) if path = include?(file) require_absolute(path) else load_error = LoadError.new("no such file to require -- #{name}:#{file}") raise clean_backtrace(load_error) end end |
#require_absolute(file) ⇒ Object
NOT SURE ABOUT USING THIS
200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/roll/library.rb', line 200 def require_absolute(file) #Library.load_monitor[file] << caller if $LOAD_MONITOR Library.load_stack << self begin success = original_require(file) #rescue LoadError => load_error # raise clean_backtrace(load_error) ensure Library.load_stack.pop end success end |
#requires ⇒ Object
105 106 107 |
# File 'lib/roll/library.rb', line 105 def requires @requires ||= .requires end |
#to_s ⇒ Object
246 247 248 |
# File 'lib/roll/library.rb', line 246 def to_s inspect end |
#verify ⇒ Object
115 116 117 118 119 |
# File 'lib/roll/library.rb', line 115 def verify requires.each do |(name, constraint)| Library.open(name, constraint) end end |
#version ⇒ Object
90 91 92 |
# File 'lib/roll/library.rb', line 90 def version @version ||= .version end |