Module: Scorm2004::Manifest::Children::ClassMethods

Defined in:
lib/scorm2004/manifest/children.rb

Instance Method Summary collapse

Instance Method Details

#childrenObject



25
26
27
# File 'lib/scorm2004/manifest/children.rb', line 25

def children
  @children ||= []
end

#has_many(xpath, options = {}) ⇒ Object Also known as: has_one_or_more



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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

#has_one(xpath, options = {}) ⇒ Object Also known as: has_one_and_only_one



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 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

#has_zero_or_more(xpath, options = {}) ⇒ Object



66
67
68
# 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 = {}) ⇒ Object



45
46
47
# File 'lib/scorm2004/manifest/children.rb', line 45

def has_zero_or_one(xpath, options = {})
  has_one(xpath, options.merge( { :allow_nil => true } ))
end