Class: ALD::Definition
- Inherits:
-
Object
- Object
- ALD::Definition
- Defined in:
- lib/ALD/definition.rb,
lib/ALD/definition_generator.rb
Overview
Public: Access information in ALD package definition files.
Defined Under Namespace
Classes: Generator
Constant Summary collapse
- SCHEMA_FILE =
Internal: Path to the file containing the XML Schema Definition used for definition validation.
"#{File.dirname(__FILE__)}/schema.xsd"
- XML_NAMESPACE =
Internal: The XML namespace used by the definitions.
'ald://package/schema/2014'
- TOPLEVEL_ATTRIBUTES =
Internal: The array of attributes which are defined in the root element of a definition. Each of these gets a dynamically defined method.
%w[ id name version type summary ]
Class Method Summary collapse
-
.schema ⇒ Object
Internal: The XML Schema instance used for validation.
Instance Method Summary collapse
-
#authors ⇒ Object
Public: Get the item’s authors information.
-
#description ⇒ Object
Public: Get the defined item’s description.
-
#initialize(source) ⇒ Definition
constructor
Public: Open a new definition file for analysis.
-
#links ⇒ Object
Public: Gets additional links this definition references.
-
#tags ⇒ Object
Public: Get the defined item’s tags.
-
#to_s ⇒ Object
Public: Get the XML string representing the definition.
-
#valid? ⇒ Boolean
Internal: Check if the definition is valid.
Constructor Details
#initialize(source) ⇒ Definition
Public: 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.
99 100 101 102 103 104 105 106 107 |
# 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 |
Class Method Details
.schema ⇒ Object
Internal: The XML Schema instance used for validation
Returns the Nokogiri::XML::Schema instance representing ::SCHEMA_FILE.
17 18 19 |
# File 'lib/ALD/definition.rb', line 17 def self.schema @schema ||= Nokogiri::XML::Schema(File.read(SCHEMA_FILE)) end |
Instance Method Details
#authors ⇒ Object
Public: Get the item’s authors information
Examples
definition..each do ||
puts "Author: #{['name']}"
puts "\tUser name: #{['user-name'] || '(unknown)'}"
puts "\tHomepage: #{['homepage'] || '(unknown)'}"
puts "\tEmail: #{['email'] || '(unknown)'}"
end
Returns an Array of Hashes, where each Hash has the ‘name’ key and may also have ‘user-name’, ‘homepage’ and ‘email’ keys.
140 141 142 |
# File 'lib/ALD/definition.rb', line 140 def attribute_hash '//ald:authors/ald:author', %w[name user-name homepage email] end |
#description ⇒ Object
Public: Get the defined item’s description.
Returns the description String.
112 113 114 |
# File 'lib/ALD/definition.rb', line 112 def description @document.xpath("//ald:description", 'ald' => XML_NAMESPACE)[0].text end |
#links ⇒ Object
Public: Gets additional links this definition references
Returns an Array of Hashes, where each Hash has the keys ‘name’, ‘href’ and ‘description’-
148 149 150 |
# File 'lib/ALD/definition.rb', line 148 def links attribute_hash '//ald:links/ald:link', %w[name description href] end |
#tags ⇒ Object
Public: Get the defined item’s tags.
Examples
definition..each { |tag| puts " - #{tag}" }
Returns the Array of Strings that the item is tagged with.
123 124 125 |
# File 'lib/ALD/definition.rb', line 123 def @document.xpath("//ald:tags/ald:tag/@ald:name", 'ald' => XML_NAMESPACE).map { |tag| tag.value } end |
#to_s ⇒ Object
Public: Get the XML string representing the definition
162 163 164 |
# File 'lib/ALD/definition.rb', line 162 def to_s @document.to_s end |
#valid? ⇒ Boolean
Internal: Check if the definition is valid. Library consumers need not call this, as ::new already does.
Returns true, if the definition is valid according to the schema, false otherwise.
157 158 159 |
# File 'lib/ALD/definition.rb', line 157 def valid? Definition.schema.valid?(@document) end |