Class: Bacon::Specification

Inherits:
Object show all
Defined in:
lib/mac_bacon.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, description, block, before_filters, after_filters) ⇒ Specification

Returns a new instance of Specification.



134
135
136
137
138
139
140
141
# File 'lib/mac_bacon.rb', line 134

def initialize(context, description, block, before_filters, after_filters)
  @context, @description, @block = context, description, block
  @before_filters, @after_filters = before_filters.dup, after_filters.dup

  @postponed_blocks_count = 0
  @exception_occurred = false
  @error = ""
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



132
133
134
# File 'lib/mac_bacon.rb', line 132

def description
  @description
end

Instance Method Details

#execute_blockObject



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/mac_bacon.rb', line 192

def execute_block
  begin
    yield
  rescue Object => e
    @exception_occurred = true

    ErrorLog << "#{e.class}: #{e.message}\n"
    e.backtrace.find_all { |line| line !~ /bin\/bacon|\/bacon\.rb:\d+/ }.
      each_with_index { |line, i|
      ErrorLog << "\t#{line}#{i==0 ? ": #{@context.name} - #{@description}" : ""}\n"
    }
    ErrorLog << "\n"

    @error = if e.kind_of? Error
      Counter[e.count_as] += 1
      e.count_as.to_s.upcase
    else
      Counter[:errors] += 1
      "ERROR: #{e.class}"
    end
  end
end

#finalizeObject



178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/mac_bacon.rb', line 178

def finalize
  if Counter[:requirements] == @number_of_requirements_before
    # the specification did not contain any requirements, so it flunked
    # TODO ugh, exceptions for control flow, need to clean this up
    execute_block { raise Error.new(:missing, "empty specification: #{@context.name} #{@description}") }
  end

  execute_block { run_after_filters }

  Counter[:depth] -= 1
  Bacon.handle_requirement_end(@error)
  @context.specification_did_finish(self)
end

#postpone_block(seconds, &block) ⇒ Object



163
164
165
166
167
168
169
# File 'lib/mac_bacon.rb', line 163

def postpone_block(seconds, &block)
  # If an exception occurred, we definitely don't need to schedule any more blocks
  unless @exception_occurred
    @postponed_blocks_count += 1
    performSelector("run_postponed_block:", withObject:block, afterDelay:seconds)
  end
end

#runObject



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/mac_bacon.rb', line 151

def run
  Bacon.handle_requirement_begin(@description)
  execute_block do
    Counter[:depth] += 1
    run_before_filters
    @number_of_requirements_before = Counter[:requirements]
    @context.instance_eval(&@block)
  end

  finalize if @postponed_blocks_count == 0
end

#run_after_filtersObject



147
148
149
# File 'lib/mac_bacon.rb', line 147

def run_after_filters
  @after_filters.each { |f| @context.instance_eval(&f) }
end

#run_before_filtersObject



143
144
145
# File 'lib/mac_bacon.rb', line 143

def run_before_filters
  @before_filters.each { |f| @context.instance_eval(&f) }
end

#run_postponed_block(block) ⇒ Object



171
172
173
174
175
176
# File 'lib/mac_bacon.rb', line 171

def run_postponed_block(block)
  # If an exception occurred, we definitely don't need execute any more blocks
  execute_block(&block) unless @exception_occurred
  @postponed_blocks_count -= 1
  finalize if @postponed_blocks_count == 0
end