Class: Prick::State
Instance Attribute Summary
Attributes inherited from PrickFile
#path
Instance Method Summary
collapse
Methods inherited from PrickFile
#exist?
Constructor Details
#initialize(path, **fields) ⇒ State
fields is a Hash from field name (Symbol) to field type (class). Eg. { f: Integer }
20
21
22
23
24
|
# File 'lib/prick/state.rb', line 20
def initialize(path, **fields)
super(path)
@fields = fields
@fields.each_key { |k| self.class.attr_accessor k }
end
|
Instance Method Details
#create ⇒ Object
26
|
# File 'lib/prick/state.rb', line 26
def create() write end
|
#read(branch: nil, tag: nil) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/prick/state.rb', line 34
def read(branch: nil, tag: nil)
if branch.nil?
hash = YAML.load(do_read(branch: branch, tag: tag))
for field, klass in @fields
value = hash[field.to_s]
value = Version.new(value) if klass == Version && !value.nil?
self.instance_variable_set("@#{field}", value)
end
else
raise NotYet
end
self
end
|
#set(**fields) ⇒ Object
28
29
30
31
32
|
# File 'lib/prick/state.rb', line 28
def set(**fields)
for field, value in fields
self.send(:"#{field}=", value)
end
end
|
#write ⇒ Object
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/prick/state.rb', line 48
def write
hash = @fields.map { |field, klass|
value = self.send(field)
value = value.to_s if klass == Version && !value.nil?
[field.to_s, value]
}.to_h
IO.write(@path, YAML.dump(hash))
raise if @version.is_a?(Array)
self
end
|