<commit_before>module MechanicalTurkHelper
  class GqlTester
    attr_reader :query_interface

    def initialize(query_interface)
      @_memo = {}
      @query_interface = query_interface
    end
    
    def move_slider(input, value)
      query_interface.execute_input(input, value)
    end

    def custom(query)
      @_memo[query] ||= query_interface.query(query)
    end

    def custom_update(query, value)
      input = Input.new(:key => 'custom', :query => query)
      move_slider(input, value)
    end
    
    def execute(query)
      if query.include?("(")
        custom(query)
      else
        custom("Q(#{query})")
      end
    end

    def method_missing(name, *args)
      custom("Q(#{name})")
    end
  end

  def some_tolerance
    @some_tolerance ||= ENV.fetch('TOLERANCE', 3.0)
  end

  def the_present
    @present.execute(example.description)
  end

  def the_future
    @future.execute(example.description)
  end

  def the_relative_increase
    (((the_future / the_present) - 1.0) * 100.0).round(1)
  end

  def the_absolute_increase
    (the_future - the_present).round(1)
  end

  def load_scenario(options = {}, &block)
    NastyCache.instance.expire!
    @scenario = Scenario.new(Scenario.default_attributes.merge options)
    Current.scenario = @scenario
    @scenario.build_update_statements
    @gql = @scenario.gql(prepare: false)
    # @gql.sandbox_mode = :console
    @gql.init_datasets
    @future  = GqlTester.new(@gql.future)
    @present = GqlTester.new(@gql.present)
    @gql.update_graphs
    instance_eval(&block) if block_given?
    @gql.calculate_graphs
  end

  # when passing a dynamically created input, make sure
  # to assign lookup_id and update_period.
  def move_slider(input, value)
    input = case input
            when Input then input
            when Numeric then Input.get(id)
            end

    @gql.update_graph(@gql.future,  input, value) if input.updates_future?
    @gql.update_graph(@gql.present, input, value) if input.updates_present?
    
  end
end<commit_msg>Make turk specs calculation lazy loaded, to allow for custom sliders.<commit_after>module MechanicalTurkHelper
  class GqlTester
    attr_reader :query_interface

    def initialize(query_interface)
      @_memo = {}
      @query_interface = query_interface
    end
    
    def move_slider(input, value)
      query_interface.execute_input(input, value)
    end

    def custom(query)
      @_memo[query] ||= query_interface.query(query)
    end

    def custom_update(query, value)
      input = Input.new(:key => 'custom', :query => query)
      move_slider(input, value)
    end
    
    def execute(query)
      if query.include?("(")
        custom(query)
      else
        custom("Q(#{query})")
      end
    end

    def method_missing(name, *args)
      custom("Q(#{name})")
    end
  end

  def some_tolerance
    @some_tolerance ||= ENV.fetch('TOLERANCE', 3.0)
  end

  def the_present
    lazy_load_calculate
    @present.execute(example.description)
  end

  def the_future
    lazy_load_calculate
    @future.execute(example.description)
  end

  def the_relative_increase
    (((the_future / the_present) - 1.0) * 100.0).round(1)
  end

  def the_absolute_increase
    (the_future - the_present).round(1)
  end

  def lazy_load_calculate
    @gql.calculate_graphs unless @gql.calculated?
  end

  def load_scenario(options = {}, &block)
    NastyCache.instance.expire!
    @scenario = Scenario.new(Scenario.default_attributes.merge options)
    Current.scenario = @scenario
    @scenario.build_update_statements
    @gql = @scenario.gql(prepare: false)
    # @gql.sandbox_mode = :console
    @gql.init_datasets
    @future  = GqlTester.new(@gql.future)
    @present = GqlTester.new(@gql.present)
    @gql.update_graphs
    instance_eval(&block) if block_given?
    # lazy_load_calculate 
    # Above is called when the first numbers are requested through the_future.
    # this allows us to have custom sliders inside "should" do (This works
    # when we initialize load_scenario inside before(:each) and not before(:all)
  end

  # when passing a dynamically created input, make sure
  # to assign lookup_id and update_period.
  def move_slider(input, value)
    input = case input
            when Input then input
            when Numeric then Input.get(input)
            end

    @gql.update_graph(@gql.future,  input, value) if input.updates_future?
    @gql.update_graph(@gql.present, input, value) if input.updates_present?
    
  end
end
{'fraction_non_alphanumeric': 0.09844444444444445, 'fraction_numerical': 0.0044444444444444444, 'mean_word_length': 3.033154121863799, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '960660', 'n_tokens_mistral': 1520, 'n_tokens_neox': 1459, 'n_words': 331}