Class: MetaCon::SavedState

Inherits:
Object
  • Object
show all
Defined in:
lib/metacon/project.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mcdir) ⇒ SavedState

Returns a new instance of SavedState.



175
176
177
178
179
180
# File 'lib/metacon/project.rb', line 175

def initialize(mcdir)
  raise "#{mcdir} not found" unless File.directory?(mcdir)
  @fstate = File.join(mcdir,'current_state')
  @in_atomic = @dirty = false
  @state = nil
end

Instance Attribute Details

#dirtyObject

Returns the value of attribute dirty.



174
175
176
# File 'lib/metacon/project.rb', line 174

def dirty
  @dirty
end

#stateObject

Returns the value of attribute state.



174
175
176
# File 'lib/metacon/project.rb', line 174

def state
  @state
end

Instance Method Details

#[](key) ⇒ Object



218
219
220
221
222
223
224
225
226
# File 'lib/metacon/project.rb', line 218

def [](key)
  if @in_atomic
    @state[key]
  elsif @state.nil?
    @state = readonly
  else
    @state[key]
  end
end

#[]=(key, val) ⇒ Object



228
229
230
231
232
233
234
235
236
237
# File 'lib/metacon/project.rb', line 228

def []=(key,val)
  if @in_atomic
    @state[key] = val
    @dirty = true
  else
    atomic do |s|
      s[key] = val
    end
  end
end

#atomic(&block) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/metacon/project.rb', line 190

def atomic(&block)
  @in_atomic = true
  `touch #{@fstate}` # Guarantee exists and change timestamps
  File.open(@fstate,'r+') do |f|
    f.flock File::LOCK_EX
    @state = YAML::load(f.read)
    @state ||= blank_initial_state
    yield self
    if @dirty
      f.pos = 0
      f.print @state.to_yaml
      f.truncate(f.pos)
    end
  end
  @dirty = false
  @in_atomic = false
end

#blank_initial_stateObject



182
183
184
185
186
187
188
# File 'lib/metacon/project.rb', line 182

def blank_initial_state
  @dirty = true
  {:role => 'main',
   :rtc => 'dev',
   :os => '(this)',
   :host => '(this)'}
end

#readonlyObject



208
209
210
211
212
213
214
215
216
# File 'lib/metacon/project.rb', line 208

def readonly
  if File.exists?(@fstate)
    res = YAML::load_file(@fstate)
    res ||= blank_initial_state
  else
    res = blank_initial_state
  end
  return res
end