Module: ActiveUnimod::Database
- Defined in:
- lib/active_unimod/database.rb
Overview
Methods for managing a local Unimod database. Information normally logged by Rails will be written to ‘unimod_log.txt’ unless ActiveRecord::Base.logger is set when this file is required.
Constant Summary collapse
- TABLES =
[ AltName, AminoAcid, Brick2element, Brick, Classification, Element, FragmentComp, Fragment, Log, Mod2brick, Modification, NeutralLoss, Position, Spec2nl, Specificity, User, XrefSource, Xref]
Class Method Summary collapse
-
.create_tables(verbose = true) ⇒ Object
Creates the ActiveUnimod tables using the default migration (db/migrate/001_active_unimod_schema).
-
.update_from_web(url = nil, overwrite = false, logger = Logger.new($stdout)) ⇒ Object
Retrieves the unimod_tables.xml from “www.unimod.org/xml/unimod_tables.xml” and uses this datafile in update_from_xml.
-
.update_from_xml(xml, overwrite = false, logger = Logger.new($stdout)) ⇒ Object
Parses the xml into ActiveUnimod records.
Class Method Details
.create_tables(verbose = true) ⇒ Object
Creates the ActiveUnimod tables using the default migration (db/migrate/001_active_unimod_schema)
24 25 26 27 |
# File 'lib/active_unimod/database.rb', line 24 def create_tables(verbose=true) ActiveRecord::Migration.verbose = verbose ActiveRecord::Migrator.migrate(File.dirname(__FILE__) + "/../../db/migrate/") end |
.update_from_web(url = nil, overwrite = false, logger = Logger.new($stdout)) ⇒ Object
Retrieves the unimod_tables.xml from “www.unimod.org/xml/unimod_tables.xml” and uses this datafile in update_from_xml.
76 77 78 79 80 81 |
# File 'lib/active_unimod/database.rb', line 76 def update_from_web(url=nil, overwrite=false, logger=Logger.new($stdout)) url = "http://www.unimod.org/xml/unimod_tables.xml" if url == nil logger.add(Logger::INFO, "retrieving: #{url}") update_from_xml(open(url), overwrite, logger) end |
.update_from_xml(xml, overwrite = false, logger = Logger.new($stdout)) ⇒ Object
Parses the xml into ActiveUnimod records. If overwrite=true the new records will replace existing records by with the same id. Otherwise, non-existant records will be created and the existing records will be checked. Any inconsistent records will be collected and returned as an array of [new_record, existing_record] arrays.
Progress is logged to logger.
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 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/active_unimod/database.rb', line 37 def update_from_xml(xml, overwrite=false, logger=Logger.new($stdout)) doc = Hpricot.XML(xml) inconsistencies = [] TABLES.each do |table| table.transaction do logger.add(Logger::INFO, "updating: #{table.table_name}") rows = doc.search("unimod/#{table.table_name}/#{table.table_name}_row") rows.each do |row| attributes = row.attributes id = attributes.delete("record_id") record = table.new(attributes) record.id = id if table.exists?(id) existing_record = table.find(id) unless record.attributes == existing_record.attributes if overwrite logger.add(Logger::INFO, "overwritting: #{id}") existing_record.attributes = row.attributes existing_record.save else inconsistencies << [record, existing_record] end end else logger.add(Logger::INFO, "adding: #{id}") record.save end end end end inconsistencies end |