class MiGA::Metadata

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

Attributes

path[R]

Path to the JSON file describing the metadata

Public Class Methods

exist?(path) click to toggle source

Does the metadata described in path already exist?

# File lib/miga/metadata.rb, line 12
def self.exist?(path) File.exist? path end
load(path) click to toggle source

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
new(path, defaults = {}) click to toggle source

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

# 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

Public Instance Methods

[](k) click to toggle source

Return the value of k in data

# File lib/miga/metadata.rb, line 109
def [](k)
  data[k.to_sym]
end
[]=(k, v) click to toggle source

Set the value of k to v

# 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() click to toggle source

Reset :created field and save the current data

# File lib/miga/metadata.rb, line 50
def create
  self[:created] = Time.now.to_s
  save
end
data() click to toggle source

Parsed data as a Hash

# File lib/miga/metadata.rb, line 43
def data
  self.load if @data.nil?
  @data
end
each(&blk) click to toggle source

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

# File lib/miga/metadata.rb, line 134
def each(&blk)
  data.each { |k, v| blk.call(k, v) }
end
load() click to toggle source

(Re-)load metadata stored in path

# 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() click to toggle source

Lock file for the metadata

# File lib/miga/metadata.rb, line 103
def lock_file
  "#{path}.lock"
end
remove!() click to toggle source

Delete file at path

# File lib/miga/metadata.rb, line 95
def remove!
  MiGA.DEBUG "Metadata.remove! #{path}"
  File.unlink(path)
  nil
end
save() click to toggle source

Save the metadata into path

# 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
to_json() click to toggle source

Show contents in JSON format as a String

# File lib/miga/metadata.rb, line 140
def to_json
  MiGA::Json.generate(data)
end