Class: Library::Feature

Inherits:
Object
  • Object
show all
Defined in:
lib/library/feature.rb

Overview

The Feature class represents a single file within a library.

This class had been called ‘Script` until it occured to me that Ruby choose the name “feature” by it’s use of tem in the global variable ‘$LOADED_FEATURES`.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(library, loadpath, filename, extension = nil) ⇒ Feature

Create a new Feature instance.

Parameters:

  • library (Library)

    The Library object to which the feature belongs.

  • loadpath (String)

    The loadpath within the library in which the feature resides.

  • filename (String)

    The file path of the feature relative to the loadpath.

  • extension (Boolean) (defaults to: nil)

    File extension to append to the feature filename.



26
27
28
29
30
31
# File 'lib/library/feature.rb', line 26

def initialize(library, loadpath, filename, extension=nil)
  @library   = library
  @loadpath  = loadpath
  @filename  = filename
  @extension = extension
end

Instance Attribute Details

#extensionObject (readonly)

Extension of feature file, e.g. ‘.rb`.



53
54
55
# File 'lib/library/feature.rb', line 53

def extension
  @extension
end

#filenameObject (readonly)

The file path of the feature relative to the loadpath.



48
49
50
# File 'lib/library/feature.rb', line 48

def filename
  @filename
end

#libraryObject (readonly)

The Library object to which the file belongs.



36
37
38
# File 'lib/library/feature.rb', line 36

def library
  @library
end

#loadpathArray (readonly)

The loadpath within the library in which the feature resides.

Returns:

  • (Array)

    Load path relative to library location.



43
44
45
# File 'lib/library/feature.rb', line 43

def loadpath
  @loadpath
end

Instance Method Details

#==(other) ⇒ true, false

Compare this features full path name to another using ‘#==`.

Parameters:

  • another (Feature, String)

    feature or file path.

Returns:

  • (true, false)

    do the features represent the the same file



163
164
165
# File 'lib/library/feature.rb', line 163

def ==(other)
  fullname == other.to_s
end

#acquire(options = {}) ⇒ true, false

Acquire the feature –Roll’s advanced require/load method.

Returns:

  • (true, false)

    true if loaded, false if it already has been loaded.



103
104
105
106
107
108
109
# File 'lib/library/feature.rb', line 103

def acquire(options={})
  if options[:load] # TODO: .delete(:load) ?
    load(options)
  else
    require(options)
  end
end

#eql?(other) ⇒ true, false

Same as ‘#==`.

Parameters:

  • another (Feature, String)

    feature or file path.

Returns:

  • (true, false)

    if features are the same file



174
175
176
# File 'lib/library/feature.rb', line 174

def eql?(other)
  fullname == other.to_s
end

#fullnameString

Full path name of of feature.

Returns:

  • (String)

    expanded file path of feature



85
86
87
# File 'lib/library/feature.rb', line 85

def fullname
  @fullname ||= ::File.join(location, loadpath, filename + (extension || ''))
end

#hashInteger

Use ‘#fullname` to calculate a hash value for the feature file.

Returns:

  • (Integer)

    hash value



201
202
203
# File 'lib/library/feature.rb', line 201

def hash
  fullname.hash
end

#library_activateObject



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

def library_activate
  library.activate if Library === library
end

#library_nameString

Name of the library to which the feature belongs.

Returns:

  • (String)

    name of the feature’s library



60
61
62
# File 'lib/library/feature.rb', line 60

def library_name
  Library === library ? library.name : nil
end

#load(options = {}) ⇒ true, false

Load feature.

Returns:

  • (true, false)

    true if loaded, false if it already has been loaded.



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/library/feature.rb', line 139

def load(options={})
  if library_name == 'ruby' or library_name == 'site_ruby'
    $" << localname # ruby 1.8 does not use absolutes
  end

  Library.load_stack << self #library
  begin
    library_activate unless options[:force]
    success = __load__(fullname, options[:wrap])
  #rescue ::LoadError => load_error
  #  raise LoadError.new(localname, library_name)
  ensure
    Library.load_stack.pop
  end
  success
end

#localnameString

The path of the feature relative to the loadpath.

Returns:

  • (String)

    file path less location and loadpath



94
95
96
# File 'lib/library/feature.rb', line 94

def localname
  @localname ||= ::File.join(filename + (extension || ''))
end

#locationString

Library location.

Returns:

  • (String)

    location of library



76
77
78
# File 'lib/library/feature.rb', line 76

def location
  Library===library ? library.location : library
end

#require(options = {}) ⇒ true, false

Require feature.

Returns:

  • (true, false)

    true if loaded, false if it already has been loaded.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/library/feature.rb', line 116

def require(options={})
  if library_name == 'ruby' or library_name == 'site_ruby'
    return false if $".include?(localname)  # ruby 1.8 does not use absolutes
    $" << localname # ruby 1.8 does not use absolutes
  end

  Library.load_stack << self #library
  begin
    library_activate unless options[:force]
    success = __require__(fullname)
  #rescue ::LoadError => load_error  # TODO: deativeate this if $DEBUG ?
  #  raise LoadError.new(localname, library_name)
  ensure
    Library.load_stack.pop
  end
  success
end

#to_sString

Same a fullname.

Returns:

  • (String)

    expanded file path



183
184
185
# File 'lib/library/feature.rb', line 183

def to_s
  fullname
end

#to_strString

Same a fullname.

Returns:

  • (String)

    expanded file path



192
193
194
# File 'lib/library/feature.rb', line 192

def to_str
  fullname
end