Class: MiGA::Metadata

Inherits:
MiGA
  • Object
show all
Defined in:
lib/miga/metadata.rb

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

Class Method Summary collapse

Instance Method Summary collapse

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?, root_path, 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.


35
36
37
38
39
40
41
# File 'lib/miga/metadata.rb', line 35

def initialize(path, defaults={})
  @path = File.absolute_path(path)
  @data = {}
  defaults.each_pair{ |k,v| self[k]=v }
  self.create unless File.size? self.path
  self.load
end

Instance Attribute Details

#dataObject (readonly)

Parsed data as a Hash.


30
31
32
# File 'lib/miga/metadata.rb', line 30

def data
  @data
end

#pathObject (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.size? path end

.load(path) ⇒ Object

Load the metadata described in path and return MiGA::Metadata if it exists, or nil otherwise.


17
18
19
20
# File 'lib/miga/metadata.rb', line 17

def self.load(path)
  return nil unless .exist? path
  MiGA::.new(path)
end

Instance Method Details

#[](k) ⇒ Object

Return the value of k in #data.


100
# File 'lib/miga/metadata.rb', line 100

def [](k) data[k.to_sym] end

#[]=(k, v) ⇒ Object

Set the value of k to v.


104
105
106
107
108
109
110
111
112
# File 'lib/miga/metadata.rb', line 104

def []=(k,v)
  k = k.to_sym
  # Protect the special field :name
  v=v.miga_name if k==:name
  # Symbolize the special field :type
  v=v.to_sym if k==:type
  # Register and return
  @data[k]=v
end

#createObject

Reset :created field and save the current data.


45
46
47
48
# File 'lib/miga/metadata.rb', line 45

def create
  @data[:created] = Time.now.to_s
  self.save
end

#each(&blk) ⇒ Object

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


116
# File 'lib/miga/metadata.rb', line 116

def each(&blk) data.each{ |k,v| blk.call(k,v) } ; end

#loadObject

(Re-)load metadata stored in #path.


73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/miga/metadata.rb', line 73

def load
  sleeper = 0.0
  while File.exist? lock_file
    sleeper += 0.1 if sleeper <= 10.0
    sleep(sleeper.to_i)
  end
  # :symbolize_names does not play nicely with :create_additions
  tmp = JSON.parse(File.read(path),
    {:symbolize_names=>false, :create_additions=>true})
  @data = {}
  tmp.each_pair{ |k,v| self[k] = v }
end

#lock_fileObject

Lock file for the metadata.


96
# File 'lib/miga/metadata.rb', line 96

def lock_file ; path + ".lock" ; end

#remove!Object

Delete file at #path.


88
89
90
91
92
# File 'lib/miga/metadata.rb', line 88

def remove!
  MiGA.DEBUG "Metadata.remove! #{self.path}"
  File.unlink(self.path)
  nil
end

#saveObject

Save the metadata into #path.


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/miga/metadata.rb', line 52

def save
  MiGA.DEBUG "Metadata.save #{path}"
  @data[:updated] = Time.now.to_s
  json = JSON.pretty_generate(data)
  sleeper = 0.0
  while File.exist?(lock_file)
    sleeper += 0.1 if sleeper <= 10.0
    sleep(sleeper.to_i)
  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