Sha256: fa86ac835e3f23828be64563ab3527a1b724d8db9af5e65bd4f2657c9eedaab7

Contents?: true

Size: 1.7 KB

Versions: 1

Compression:

Stored size: 1.7 KB

Contents

require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'fathom'))
class Fathom::MCNode < Fathom::Node
  
  attr_reader :samples_taken
  attr_accessor :value_description
  
  def initialize(opts={}, &block) 
    super(opts)
    @value_description = opts[:value_description]
    @value_description ||= block if block_given?
    raise ArgumentError, "Must provide a value_description from either a parameter or by passing in a block" unless
      @value_description
  end
  
  def process(n=10_000)
    @samples_taken, @samples = n, {}
    @samples_taken.times do
      result = value_description.call(self)
      store(result)
    end
    assert_nodes
  end
  
  def reset!
    @samples_taken, @samples = nil, {}
    @samples_asserted = false
  end
  
  def fields
    self.children.map {|c| c.name_sym}.compact
  end
  
  protected
    
    def store(result)
      result = assert_result_hash(result)
      assert_samples(result)
      result.each do |key, value|
        @samples[key.to_sym] << value
      end
    end
    
    def assert_samples(result)
      return true if @samples_asserted
      result.each do |k, v|
        @samples[k.to_sym] ||= []
      end
      @samples_asserted = true
    end
    
    def assert_result_hash(result)
      result.is_a?(Hash) ? result : {:result => result}
    end
    
    # Assumes the same value description for all samples taken
    def assert_nodes
      @samples.each do |key, values|
        node = Fathom::DataNode.new(:name => key, :values => values)
        add_child(node)
#        self.class.define_summary_method(key)
      end
    end
    
end

if __FILE__ == $0
  include Fathom
  # TODO: Is there anything you want to do to run this file on its own?
  # MCNode.new
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fathom-0.3.7 lib/fathom/node/mc_node.rb