Metadata associated to objects like MiGA::Project, MiGA::Dataset, and MiGA::Result.
Path to the JSON file describing the metadata
Does the metadata described in path
already exist?
# File lib/miga/metadata.rb, line 12 def self.exist?(path) File.exist? path end
Load the metadata described in path
and return MiGA::Metadata if it exists, or nil otherwise.
# File lib/miga/metadata.rb, line 17 def self.load(path) return nil unless Metadata.exist? path MiGA::Metadata.new(path) end
Initiate a MiGA::Metadata object with
description in path
. It will create it if it doesn't
exist.
# 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
Return the value of k
in data
# 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
Set the value of k
to v
# 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
Reset :created field and save the current data
# File lib/miga/metadata.rb, line 51 def create self[:created] = Time.now.to_s save end
Time of creation
# File lib/miga/metadata.rb, line 148 def created Time.parse(self[:created]) unless self[:created].nil? end
Parsed data as a Hash
# File lib/miga/metadata.rb, line 44 def data self.load if @data.nil? @data end
Iterate blk
for each data with 2 arguments: key and value
# File lib/miga/metadata.rb, line 136 def each(&blk) data.each { |k, v| blk.call(k, v) } end
(Re-)load metadata stored in path
# 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 for the metadata
# File lib/miga/metadata.rb, line 101 def lock_file "#{path}.lock" end
Delete file at path
# File lib/miga/metadata.rb, line 93 def remove! MiGA.DEBUG "Metadata.remove! #{path}" File.unlink(path) nil end
Save the metadata into path
# 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
Show contents in JSON format as a String
# File lib/miga/metadata.rb, line 154 def to_json MiGA::Json.generate(data) end
Time of last update
# File lib/miga/metadata.rb, line 142 def updated Time.parse(self[:updated]) unless self[:updated].nil? end