Class: Scorm::Metadata::DataElement

Inherits:
Object
  • Object
show all
Defined in:
lib/scorm/metadata.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = '', default_lang = nil) ⇒ DataElement

Returns a new instance of DataElement.



54
55
56
57
58
59
60
61
62
63
# File 'lib/scorm/metadata.rb', line 54

def initialize(value = '', default_lang = nil)
  if value.is_a? String
    @langstrings = Hash.new
    @langstrings['x-none'] = value
    @default_lang = 'x-none'
  elsif value.is_a? Hash
    @langstrings = value.dup
    @default_lang = default_lang || 'x-none'
  end
end

Class Method Details

.from_xml(element) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/scorm/metadata.rb', line 65

def self.from_xml(element)
  if element.elements.size == 0
    return self.new(element.text.to_s)
    
  elsif element.get_elements('value').size != 0
    value_el = element.get_elements('value').first
    return self.from_xml(value_el)
    
  elsif element.get_elements('langstring').size != 0
    langstrings = Hash.new
    default_lang = nil
    element.each_element('langstring') do |ls|
      default_lang = ls.attribute('xml:lang').to_s if default_lang.nil?
      langstrings[ls.attribute('xml:lang').to_s || 'x-none'] = ls.text.to_s
    end
    return self.new(langstrings, default_lang)
    
  else
    return Category.from_xml(element)
    
  end
end

Instance Method Details

#value(lang = nil) ⇒ Object Also known as: to_s, to_str



88
89
90
91
92
93
94
# File 'lib/scorm/metadata.rb', line 88

def value(lang = nil)
  if lang.nil?
    (@langstrings && @default_lang) ? @langstrings[@default_lang] : ''
  else
    (@langstrings) ? @langstrings[lang] || '' : ''
  end
end