class Mexico::FileSystem::Corpus
Attributes
Public Class Methods
Creates a new instance of the {Corpus} class. @param [String] path the path where the corpus is @option opts [String] :identifier The identifier that will be given if a new corpus is created
# File lib/mexico/file_system/corpus.rb, line 91 def initialize(path, opts={}) init_folder(path, opts) @base_path = File.expand_path(path) @corpus_file_name = File.join(@base_path, "Corpus.xml") f = File.open(@corpus_file_name, 'r') @xml_doc = ::Nokogiri::XML(f) f.close end
Opens the corpus folder and its manifest file at the given path. @param [String] path the path where the corpus is @option opts [String] :identifier The identifier that will be given if a new corpus is created @return [Mexico::FileSystem::Corpus] the new or opened Corpus
object
# File lib/mexico/file_system/corpus.rb, line 82 def self.open(path, opts={}) xml = File.open(File.join(path,'Corpus.xml'),'rb') { |f| f.read } c = Mexico::FileSystem::Corpus.from_xml(xml, opts.merge({:path=>path})) return c end
Public Instance Methods
Returns the disk usage in bytes for the whole corpus. @return [Integer] the number of bytes used by files of this corpus
# File lib/mexico/file_system/corpus.rb, line 137 def complete_file_size resources.collect{ |r| r.complete_file_size }.inject(:+) end
Creates a new design object and binds it to this corpus object. @option opts [String] :identifier The identifier of the new design (required). @option opts [String] :name The name of the new design. (required). @option opts [String] :description A description of the new design (optional).
# File lib/mexico/file_system/corpus.rb, line 129 def create_design(opts={}) d = ::Mexico::FileSystem::Design.new(opts) d.bind_to_corpus(self) d end
Saves the current data structure to the current file handle. @return [void]
# File lib/mexico/file_system/corpus.rb, line 116 def save_xml doc = Nokogiri::XML::Document.new doc.root = @corpus.to_xml open(File.join(@base_path, "Corpus.OUT.xml"), 'w') do |file| file << doc.serialize end # File.open(@corpus_file, 'w') {|f| f.write(doc.to_xml) } end
Private Instance Methods
after parsing a XML corpus representation, this method binds all components to the corpus object.
# File lib/mexico/file_system/corpus.rb, line 145 def after_parse participants.each do |participant| participant.bind_to_corpus(self) end designs.each do |design| design.bind_to_corpus(self) design.design_components.each do |dc| dc.bind_to_corpus(self) end end trials.each do |trial| trial.bind_to_corpus(self) end resources.each do |resource| resource.bind_to_corpus(self) resource.local_files.each do |loc| loc.bind_to_corpus(self) end resource.urls.each do |url| url.bind_to_corpus(self) end end end
helping method to retrieve all existing design components
# File lib/mexico/file_system/corpus.rb, line 203 def design_components @designs.collect{|d| d.design_components}.flatten end
This method inits a new corpus folder in the file system. @param [String] path The path where the folder should be initialized. @param [Hash] opts A hash with additional options and parameters. @option opts [String] :identifier The identifier of the new corpus (required). @return true
if all operations were successful, false otherwise.
# File lib/mexico/file_system/corpus.rb, line 180 def init_folder(path, opts = {}) # check whether the given folder does not exist and is writable unless File.exists?(path) begin FileUtils.mkpath(path) rescue # if something goes wrong, stop here and return false end end corpus_file_name = File.join(path, "Corpus.xml") unless File.exists?(corpus_file_name) corpus_file = File.open(corpus_file_name, 'w+') corpus_file << "<Corpus identifier='#{opts[:identifier]}'></Corpus>" corpus_file.close end # create end