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.
-
#saved_hash ⇒ Object
readonly
Hash (Integer) of the last saved data Hash (object).
Attributes included from Common::Net
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.
-
#save! ⇒ Object
Force
save
even if nothing has changed since the last save or load. -
#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, #http_request, #known_hosts, #main_server, #net_method, #normalize_encoding, #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.
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/miga/metadata.rb', line 36 def initialize(path, defaults = {}) @data = nil @path = File.absolute_path(path) @saved_hash = nil 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 |
#saved_hash ⇒ Object (readonly)
Hash (Integer) of the last saved data Hash (object)
31 32 33 |
# File 'lib/miga/metadata.rb', line 31 def saved_hash @saved_hash 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
118 119 120 121 122 123 124 |
# File 'lib/miga/metadata.rb', line 118 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
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/miga/metadata.rb', line 128 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
56 57 58 59 |
# File 'lib/miga/metadata.rb', line 56 def create self[:created] = Time.now.to_s save end |
#created ⇒ Object
Time of creation
159 160 161 |
# File 'lib/miga/metadata.rb', line 159 def created Time.parse(self[:created]) unless self[:created].nil? end |
#data ⇒ Object
Parsed data as a Hash
49 50 51 52 |
# File 'lib/miga/metadata.rb', line 49 def data self.load if @data.nil? @data end |
#each(&blk) ⇒ Object
Iterate blk
for each data with 2 arguments: key and value
147 148 149 |
# File 'lib/miga/metadata.rb', line 147 def each(&blk) data.each { |k, v| blk.call(k, v) } end |
#load ⇒ Object
(Re-)load metadata stored in #path
94 95 96 97 98 99 100 |
# File 'lib/miga/metadata.rb', line 94 def load wait_for_lock tmp = MiGA::Json.parse(path, additions: true) @data = {} tmp.each { |k, v| self[k] = v } @saved_hash = data.hash end |
#lock_file ⇒ Object
Lock file for the metadata
112 113 114 |
# File 'lib/miga/metadata.rb', line 112 def lock_file "#{path}.lock" end |
#remove! ⇒ Object
Delete file at #path
104 105 106 107 108 |
# File 'lib/miga/metadata.rb', line 104 def remove! MiGA.DEBUG "Metadata.remove! #{path}" File.unlink(path) if File.exist?(path) nil end |
#save ⇒ Object
Save the metadata into #path
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/miga/metadata.rb', line 63 def save return if self[:never_save] return if !saved_hash.nil? && saved_hash == data.hash MiGA::MiGA.DEBUG "Metadata.save #{path}" path_tmp = "#{path}.tmp" self[:updated] = Time.now.to_s @saved_hash = data.hash json = to_json wait_for_lock FileUtils.touch(lock_file) File.open(path_tmp, 'w') { |ofh| ofh.puts json } 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 |
#save! ⇒ Object
Force save
even if nothing has changed since the last save or load. However, it doesn’t save if :never_save
is true.
87 88 89 90 |
# File 'lib/miga/metadata.rb', line 87 def save! @saved_hash = nil save end |
#to_json ⇒ Object
Show contents in JSON format as a String
165 166 167 |
# File 'lib/miga/metadata.rb', line 165 def to_json MiGA::Json.generate(data) end |
#updated ⇒ Object
Time of last update
153 154 155 |
# File 'lib/miga/metadata.rb', line 153 def updated Time.parse(self[:updated]) unless self[:updated].nil? end |