Method: ActiveFacts::API::Constellation#with_candidates

Defined in:
lib/activefacts/api/constellation.rb

#with_candidates(&b) ⇒ Object

Candidates is an array of object instances that do not already exist in the constellation but will be added if an assertion succeeds. After the assertion is found to be acceptable, these objects are indexed in the constellation and in the counterparts of their identifying roles, and the candidates array is nullified.



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/activefacts/api/constellation.rb', line 130

def with_candidates &b
  # Multiple assignment reduces (s)teps while debugging
  outermost, @candidates, @on_admission = @candidates.nil?, (@candidates || []), (@on_admission || [])
  begin
    b.call
  rescue Exception
    # Do not accept any of these candidates, there was a problem:
    @candidates = [] if outermost
    raise
  ensure
    if outermost
      while @candidates
        # Index the accepted instances in the constellation:
        candidates = @candidates
        on_admission = @on_admission
        @candidates = nil
        @on_admission = nil
        candidates.each do |instance|
          instance.class.index_instance(self, instance)
          loggers.each{|l| l.call(:assert, instance.class, instance.identifying_role_values)}
        end
        on_admission.each do |b|
          b.call
        end
      end
    end
  end
end