Sha256: 81094c4a09f6b279df57bb106f8e8e843dd4c1236118d63ed844fff037dab475

Contents?: true

Size: 1.52 KB

Versions: 3

Compression:

Stored size: 1.52 KB

Contents

# Copyright 2013 Google Inc. All Rights Reserved.
# Licensed under the Apache License, Version 2.0, found in the LICENSE file.

FactoryGirl.define do
  factory :allocation, class: AllocationStats::Allocation do

    ignore do
      object "A String"
      sourcefile "foo/bar.rb"
      sourceline 7
    end

    initialize_with { new(object) }

    after(:build) do |allocation, evaluator|
      allocation.instance_variable_set(:@sourcefile, evaluator.sourcefile)
      allocation.instance_variable_set(:@sourceline, evaluator.sourceline)
    end
  end

  # This might be overly complicated for now... but it was terribly fun to learn
  # FactoryGirl's API, and I'll leave it for future use...
  factory :stats, class: AllocationStats do
    ignore do
      size 5
      files ["foo/bar.rb", "baz/quux.rb"]
      sourceline 7
    end

    after(:build) do |stats, evaluator|
      allocations = []
      size = evaluator.size
      files_count = evaluator.files.size

      # 3 from 1st file
      if size > 0
        file = evaluator.files[0]
        count = [size, 3].min
        count.times { allocations << FactoryGirl.build(:allocation, sourcefile: file, sourceline: evaluator.sourceline) }
        size -= count
      end

      # more from 2nd file
      if size > 0
        file = evaluator.files[1 % files_count]
        size.times { allocations << FactoryGirl.build(:allocation, sourcefile: file, sourceline: evaluator.sourceline) }
        size -= count
      end

      stats.instance_variable_set(:@new_allocations, allocations)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rack-allocation_stats-0.1.2 spec/factories.rb
rack-allocation_stats-0.1.1 ./spec/factories.rb
rack-allocation_stats-0.1.0 ./spec/factories.rb