Sha256: 679104603b18b66833145680f1a321e561eb5f608fa213adc1f2c0cde88b2be7

Contents?: true

Size: 1.67 KB

Versions: 1

Compression:

Stored size: 1.67 KB

Contents

require File.expand_path(File.join(File.dirname(__FILE__), '..', 'fathom'))
class Fathom::MCNode < Node
  
  attr_reader :value_description, :samples_taken
  
  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 = 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.0 lib/fathom/mc_node.rb