Class: LogicalConstruct::ResolutionServer::PlanRecords

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/logical-construct/target/plan-records.rb

Defined Under Namespace

Classes: Directories, Record

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePlanRecords

Returns a new instance of PlanRecords.



13
14
15
16
17
# File 'lib/logical-construct/target/plan-records.rb', line 13

def initialize
  @record_lock = Mutex.new
  @records = []
  @directories = Directories.new(nil, nil, nil)
end

Instance Attribute Details

#directoriesObject (readonly)

Returns the value of attribute directories.



18
19
20
# File 'lib/logical-construct/target/plan-records.rb', line 18

def directories
  @directories
end

Instance Method Details

#add(name, hash) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/logical-construct/target/plan-records.rb', line 52

def add(name, hash)
  record = nil
  @record_lock.synchronize do
    if @records.any?{|record| record.name == name}
      raise "Cannot add a second plan requirement for #{name}"
    end
    record = States::Unresolved.new(self, Record.new(name, hash))
    @records << record
  end
  record.resolve

  return find(name)
end

#change(old_state, new_state_class) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/logical-construct/target/plan-records.rb', line 66

def change(old_state, new_state_class)
  unless old_state.alive?
    raise "Tried to change from old invalid state: #{old_state}"
  end
  new_state = new_state_class.new(self, old_state.record)

  @record_lock.synchronize do
    @records.delete(old_state)
    @records << new_state
    old_state.cancel!
  end
  new_state.enter

  return new_state
end

#clear_files(directory) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/logical-construct/target/plan-records.rb', line 30

def clear_files(directory)
  dirpath = Pathname.new(directory)
  dirpath.mkpath
  dirpath.each_child do |delivered|
    delivered.delete
  end
end

#each(&block) ⇒ Object



48
49
50
# File 'lib/logical-construct/target/plan-records.rb', line 48

def each(&block)
  @records.each(&block)
end

#find(name) ⇒ Object



44
45
46
# File 'lib/logical-construct/target/plan-records.rb', line 44

def find(name)
  record = @records.find{|record| record.name == name}
end

#reset!Object Also known as: reset



20
21
22
23
24
25
26
27
# File 'lib/logical-construct/target/plan-records.rb', line 20

def reset!
  each do |record|
    record.cancel!
  end
  @records.clear
  clear_files(directories.delivered)
  clear_files(directories.current)
end

#total_stateObject



38
39
40
41
42
# File 'lib/logical-construct/target/plan-records.rb', line 38

def total_state
  return "no-plans-yet" if @records.empty?
  return "resolved" if @records.all?(&:resolved?)
  return "unresolved"
end