class ALD::Definition

Access information in ALD package definition files.

Public

↑ top

Public Class Methods

new(source) click to toggle source

Open a new definition file for analysis.

source

the source to read the definition from. This can be a Nokogiri::XML::Document, a String or any object that responds to #read and #close.

Examples

definition = ALD::Definition.new('/path/to/def.xml')

Raises ALD::InvalidDefinitionError if the supplied source is not a valid ALD package definition.

# File lib/ALD/definition.rb, line 99
def initialize(source)
  if source.is_a? Nokogiri::XML::Document
    @document = source
  else
    @document = Nokogiri::XML(source) { |config| config.nonet }
  end

  raise InvalidDefinitionError unless valid?
end

Public Instance Methods

authors() click to toggle source

Get the item's authors information

Examples

definition.authors.each do |author|
  puts "Author: #{author['name']}"
  puts "\tUser name: #{author['user-name'] || '(unknown)'}"
  puts "\tHomepage:  #{author['homepage']  || '(unknown)'}"
  puts "\tEmail:     #{author['email']     || '(unknown)'}"
end

Returns

Returns an Array of Hashes, where each Hash has the 'name' key and may also have 'user-name', 'homepage' and 'email' keys.

# File lib/ALD/definition.rb, line 140
def authors
  attribute_hash '//ald:authors/ald:author', %w[name user-name homepage email]
end
description() click to toggle source

Get the defined item's description.

Returns

Returns the description String.

# File lib/ALD/definition.rb, line 112
def description
  @document.xpath("//ald:description", 'ald' => XML_NAMESPACE)[0].text
end
id() click to toggle source

Gets the ID of the item to be represented by this definition.

Examples

puts "Item ID: #{definition.id}"

Signature

# File lib/ALD/definition.rb, line 41
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
name() click to toggle source

Gets the name of the item defined by this definition.

Examples

puts "The item is called '#{definition.name}'"

Signature

# File lib/ALD/definition.rb, line 51
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
summary() click to toggle source

Gets the item's summary text.

Examples

puts "\n#{definition.summary}\n"

Signature

# File lib/ALD/definition.rb, line 81
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
tags() click to toggle source

Get the defined item's tags.

Examples

definition.tags.each { |tag| puts " - #{tag}" }

Returns

Returns the Array of Strings that the item is tagged with.

# File lib/ALD/definition.rb, line 123
def tags
  @document.xpath("//ald:tags/ald:tag/@ald:name", 'ald' => XML_NAMESPACE).map { |tag| tag.value }
end
to_s() click to toggle source

Get the XML string representing the definition

# File lib/ALD/definition.rb, line 162
def to_s
  @document.to_s
end
type() click to toggle source

Gets the type of item this definition represents.

Examples

puts "Item type is #{definition.type}"

Signature

# File lib/ALD/definition.rb, line 71
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
version() click to toggle source

Gets the semver version of this definition's item.

Examples

puts "#{definition.name} v#{definition.version}"

Signature

# File lib/ALD/definition.rb, line 61
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        

Internal

↑ top

Constants

SCHEMA_FILE

Path to the file containing the XML Schema Definition used for definition validation.

TOPLEVEL_ATTRIBUTES

The array of attributes which are defined in the root element of a definition. Each of these gets a dynamically defined method.

XML_NAMESPACE

The XML namespace used by the definitions.

Public Class Methods

schema() click to toggle source

The XML Schema instance used for validation

Returns

Returns the Nokogiri::XML::Schema instance representing ::SCHEMA_FILE.

# File lib/ALD/definition.rb, line 17
def self.schema
  @schema ||= Nokogiri::XML::Schema(File.read(SCHEMA_FILE))
end

Public Instance Methods

valid?() click to toggle source

Check if the definition is valid. Library consumers need not call this, as ::new already does.

Returns

Returns true, if the definition is valid according to the schema, false otherwise.

# File lib/ALD/definition.rb, line 157
def valid?
  Definition.schema.valid?(@document)
end

Private Instance Methods

attribute_hash(xpath, keys) click to toggle source

Get an Array of attribute Hashes from a list of elements in the definition.

xpath

the XPath String pointing to the XML elements in the definition

keys

the Array of keys to retrieve from the elements

Returns

Returns an Array of Hashes, where each Hash has all those of the given keys that were actual attributes on the relevant element.

# File lib/ALD/definition.rb, line 176
def attribute_hash(xpath, keys)
  @document.xpath(xpath, 'ald' => XML_NAMESPACE).map do |e|
    Hash[keys.map { |k| e.attribute_with_ns(k, XML_NAMESPACE) }.compact.map { |a| [a.node_name, a.value] }]
  end
end