module Scorm2004::Manifest::VisitorPattern

Public Class Methods

included(base) click to toggle source
# File lib/scorm2004/manifest/visitor_pattern.rb, line 4
def self.included(base)
  base.class_eval do
    const_set('Error', Class.new(Scorm2004::Manifest::Error))
  end
end
new(options = {}) click to toggle source
# File lib/scorm2004/manifest/visitor_pattern.rb, line 10
def initialize(options = {})
  @options = options
end

Public Instance Methods

metadata() click to toggle source

@return [Nokogiri::XML::Node, nil] The <metadata> element or nil

# File lib/scorm2004/manifest/visitor_pattern.rb, line 35
def metadata
  @el.at('./imscp:metadata', NS)
end
to_hash() click to toggle source
# File lib/scorm2004/manifest/visitor_pattern.rb, line 23
def to_hash
  if respond_to? :content
    content
  else
    {}.merge(self.class.respond_to?(:attributes) ? attributes_hash : {})
      .merge(self.class.respond_to?(:children) ? children_hash : {})
      .merge(respond_to?(:href) ? {:href => href } : {})
      .reject { |k, v| v.nil? || v == [] || v == {} }
  end
end
visit(el) click to toggle source
# File lib/scorm2004/manifest/visitor_pattern.rb, line 14
def visit(el)
  @el = el
  check_attributes if self.class.respond_to?(:attributes)
  do_visit         if self.respond_to?(:do_visit, true)
  visit_children   if self.class.respond_to?(:children)
  post_visit       if self.respond_to?(:post_visit, true)
  self
end

Private Instance Methods

attributes_hash() click to toggle source
# File lib/scorm2004/manifest/visitor_pattern.rb, line 57
def attributes_hash
  Hash[self.class.attributes.map { |attr| [attr, instance_variable_get("@#{attr}")]}]
end
check_attributes() click to toggle source
# File lib/scorm2004/manifest/visitor_pattern.rb, line 45
def check_attributes
  self.class.attributes.each do |attr|
    send("check_#{attr}".intern)
  end
end
children_hash() click to toggle source
# File lib/scorm2004/manifest/visitor_pattern.rb, line 61
def children_hash
  Hash[self.class.children.map { |child|
      value = instance_variable_get("@#{child}")
      if value.respond_to?(:map)
        [child, value.map { |v| v.to_hash }]
      else
        [child, value.try(:to_hash)]
      end
      }]
end
error(message) click to toggle source
# File lib/scorm2004/manifest/visitor_pattern.rb, line 41
def error(message)
  raise("#{self.class}::Error".constantize, [message, @el.try(:to_s)].compact.join("\n"))
end
visit_children() click to toggle source
# File lib/scorm2004/manifest/visitor_pattern.rb, line 51
def visit_children
  self.class.children.each do |child|
    send("visit_#{child}".intern)
  end
end