Class: MOLGENIS::Parser

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

Instance Method Summary collapse

Instance Method Details

#create_model(element, version) ⇒ Object

:nodoc:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/molgenis_parser.rb', line 32

def create_model(element, version) # :nodoc:
  model = MolgenisModel.new
  model.name = element.attributes['name']
  puts element.attributes['name'] + model.name
  model.version = version
  model.description = ""
  element.find("description").each { |desc| 
    desc.children.each { |el| 
      model.description = model.description + el.to_s
    }
  }     
  
  color = 0
  parse_entities(element.find("entity"),model, color)
  
  element.find("module").each { |module_xml|
    
    module_obj = ModuleModel.new
    module_attributes = module_xml.attributes
    module_obj.name = module_attributes["name"]
    entities = module_xml.find("entity")
    
    color += 1
    module_obj.color = color
    parse_entities(entities,module_obj,color)
    
    model.modules << module_obj
  }
  
  return model
end

#parse(molgenis_src) ⇒ Object

Returns the model for the given molgenis_model. The method accepts objects of classes File, StringIO and String only.

Usage

foo = … # stuff to initialize foo here bar = MOLGENIS_MODEL:Parser.new.parse(foo)



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/molgenis_parser.rb', line 11

def parse(molgenis_src)
  puts "tests"
  case molgenis_src.class.to_s
    when /^string$/i
    document = LibXML::XML::Parser.string(molgenis_src,:encoding => LibXML::XML::Encoding::UTF_8).parse
    when /^stringio|file$/i
    molgenis_src.rewind
    document = LibXML::XML::Parser.string(molgenis_src.read,:encoding => LibXML::XML::Encoding::UTF_8).parse
  else
    raise "Error parsing file."
  end
  
  root = document.root
  return nil if(root.name != "molgenis")
  
  raise "Doesn't appear to be a MOLGENIS model" if root.name != "molgenis"
  version = root["version"]
  
  create_model(root, version)
end

#parse_entities(entities, module_obj, colorno) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/molgenis_parser.rb', line 64

def parse_entities(entities,module_obj,colorno)
  if entities
    entities.each do |entity_xml|
      entity = EntityModel.new
      entity.module_name = module_obj.name
      entity_attributes = entity_xml.attributes
      entity.name = entity_attributes["name"]
      entity.color = colorno
      
      entity.description =""
      entity_xml.find("description").each { |desc| 
        desc.children.each { |el| 
          entity.description = entity.description + el.to_s
        }
      }    
      
      entity_attributes["implements"].split(",").each { |interface|
        entity.implements << interface
      } if !entity_attributes["implements"].nil?
      entity.extends = entity_attributes["extends"]
      
      entity_xml.find("field").each do |field_xml|
        field = FieldModel.new
        field_attributes = field_xml.attributes
        field.name = field_attributes["name"]
        field.type = field_attributes["type"]
        field.label = field_attributes["label"]
        field.description = field_attributes["description"]
        if(field_attributes["xref_field"])
          split = field_attributes["xref_field"].split(".")
          if(split.size == 2)
            field.xref_entity = split[0]
            field.xref_field = split[1]
          else
            field.xref_field = split[0]
            field.xref_entity = field_attributes["xref_entity"]
          end
        end
        entity.fields << field
      end
      module_obj.entities << entity
    end
  end
end