Module: SpecIDXML

Overview

I would prefer to call this SpecID::XML, but I keep getting an error: /home/john/Proteomics/msprot/lib/spec_id/bioworks.rb:412: warning: toplevel constant XML referenced by SpecID::XML’ This works around that for now. Any major xml elements should return a newline at the end for simple concatenation into a file

Constant Summary collapse

Special_chrs_hash =
{
  '"' => '"',
  '&' => '&',
  "'" => ''',
  '<' => '&lt;',
  '>' => '&gt;',
}

Instance Method Summary collapse

Instance Method Details

#attr_xml(symbol) ⇒ Object



90
91
92
# File 'lib/spec_id_xml.rb', line 90

def attr_xml(symbol)
  "#{symbol}=\"#{send(symbol)}\""
end

#attrs_xml(list_of_symbols) ⇒ Object



94
95
96
97
98
# File 'lib/spec_id_xml.rb', line 94

def attrs_xml(list_of_symbols)
  list_of_symbols.collect {|sy|
    attr_xml(sy)
  }.join(" ")
end

#element_xml(element, att_list) ⇒ Object

takes an element as a symbol and returns the



71
72
73
74
75
76
77
78
# File 'lib/spec_id_xml.rb', line 71

def element_xml(element, att_list)

  start = "#{tabs}<#{element} #{attrs_xml(att_list)}>\n"
  $DEPTH += 1
  if block_given? ; middle = yield else ; middle = '' end
  $DEPTH -= 1
  start + middle + "#{tabs}</#{element}>\n"
end

#element_xml_and_att_string(element, att_string) ⇒ Object

element as symbol and att_string as attributes takes a block of whatever



82
83
84
85
86
87
88
# File 'lib/spec_id_xml.rb', line 82

def element_xml_and_att_string(element, att_string)
  start = "#{tabs}<#{element} #{att_string}>\n"
  $DEPTH += 1
  if block_given? ; middle = yield else ; middle = '' end
  $DEPTH -= 1
  start + middle + "#{tabs}</#{element}>\n"
end

#element_xml_no_atts(element) ⇒ Object

takes an element as a symbol and returns the



62
63
64
65
66
67
68
# File 'lib/spec_id_xml.rb', line 62

def element_xml_no_atts(element)
  start = "#{tabs}<#{element}>\n"
  $DEPTH += 1
  if block_given? ; middle = yield else ; middle = '' end
  $DEPTH -= 1
  start + middle + "#{tabs}</#{element}>\n"
end

#escape_special_chars(string) ⇒ Object

substitutes special xml chars



18
19
20
21
22
23
24
# File 'lib/spec_id_xml.rb', line 18

def escape_special_chars(string)
  string.split('').map do |char|
    if Special_chrs_hash.key? char ; Special_chrs_hash[char] 
      # if x = Special_chrs_hash[char] ; x  # <-- that's slightly slower
    else ; char end
  end.join
end

#param_xml(symbol) ⇒ Object



36
37
38
# File 'lib/spec_id_xml.rb', line 36

def param_xml(symbol)
  tabs + '<parameter name="' + "#{symbol}" + '" value="' + "#{send(symbol)}" + '"/>'
end

#params_xml(*symbol_list) ⇒ Object



40
41
42
43
44
# File 'lib/spec_id_xml.rb', line 40

def params_xml(*symbol_list)
  symbol_list.collect { |sy|
    param_xml(sy)
  }.join("\n") + "\n"
end

#short_element_xml(element, att_list) ⇒ Object



46
47
48
# File 'lib/spec_id_xml.rb', line 46

def short_element_xml(element, att_list)
  "#{tabs}<#{element} #{attrs_xml(att_list)}/>\n"
end

#short_element_xml_and_att_string(element, att_string) ⇒ Object



50
51
52
# File 'lib/spec_id_xml.rb', line 50

def short_element_xml_and_att_string(element, att_string)
  "#{tabs}<#{element} #{att_string}/>\n"
end

#short_element_xml_from_instance_vars(element_name) ⇒ Object

requires that obj have attribute ‘@xml_element_name’ displays all instance_variables (does not call methods!)



56
57
58
59
# File 'lib/spec_id_xml.rb', line 56

def short_element_xml_from_instance_vars(element_name)
  string = instance_variables.map{|v| "#{v[1..-1]}=\"#{instance_variable_get(v)}\"" }.join(' ')
  "#{tabs}<#{element_name} #{string}/>\n"
end

#tabsObject



28
29
30
31
32
33
# File 'lib/spec_id_xml.rb', line 28

def tabs
  # this is ugly
  string = ""
  $DEPTH.times { string << "\t" }
  string
end