module Scorm2004::Manifest::Attributes::ClassMethods

Public Instance Methods

attribute(type, name, options = {}) click to toggle source
# File lib/scorm2004/manifest/attributes.rb, line 31
def attribute(type, name, options = {})
  base = basename(name)
  attributes << base
  define_method("raw_#{base}") do
    @el.at("./@#{name}", NS).try(:content) || options[:default].try(:to_s)
  end
  send("#{type}_attribute", name, options)
  attr_reader base
end
attributes() click to toggle source
# File lib/scorm2004/manifest/attributes.rb, line 27
def attributes
  @attributes ||= []
end

Private Instance Methods

any_uri_attribute(name, options) click to toggle source
# File lib/scorm2004/manifest/attributes.rb, line 126
def any_uri_attribute(name, options)
  base = basename(name)
  define_method("check_#{base}".intern) do
    raw = send("raw_#{base}".intern)
    return if options[:allow_nil] && raw.nil?
    error("No #{name} attribute.") if raw.nil?
    begin
      uri = URI(raw)
    rescue URI::InvalidURIError => e
      error(e)
    end
    instance_variable_set("@#{base}".intern, uri.to_s)
  end
end
basename(name) click to toggle source
# File lib/scorm2004/manifest/attributes.rb, line 141
def basename(name)
  name.split(':').last.underscore.intern
end
boolean_attribute(name, options) click to toggle source
# File lib/scorm2004/manifest/attributes.rb, line 43
def boolean_attribute(name, options)
  base = basename(name)
  define_method("check_#{base}".intern) do
    raw = send("raw_#{base}")
    error("No #{name} attribute.") if raw.nil?
    unless %( true false 1 0 ).include?(raw)
      error("Non xs:boolean value for the #{name}: #{raw}")
    end
    instance_variable_set("@#{base}".intern, %( true 1 ).include?(raw))
  end
end
decimal_attribute(name, options) click to toggle source
# File lib/scorm2004/manifest/attributes.rb, line 78
def decimal_attribute(name, options)
  base = basename(name)
  define_method("check_#{base}".intern) do
    raw = send("raw_#{base}".intern)
    return if options[:allow_nil] && raw.nil?
    error("No #{name} attribute.") if raw.nil?
    if options[:range] && !options[:range].include?(Float(raw))
      error("The decimal attribute, #{name}, out of range (#{options[:range]}): #{raw}")
    end
    instance_variable_set("@#{base}".intern, Float(raw))
  end
end
duration_attribute(name, options) click to toggle source
# File lib/scorm2004/manifest/attributes.rb, line 104
def duration_attribute(name, options)
  base = basename(name)
  define_method("check_#{base}".intern) do
    raw = send("raw_#{base}".intern)
    return if options[:allow_nil] && raw.nil?
    error("No #{name} attribute.") if raw.nil?
    error("Non xs:duration value for #{name}: #{raw}") unless xs_duration?(raw)
    instance_variable_set("@#{base}".intern, raw)
  end
end
id_attribute(name, options) click to toggle source
# File lib/scorm2004/manifest/attributes.rb, line 67
def id_attribute(name, options)
  base = basename(name)
  define_method("check_#{base}".intern) do
    value = send("raw_#{base}").try(:strip)
    error("No #{name} attribute.") unless options[:allow_nil] || value
    error("Non xs:ID value for the #{name} attribute: #{value}") unless xs_id?(value)
    instance_variable_set("@#{base}".intern, value)
  end
end
Also aliased as: idref_attribute
idref_attribute(name, options)
Alias for: id_attribute
non_negative_integer_attribute(name, options) click to toggle source
# File lib/scorm2004/manifest/attributes.rb, line 91
def non_negative_integer_attribute(name, options)
  base = basename(name)
  define_method("check_#{base}".intern) do
    raw = send("raw_#{base}".intern)
    return if options[:allow_nil] && raw.nil?
    error("No #{name} attribute.") if raw.nil?
    unless xs_non_negative_integer?(raw)
      error("Non xs:nonNegativeInteger value for #{name}: #{raw}")
    end
    instance_variable_set("@#{base}".intern, Integer(raw, 10))
  end
end
string_attribute(name, options) click to toggle source
# File lib/scorm2004/manifest/attributes.rb, line 55
def string_attribute(name, options)
  base = basename(name)
  define_method("check_#{base}".intern) do
    raw = send("raw_#{base}")
    error("No #{name} attribute.") unless options[:allow_nil] || raw
    if options[:spm] && raw.length > options[:spm]
      error("The length of #{name} exceeds the SPM of #{options[:spm]} characters.")
    end
    instance_variable_set("@#{base}".intern, raw)
  end
end
token_attribute(name, options) click to toggle source
# File lib/scorm2004/manifest/attributes.rb, line 115
def token_attribute(name, options)
  base = basename(name)
  define_method("check_#{base}".intern) do
    raw = send("raw_#{base}".intern)
    error("No #{name} attribute.") if raw.nil?
    raw = replace_and_collapse_whitespaces(raw)
    error("Invalid #{name}: #{raw}") unless options[:vocabulary].include?(raw)
    instance_variable_set("@#{base}".intern, raw)
  end
end