<commit_before>class LookupSecurity
  def initialize(composite_exchange, local_exchange_or_exchanges, catch_all_exchange_or_exchanges)
    @composite_exchange = composite_exchange
    @local_exchanges = [local_exchange_or_exchanges].flatten
    @catch_all_exchanges = [catch_all_exchange_or_exchanges].flatten
  end

  # search for the security
  # (1) in the appropriate composite exchange,
  # (2) in the appropriate constituent (local) exchanges,
  # and finally (3) in the catch-all exchange
  def run(symbol, date)
    # 1. search for the security in the appropriate composite exchange
    begin
      if @composite_exchange
        Security.first(symbol: symbol, exchange_id: @composite_exchange.id)
      end
    end ||
    # 2. if not found in (1), search in the appropriate constituent (local) exchange(s)
    begin
      if @local_exchanges && !@local_exchanges.empty?
        securities_in_local_exchanges = Security.where(symbol: symbol, exchange_id: @local_exchanges.map(&:id)).to_a
        case securities_in_local_exchanges.count
        when 0
          nil
        when 1
          securities_in_local_exchanges.first
        else
          # todo: figure out which exchange is preferred, and then return the security in the most preferred exchange
          raise "Symbol #{symbol} is listed in multiple exchanges: #{securities_in_local_exchanges.map(&:exchange).map(&:label)}"
        end
      end
    end ||
    # 3. if not found in (1) or (2), search in the appropriate catch-all exchange(s)
    begin
      if @catch_all_exchanges && !@catch_all_exchanges.empty?
        securities_in_catch_all_exchanges = Security.
                                              where(symbol: symbol, exchange_id: @catch_all_exchanges.map(&:id)).
                                              where { (start_date <= date) & (end_date >= date) }.
                                              to_a
        case securities_in_catch_all_exchanges.count
        when 0
          nil
        when 1
          securities_in_catch_all_exchanges.first
        else
          raise "Multiple securities in the catch all exchange(s) traded under the symbol '#{symbol}' on #{date}: #{securities_in_catch_all_exchanges.inspect}"
        end
      end
    end
  end
end<commit_msg>adding logic to LookupSecurity class<commit_after>class LookupSecurity
  def initialize(composite_exchange, local_exchange_or_exchanges, catch_all_exchange_or_exchanges)
    @composite_exchange = composite_exchange
    @local_exchanges = [local_exchange_or_exchanges].flatten
    @catch_all_exchanges = [catch_all_exchange_or_exchanges].flatten
  end

  # search for the security
  # (1) in the appropriate composite exchange,
  # (2) in the appropriate constituent (local) exchanges,
  # and finally (3) in the catch-all exchanges
  def run(symbol, date = nil)
    # 1. search for the security in the appropriate composite exchange
    begin
      if @composite_exchange
        Security.first(symbol: symbol, exchange_id: @composite_exchange.id)
      end
    end ||
    # 2. if not found in (1), search in the appropriate constituent (local) exchange(s)
    begin
      if @local_exchanges && !@local_exchanges.empty?
        securities_in_local_exchanges = Security.where(symbol: symbol, exchange_id: @local_exchanges.map(&:id)).to_a
        case securities_in_local_exchanges.count
        when 0
          nil
        when 1
          securities_in_local_exchanges.first
        else
          # todo: figure out which exchange is preferred, and then return the security in the most preferred exchange
          raise "Symbol #{symbol} is listed in multiple exchanges: #{securities_in_local_exchanges.map(&:exchange).map(&:label)}"
        end
      end
    end ||
    # 3. if not found in (1) or (2), search in the appropriate catch-all exchange(s)
    begin
      if @catch_all_exchanges && !@catch_all_exchanges.empty?
        securities_in_catch_all_exchanges = if date
          Security.
            where(symbol: symbol, exchange_id: @catch_all_exchanges.map(&:id)).
            where { (start_date <= date) & (end_date >= date) }.
            to_a
        else
          Security.
            where(symbol: symbol, exchange_id: @catch_all_exchanges.map(&:id)).
            to_a
        end

        case securities_in_catch_all_exchanges.count
        when 0
          nil
        when 1
          securities_in_catch_all_exchanges.first
        else
          raise "Multiple securities in the catch all exchange(s) traded under the symbol '#{symbol}' on #{date}: #{securities_in_catch_all_exchanges.inspect}"
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.07544729467557663, 'fraction_numerical': 0.005604656175899978, 'mean_word_length': 2.697211155378486, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3075880', 'n_tokens_mistral': 1348, 'n_tokens_neox': 1291, 'n_words': 372}