Class: RubyLibrary

Inherits:
Library show all
Includes:
RbConfig
Defined in:
lib/library/rubylib.rb

Overview

RubyLibrary is a specialized subclass of Library specifically designed to sever Ruby’s standard library. It is used to speed up load times for for library files that are standard Ruby scripts and should never be overriden by any 3rd party libraries. Good examples are ‘ostruct’ and ‘optparse’.

This class is in the proccess of being refined to exclude certian 3rd party redistributions, such RDoc and Soap4r.

Constant Summary

Constants included from RbConfig

RbConfig::WIN_PATTERNS

Constants inherited from Library

Library::SUFFIXES, Library::SUFFIX_PATTERN

Instance Method Summary collapse

Methods included from RbConfig

confdir, datadir, windows_platform?

Methods inherited from Library

#<=>, [], #absolute_loadpath, activate, #activate, #active?, add, #default, #feature, #inspect, instance, #legacy?, #legacy_loadpath, #load, #location, #metadata, #omit, #require, #runtime_requirements, #to_h, #to_s, #verify

Methods included from Library::Domain

#PATH, #acquire, #find_any, #find_files, #glob, #ledger, #load, #load_stack, #names, #prime, #require, #search

Constructor Details

#initializeRubyLibrary

Setup Ruby library.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/library/rubylib.rb', line 20

def initialize(*) #(location, metadata={})
  #rubylibdir  = ::RbConfig::CONFIG['rubylibdir']
  #rubyarchdir = ::RbConfig::CONFIG['archdir']
  #rel_archdir  = rubyarchdir.sub(rubylibdir+'/', '')
  #
  #@location = rubylibdir
  #@loadpath = ['', rel_archpath]

  location = find_base_path(CONFIG.values_at('rubylibdir', 'sitelibdir', 'vendorlibdir'))
  loadpath = CONFIG.values_at(
    'rubylibdir',
    'archdir',
    'sitelibdir',
    'sitearchdir',
    'vendorlibdir',
    'vendorarchdir'
  ).map{ |d| d.sub(location + '/','') }

  @location = location
  @loadpath = loadpath
  @name     = 'ruby'
  @metadata = {}  # TODO: can we fillout Ruby's metadata some ?
end

Instance Method Details

#bindirObject

Location of executables, which for Ruby is ‘RbConfig::CONFIG`.



106
107
108
# File 'lib/library/rubylib.rb', line 106

def bindir
  ::RbConfig::CONFIG['bindir']
end

#bindir?Boolean

Is there a ‘bin/` location?

Returns:

  • (Boolean)


113
114
115
# File 'lib/library/rubylib.rb', line 113

def bindir?
  File.exist?(bindir)
end

#confdirObject

Location of library system configuration files. For Ruby this is ‘RbConfig::CONFIG`.



121
122
123
# File 'lib/library/rubylib.rb', line 121

def confdir
  ::RbConfig::CONFIG['sysconfdir']
end

#confdir?Boolean

Is there a “‘etc`” location?

Returns:

  • (Boolean)


128
129
130
# File 'lib/library/rubylib.rb', line 128

def confdir?
  File.exist?(confdir)
end

#datadirObject

Location of library shared data directory. For Ruby this is ‘RbConfig::CONFIG`.



136
137
138
# File 'lib/library/rubylib.rb', line 136

def datadir
  ::RbConfig::CONFIG['datadir']
end

#datadir?Boolean

Is there a ‘data/` location?

Returns:

  • (Boolean)


143
144
145
# File 'lib/library/rubylib.rb', line 143

def datadir?
  File.exist?(datadir)
end

#dateObject Also known as: released

TODO:

This currently just returns current date/time. Is there a way to get Ruby’s own release date?

Release date.



79
80
81
# File 'lib/library/rubylib.rb', line 79

def date
  Time.now
end

#find(file, suffix = true) ⇒ Object

Ruby needs to ignore a few 3rd party libraries. They will be picked up by the final fallback to Ruby’s original require if all else fails.



98
99
100
101
# File 'lib/library/rubylib.rb', line 98

def find(file, suffix=true)
  return nil if /^rdoc/ =~ file
  super(file, suffix)
end

#find_base_path(paths) ⇒ Object (private)

Given an array of path strings, find the longest common prefix path.



195
196
197
198
199
200
201
202
203
# File 'lib/library/rubylib.rb', line 195

def find_base_path(paths)
  return paths.first if paths.length <= 1
  arr = paths.sort
  f = arr.first.split('/')
  l = arr.last.split('/')
  i = 0
  i += 1 while f[i] == l[i] && i <= f.length
  f.slice(0, i).join('/')
end

#libfile(lpath, file, ext = nil) ⇒ Object

Construct a Script match.



188
189
190
# File 'lib/library/rubylib.rb', line 188

def libfile(lpath, file, ext=nil)
  Library::Feature.new(self, lpath, file, ext) 
end

#load_absolute(feature, wrap = nil) ⇒ Boolean

Load library file given as a Script instance.

Parameters:

  • feature (String)

    Instance of Feature.

Returns:

  • (Boolean)

    Success of loading the feature.



171
172
173
174
175
176
# File 'lib/library/rubylib.rb', line 171

def load_absolute(feature, wrap=nil)
  success = super(feature, wrap)
  $" << feature.localname # ruby 1.8 does not use absolutes TODO: move up?
  $".uniq!
  success
end

#loadpathObject Also known as: load_path

Load path is essentially $LOAD_PATH, less gem paths.



66
67
68
69
# File 'lib/library/rubylib.rb', line 66

def loadpath
  #$LOAD_PATH - ['.']
  @loadpath
end

#loadpath_sortedObject

The loadpath sorted by largest path first.



181
182
183
# File 'lib/library/rubylib.rb', line 181

def loadpath_sorted
  loadpath.sort{ |a,b| b.size <=> a.size }
end

#nameObject

Then name of RubyLibrary is ‘ruby`.



47
48
49
# File 'lib/library/rubylib.rb', line 47

def name
  'ruby'
end

#require_absolute(feature) ⇒ Boolean

Require library file given as a Script instance.

Parameters:

  • feature (String)

    Instance of Feature.

Returns:

  • (Boolean)

    Success of requiring the feature.



155
156
157
158
159
160
161
# File 'lib/library/rubylib.rb', line 155

def require_absolute(feature)
  return false if $".include?(feature.localname)  # ruby 1.8 does not use absolutes
  success = super(feature)
  $" << feature.localname # ruby 1.8 does not use absolutes TODO: move up?
  $".uniq!
  success
end

#requirementsObject

Ruby requires nothing.



89
90
91
# File 'lib/library/rubylib.rb', line 89

def requirements
  []
end

#versionObject

Ruby version is RUBY_VERSION.



54
55
56
# File 'lib/library/rubylib.rb', line 54

def version
  RUBY_VERSION
end