Class: Library::Metadata

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

Overview

The Metadata call encapsulates a library’s information, in particular ‘name`, `version` and `load_path`.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(location, metadata = {}) ⇒ Metadata

Setup new metadata object.

Parameters:

  • location (String)

    Location of project on disc.

  • metadata (Hash) (defaults to: {})

    Manual metadata settings.

Options Hash (metadata):

  • :load (Boolean)

    Set to false will prevent metadata being loaded from .ruby or .gemspec file, but a LoadError will be raised without ‘:name` and `:version`.



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

def initialize(location, ={})
  @location  = location

  @load = .delete(:load)
  @load = true if @load.nil?  # default is true

  @data = {}

  update()

  if @load
    if not (@data['name'] && @data['version'] && @data['load_path'])
      
    end
  else
    raise LoadError unless data['name'] && data['version']   # todo: just name ?
  end
end

Instance Attribute Details

#locationObject (readonly)

Location of library.



62
63
64
# File 'lib/library/metadata.rb', line 62

def location
  @location
end

Instance Method Details

#[](name) ⇒ Object

Access to non-primary metadata.



164
165
166
# File 'lib/library/metadata.rb', line 164

def [](name)
  @data[name.to_sym]
end

#dateObject Also known as: released

Release date.



123
124
125
# File 'lib/library/metadata.rb', line 123

def date
  @data[:date] || (; @data[:data])
end

#date=(date) ⇒ Object Also known as: released=

Set the date.



134
135
136
# File 'lib/library/metadata.rb', line 134

def date=(date)
  @data[:date] = date
end

#development_requirementsObject

Development requirements.



157
158
159
# File 'lib/library/metadata.rb', line 157

def development_requirements
  @development_requirements ||= requirements.select{ |r| r['development'] }
end

#dotruby?Boolean

Does this location have .ruby entries?

Returns:

  • (Boolean)


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

def dotruby?
  @_dotruby ||= File.exist?(File.join(location, '.ruby'))
end

#gemspecObject

Access to complete gemspec. This is for use with extended metadata.



207
208
209
210
211
212
# File 'lib/library/metadata.rb', line 207

def gemspec
  @_gemspec ||= (
    require 'rubygems'
    ::Gem::Specification.load(gemspec_file)
  )
end

#gemspec?Boolean

Deterime if the location is a gem location. It does this by looking for the corresponding ‘gems/specification/*.gemspec` file.

Returns:

  • (Boolean)


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

def gemspec?
  #return true if Dir[File.join(location, '*.gemspec')].first
  pkgname = File.basename(location)
  gemsdir = File.dirname(location)
  specdir = File.join(File.dirname(gemsdir), 'specifications')
  Dir[File.join(specdir, "#{pkgname}.gemspec")].first
end

#gemspec_fileObject (private)

Returns the path to the .gemspec file.



350
351
352
# File 'lib/library/metadata.rb', line 350

def gemspec_file
  gemspec_system_file || gemspec_local_file
end

#gemspec_file_localObject (private)

Returns the path to a gemspec file located in the project location, if it exists. Otherwise returns nil.



358
359
360
# File 'lib/library/metadata.rb', line 358

def gemspec_file_local
  @_gemspec_file_local ||= Dir[File.join(location, '*.gemspec')].first
end

#gemspec_file_systemObject (private)

Returns the path to a gemspec file located in the gems/specifications directory, if it exists. Otherwise returns nil.



366
367
368
369
370
371
372
373
# File 'lib/library/metadata.rb', line 366

def gemspec_file_system
  @_gemspec_file_system ||= (
    pkgname = File.basename(location)
    gemsdir = File.dirname(location)
    specdir = File.join(File.dirname(gemsdir), 'specifications')
    Dir[File.join(specdir, "#{pkgname}.gemspec")].first
  )
end

#load_dotrubyObject (private)

Load metadata for .ruby file.



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/library/metadata.rb', line 281

def load_dotruby
  require 'yaml'

  data = YAML.load_file(File.join(location, '.ruby'))

  update(data)

  #if Hash === data
  #  self.name         = data['name']
  #  self.version      = data['version']      #|| '0.0.0'
  #  self.load_path    = data['load_path']    || ['lib']
  #
  #  self.title        = data['title']        || data['name'].capitalize
  #  self.date         = data['date']
  #
  #  reqs = data['requirements'] || []
  #  reqs.each do |req|
  #    if req['development']
  #      self.development_requirements << [req['name'], req['version']]
  #    else
  #      self.runtime_requirements << [req['name'], req['version']]
  #    end
  #  end
  #end
end

#load_gemspecObject (private)

Load metadata from a gemspec. This is a fallback option. It is highly recommended that a project have a ‘.ruby` file instead.

This method requires that the ‘dotruby` gem be installed.



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/library/metadata.rb', line 312

def load_gemspec
  #require 'rubygems'
  require 'dotruby/rubygems'

  text = File.read(gemspec_file)
  if text =~ /\A---/  # TODO: improve
    require 'yaml'
    spec = YAML.load(text)
  else
    spec = eval(text)
  end

  dotruby = DotRuby::Spec.parse_gemspec(spec)

  data = dotruby.to_h

  udpate(data)

  return

  #self.name         = spec.name
  #self.version      = spec.version.to_s
  #self.load_path    = spec.require_paths
  #self.date         = spec.date
  #self.title        = spec.name.capitalize  # for lack of better way

  #spec.dependencies.each do |dep|
  #  if dep.development?
  #    self.development_requirements < [dep.name, dep.version]
  #  else
  #    self.runtime_requirements < [dep.name, dep.verison]
  #  end
  #end
end

#load_metadataObject (private)

Load metadata.



268
269
270
271
272
273
274
275
276
277
278
# File 'lib/library/metadata.rb', line 268

def 
  return unless @load

  if dotruby?
    load_dotruby
  elsif gemspec?
    load_gemspec
  end

  @load = false  # loading is complete
end

#load_pathObject Also known as: loadpath

Local load paths.



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

def load_path
  @data[:load_path] || ['lib']
end

#load_path=(path) ⇒ Object Also known as: loadpath=

Set the loadpath.



76
77
78
79
80
81
82
83
84
# File 'lib/library/metadata.rb', line 76

def load_path=(path)
  case path
  when nil
    path = ['lib']
  when String
    path = path.strip.split(/[,;:\ \n\t]/).map{|s| s.strip}
  end
  @data[:load_path] = path
end

#missing_requirements(development = false) ⇒ Array<String,String>

Verify that a library’s requirements are all available in the ledger. Returns a list of ‘[name, version]` of Libraries that were not found.

Returns:

  • (Array<String,String>)

    List of missing requirements.



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/library/metadata.rb', line 220

def missing_requirements(development=false) #verbose=false)
  libs, fail = [], []
  reqs = development ? requirements : runtime_requirements
  reqs.each do |req|
    name = req['name']
    vers = req['version']
    lib = Library[name, vers]
    if lib
      libs << lib
      #$stdout.puts "  [LOAD] #{name} #{vers}" if verbose
      unless libs.include?(lib) or fail.include?([lib,vers])
        lib.verify_requirements(development) #verbose)
      end
    else
      fail << [name, vers]
      #$stdout.puts "  [FAIL] #{name} #{vers}" if verbose
    end
  end
  return fail
end

#missing_requirements?(development = false) ⇒ Boolean

Like #missing_requirements but returns ‘true`/`false`.

Returns:

  • (Boolean)


244
245
246
247
# File 'lib/library/metadata.rb', line 244

def missing_requirements?(development=false)
  list = missing_requirements(development=false)
  list.empty? ? false : true
end

#nameObject

Name of library.



91
92
93
# File 'lib/library/metadata.rb', line 91

def name
  @data[:name]
end

#name=(string) ⇒ Object

Set name.



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

def name=(string)
  @data[:name] = string.to_s if string
end

#omit=(boolean) ⇒ Object

Set omit.



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

def omit=(boolean)
  @omit = boolean
end

#omit?Boolean

Omit from any ledger?

Returns:

  • (Boolean)


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

def omit?
  @omit
end

#requirementsObject

Runtime and development requirements combined.



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

def requirements
  @data[:requirements] || (; @data[:requirements])
end

#runtime_requirementsObject

Runtime requirements.



150
151
152
# File 'lib/library/metadata.rb', line 150

def runtime_requirements
  @runtime_requirements ||= requirements.reject{ |r| r['development'] }
end

#to_hHash

Returns hash of primary metadata.

Returns:

  • (Hash)

    primary metdata



254
255
256
257
258
259
260
261
262
263
# File 'lib/library/metadata.rb', line 254

def to_h
  { 'location'     => location,
    'name'         => name,
    'version'      => version.to_s,
    'date'         => date.to_s,
    'load_path'    => load_path,
    'requirements' => requirements,
    'omit'         => omit
  }
end

#update(data) ⇒ Object

Update metadata with data hash.

Parameters:

  • data (Hash)

    Data to merge into metadata table.



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/library/metadata.rb', line 47

def update(data)
  data = data.rekey

  @data.update(data)

  self.name      = data[:name]      if data[:name]
  self.version   = data[:version]   if data[:version]
  self.load_path = data[:load_path] if data[:load_path]
  self.date      = data[:date]      if data[:date]
  self.omit      = data[:omit]
end

#versionObject

Version number.

Technically, a library should not appear in a ledger list if it lacks a version. However, just in case this occurs (say by a hand edited environment) we fallback to a version of ‘0.0.0’.



109
110
111
# File 'lib/library/metadata.rb', line 109

def version
  @data[:version] ||= Version.new('0.0.0')
end

#version=(string) ⇒ Object

Set version, converts string into Version number class.



116
117
118
# File 'lib/library/metadata.rb', line 116

def version=(string)
  @data[:version] = Version.new(string) if string
end