module Scorm2004::Manifest::Children::ClassMethods

Public Instance Methods

children() click to toggle source
# File lib/scorm2004/manifest/children.rb, line 25
def children
  @children ||= []
end
has_many(xpath, options = {}) click to toggle source
# File lib/scorm2004/manifest/children.rb, line 49
def has_many(xpath, options = {})
  name = options[:name].try(:to_s) || guess_child_name(xpath, options)
  children << name.pluralize.intern
  attr_reader name.pluralize.intern
  visitor_name = options[:visitor].try(:to_s) || name
  define_method("visit_#{name.pluralize}".intern) do
    unless options[:allow_nil] || search(xpath).size > 0
      error("<#{xpath}> not found.")
    end
    instance_variable_set("@#{name.pluralize}".intern, search(xpath).map { |child|
        child.accept(create_visitor(visitor_name))
      } )
  end
  define_visitor(visitor_name)
end
Also aliased as: has_one_or_more
has_one(xpath, options = {}) click to toggle source
# File lib/scorm2004/manifest/children.rb, line 29
def has_one(xpath, options = {})
  name = options[:name].try(:to_s) || guess_child_name(xpath, options)
  children << name.intern
  attr_reader name.intern
  visitor_name = options[:visitor].try(:to_s) || name
  define_method("visit_#{name}".intern) do
    error("Two <#{xpath}> elements found.") if search(xpath).size > 1
    error("<#{xpath}> not found.") unless options[:allow_nil] || at(xpath)
    if at(xpath)
      instance_variable_set("@#{name}".intern, at(xpath).accept(create_visitor(visitor_name)))
    end
  end
  define_visitor(visitor_name)
end
Also aliased as: has_one_and_only_one
has_one_and_only_one(xpath, options = {})
Alias for: has_one
has_one_or_more(xpath, options = {})
Alias for: has_many
has_zero_or_more(xpath, options = {}) click to toggle source
# File lib/scorm2004/manifest/children.rb, line 66
def has_zero_or_more(xpath, options = {})
  has_many(xpath, options.merge( { :allow_nil => true} ))
end
has_zero_or_one(xpath, options = {}) click to toggle source
# File lib/scorm2004/manifest/children.rb, line 45
def has_zero_or_one(xpath, options = {})
  has_one(xpath, options.merge( { :allow_nil => true } ))
end

Private Instance Methods

define_visitor(name) click to toggle source
# File lib/scorm2004/manifest/children.rb, line 76
def define_visitor(name)
  define_method("#{name}_visitor".intern) do
    options = respond_to?(:base) ? { :base => base } : {}
    "Scorm2004::Manifest::#{name.camelize}".constantize.new(options)
  end
end
guess_child_name(xpath, options) click to toggle source
# File lib/scorm2004/manifest/children.rb, line 72
def guess_child_name(xpath, options)
  xpath.split(%r{[/:]}).last.underscore
end