Class: MiGA::Metadata
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
-
#path ⇒ Object
readonly
Path to the JSON file describing the metadata.
Class Method Summary collapse
-
.exist?(path) ⇒ Boolean
Does the metadata described in
pathalready exist?. -
.load(path) ⇒ Object
Load the metadata described in
pathand return MiGA::Metadata if it exists, or nil otherwise.
Instance Method Summary collapse
-
#[](k) ⇒ Object
Return the value of
kin #data. -
#[]=(k, v) ⇒ Object
Set the value of
ktov. -
#create ⇒ Object
Reset :created field and save the current data.
-
#data ⇒ Object
Parsed data as a Hash.
-
#each(&blk) ⇒ Object
Iterate
blkfor each data with 2 arguments: key and value. -
#initialize(path, defaults = {}) ⇒ Metadata
constructor
Initiate a MiGA::Metadata object with description in
path. -
#load ⇒ Object
(Re-)load metadata stored in #path.
-
#lock_file ⇒ Object
Lock file for the metadata.
-
#remove! ⇒ Object
Delete file at #path.
-
#save ⇒ Object
Save the metadata into #path.
-
#to_json ⇒ Object
Show contents in JSON format as a String.
Methods inherited from MiGA
CITATION, DEBUG, DEBUG_OFF, DEBUG_ON, DEBUG_TRACE_OFF, DEBUG_TRACE_ON, FULL_VERSION, LONG_VERSION, VERSION, VERSION_DATE, initialized?, #result_files_exist?
Methods included from Common::Path
Methods included from Common::Format
#clean_fasta_file, #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 { |k,v| self[k] = v } create end end |
Instance Attribute Details
#path ⇒ Object (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?
12 |
# File 'lib/miga/metadata.rb', line 12 def self.exist?(path) File.exist? path end |
Instance Method Details
#[](k) ⇒ Object
Return the value of k in #data
109 110 111 |
# File 'lib/miga/metadata.rb', line 109 def [](k) data[k.to_sym] end |
#[]=(k, v) ⇒ Object
Set the value of k to v
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/miga/metadata.rb', line 115 def []=(k, v) self.load if @data.nil? k = k.to_sym return @data.delete(k) if v.nil? case k when :name # Protect the special field :name v = v.miga_name when :type # Symbolize the special field :type v = v.to_sym if k == :type end @data[k] = v end |
#create ⇒ Object
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 |
#data ⇒ Object
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
134 135 136 |
# File 'lib/miga/metadata.rb', line 134 def each(&blk) data.each { |k, v| blk.call(k, v) } end |
#load ⇒ Object
(Re-)load metadata stored in #path
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/miga/metadata.rb', line 82 def load sleeper = 0.0 while File.exist? lock_file sleeper += 0.1 if sleeper <= 10.0 sleep(sleeper.to_i) end tmp = MiGA::Json.parse(path, additions: true) @data = {} tmp.each { |k, v| self[k] = v } end |
#lock_file ⇒ Object
Lock file for the metadata
103 104 105 |
# File 'lib/miga/metadata.rb', line 103 def lock_file "#{path}.lock" end |
#remove! ⇒ Object
Delete file at #path
95 96 97 98 99 |
# File 'lib/miga/metadata.rb', line 95 def remove! MiGA.DEBUG "Metadata.remove! #{path}" File.unlink(path) nil end |
#save ⇒ Object
Save the metadata into #path
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/miga/metadata.rb', line 57 def save MiGA.DEBUG "Metadata.save #{path}" self[:updated] = Time.now.to_s json = to_json sleeper = 0.0 slept = 0 while File.exist?(lock_file) MiGA::MiGA.DEBUG "Waiting for lock: #{lock_file}" sleeper += 0.1 if sleeper <= 10.0 sleep(sleeper.to_i) slept += sleeper.to_i raise "Lock detected for over 10 minutes: #{lock_file}" if slept > 600 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 |