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
path
already exist?. -
.load(path) ⇒ Object
Load the metadata described in
path
and return MiGA::Metadata if it exists, or nil otherwise.
Instance Method Summary collapse
-
#[](k) ⇒ Object
Return the value of
k
in #data. -
#[]=(k, v) ⇒ Object
Set the value of
k
tov
. -
#create ⇒ Object
Reset :created field and save the current data.
-
#created ⇒ Object
Time of creation.
-
#data ⇒ Object
Parsed data as a Hash.
-
#each(&blk) ⇒ Object
Iterate
blk
for 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.
-
#updated ⇒ Object
Time of last update.
Methods inherited from MiGA
CITATION, CITATION_ARRAY, DEBUG, DEBUG_OFF, DEBUG_ON, DEBUG_TRACE_OFF, DEBUG_TRACE_ON, FULL_VERSION, LONG_VERSION, VERSION, VERSION_DATE, #advance, debug?, debug_trace?, initialized?, #like_io?, #num_suffix, rc_path, #result_files_exist?, #say
Methods included from Common::Path
Methods included from Common::Format
#clean_fasta_file, #seqs_length, #tabulate
Methods included from Common::Net
#download_file_ftp, #known_hosts, #remote_connection
Methods included from Common::SystemCall
Constructor Details
#initialize(path, defaults = {}) ⇒ Metadata
Initiate a MiGA::Metadata object with description in path
. It will create it if it doesn’t exist.
32 33 34 35 36 37 38 39 40 |
# File 'lib/miga/metadata.rb', line 32 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
27 28 29 |
# File 'lib/miga/metadata.rb', line 27 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
107 108 109 110 111 112 113 |
# File 'lib/miga/metadata.rb', line 107 def [](k) if k.to_s =~ /(.+):(.+)/ data[$1.to_sym]&.fetch($2) else data[k.to_sym] end end |
#[]=(k, v) ⇒ Object
Set the value of k
to v
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/miga/metadata.rb', line 117 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
51 52 53 54 |
# File 'lib/miga/metadata.rb', line 51 def create self[:created] = Time.now.to_s save end |
#created ⇒ Object
Time of creation
148 149 150 |
# File 'lib/miga/metadata.rb', line 148 def created Time.parse(self[:created]) unless self[:created].nil? end |
#data ⇒ Object
Parsed data as a Hash
44 45 46 47 |
# File 'lib/miga/metadata.rb', line 44 def data self.load if @data.nil? @data end |
#each(&blk) ⇒ Object
Iterate blk
for each data with 2 arguments: key and value
136 137 138 |
# File 'lib/miga/metadata.rb', line 136 def each(&blk) data.each { |k, v| blk.call(k, v) } end |
#load ⇒ Object
(Re-)load metadata stored in #path
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/miga/metadata.rb', line 80 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
101 102 103 |
# 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 |
#save ⇒ Object
Save the metadata into #path
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/miga/metadata.rb', line 58 def save return if self[:never_save] MiGA::MiGA.DEBUG "Metadata.save #{path}" self[:updated] = Time.now.to_s json = to_json wait_for_lock FileUtils.touch(lock_file) ofh = File.open("#{path}.tmp", 'w') ofh.puts json ofh.close unless File.exist?("#{path}.tmp") && File.exist?(lock_file) raise "Lock-racing detected for #{path}" end File.rename("#{path}.tmp", path) File.unlink(lock_file) end |
#to_json ⇒ Object
Show contents in JSON format as a String
154 155 156 |
# File 'lib/miga/metadata.rb', line 154 def to_json MiGA::Json.generate(data) end |
#updated ⇒ Object
Time of last update
142 143 144 |
# File 'lib/miga/metadata.rb', line 142 def updated Time.parse(self[:updated]) unless self[:updated].nil? end |