Class: MiGA::Metadata

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

Overview

Metadata associated to objects like MiGA::Project, MiGA::Dataset, and MiGA::Result.

Constant Summary

Constants included from MiGA

CITATION, VERSION, VERSION_DATE, VERSION_NAME

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from MiGA

CITATION, DEBUG, DEBUG_OFF, DEBUG_ON, DEBUG_TRACE_OFF, DEBUG_TRACE_ON, FULL_VERSION, LONG_VERSION, VERSION, VERSION_DATE, clean_fasta_file, initialized?, #result_files_exist?, root_path, script_path, seqs_length, tabulate

Constructor Details

#initialize(path, defaults = {}) ⇒ Metadata

Initiate a MiGA::Metadata object with description in path. It will create it if it doesn’t exist.



31
32
33
34
35
36
37
38
39
# File 'lib/miga/metadata.rb', line 31

def initialize(path, defaults={})
  @data = nil
  @path = File.absolute_path(path)
  unless File.exist? path
    @data = {}
    defaults.each_pair{ |k,v| self[k]=v }
    create
  end
end

Instance Attribute Details

#pathObject (readonly)

Path to the JSON file describing the metadata.



26
27
28
# File 'lib/miga/metadata.rb', line 26

def path
  @path
end

Class Method Details

.exist?(path) ⇒ Boolean

Does the metadata described in path already exist?

Returns:

  • (Boolean)


12
# File 'lib/miga/metadata.rb', line 12

def self.exist?(path) File.exist? path end

.load(path) ⇒ Object

Load the metadata described in path and return MiGA::Metadata if it exists, or nil otherwise.



17
18
19
20
# File 'lib/miga/metadata.rb', line 17

def self.load(path)
  return nil unless Metadata.exist? path
  MiGA::Metadata.new(path)
end

Instance Method Details

#[](k) ⇒ Object

Return the value of k in #data.



105
# File 'lib/miga/metadata.rb', line 105

def [](k) data[k.to_sym] end

#[]=(k, v) ⇒ Object

Set the value of k to v.



109
110
111
112
113
114
115
116
117
118
# File 'lib/miga/metadata.rb', line 109

def []=(k,v)
  self.load if @data.nil?
  k = k.to_sym
  # Protect the special field :name
  v=v.miga_name if k==:name
  # Symbolize the special field :type
  v=v.to_sym if k==:type
  # Delete if nil, register, and return
  v.nil? ? @data.delete(k) : (@data[k]=v)
end

#createObject

Reset :created field and save the current data.



50
51
52
53
# File 'lib/miga/metadata.rb', line 50

def create
  self[:created] = Time.now.to_s
  save
end

#dataObject

Parsed data as a Hash.



43
44
45
46
# File 'lib/miga/metadata.rb', line 43

def data
  self.load if @data.nil?
  @data
end

#each(&blk) ⇒ Object

Iterate blk for each data with 2 arguments key and value.



122
# File 'lib/miga/metadata.rb', line 122

def each(&blk) data.each{ |k,v| blk.call(k,v) } ; end

#loadObject

(Re-)load metadata stored in #path.



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/miga/metadata.rb', line 78

def load
  sleeper = 0.0
  while File.exist? lock_file
    sleeper += 0.1 if sleeper <= 10.0
    sleep(sleeper.to_i)
  end
  # :symbolize_names does not play nicely with :create_additions
  tmp = JSON.parse(File.read(path),
    {:symbolize_names=>false, :create_additions=>true})
  @data = {}
  tmp.each_pair{ |k,v| self[k] = v }
end

#lock_fileObject

Lock file for the metadata.



101
# File 'lib/miga/metadata.rb', line 101

def lock_file ; "#{path}.lock" ; end

#remove!Object

Delete file at #path.



93
94
95
96
97
# File 'lib/miga/metadata.rb', line 93

def remove!
  MiGA.DEBUG "Metadata.remove! #{path}"
  File.unlink(path)
  nil
end

#saveObject

Save the metadata into #path.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/miga/metadata.rb', line 57

def save
  MiGA.DEBUG "Metadata.save #{path}"
  self[:updated] = Time.now.to_s
  json = JSON.pretty_generate(data)
  sleeper = 0.0
  while File.exist?(lock_file)
    sleeper += 0.1 if sleeper <= 10.0
    sleep(sleeper.to_i)
  end
  FileUtils.touch lock_file
  ofh = File.open("#{path}.tmp", "w")
  ofh.puts json
  ofh.close
  raise "Lock-racing detected for #{path}." unless
    File.exist?("#{path}.tmp") and File.exist?(lock_file)
  File.rename("#{path}.tmp", path)
  File.unlink(lock_file)
end