<commit_before>require 'pathname'
module Exegesis
  class Design
    include Exegesis::Document
    
    def self.design_directory= dir
      @design_directory = Pathname.new(dir)
    end
    
    def self.design_directory
      @design_directory ||= Pathname.new("designs/#{design_name}")
    end
    
    def self.design_name= n
      @design_name = n.to_s
    end
    
    def self.design_name
      @design_name ||= name.scan(/^(?:[A-Za-z0-9\:]+::)([A-Za-z0-9]+)Design$/).first.first.downcase
    end
    
    def self.compose_canonical
      Dir[design_directory + 'views' + '**/*.js'].each do |view_func|
        path = view_func.split('/')
        func = path.pop.sub(/\.js$/,'')
        name = path.pop
        canonical_design['views'][name] ||= {}
        canonical_design['views'][name][func] = File.read(view_func)
      end
    end
    
    def self.canonical_design
      @canonical_design ||= {
        '_id' => "_design/#{design_name}",
        'views' => {}
      }
    end
    
    def self.view name, default_options={}
      define_method name do |key, *opts|
        view name, key, opts.first, default_options
      end
    end
    
    def self.docs name, default_options={}
      default_options = {:include_docs => true, :reduce => false}.merge(default_options)
      define_method name do |*opts|
        key = opts.shift
        options = parse_opts key, opts.first, default_options
        response = call_view name, options
        ids = []
        response.inject([]) do |memo, doc|
          unless ids.include?(doc['id'])
            ids << doc['id']
            memo << Exegesis.instantiate(doc['doc'], database)
          end
          memo
        end
      end
    end
    
    def self.hash name, default_options={}
      default_options = {:group => true}.merge(default_options)
      view_name = default_options.delete(:view) || name
      define_method name do |*opts|
        key = opts.shift
        options = parse_opts key, opts.first, default_options
        options.delete(:group) if options[:key]
        
        response = call_view view_name, options
        if response.size == 1 && response.first['key'].nil?
          response.first['value']
        else
          response.inject({}) do |memo, row|
            if ! memo.has_key?(row['key'])
              memo[row['key']] = row['value']
            end
            memo
          end
        end
      end
    end
    
    
    def initialize db
      begin
        super db.get("_design/#{design_name}"), db
      rescue RestClient::ResourceNotFound
        db.put("_design/#{design_name}", self.class.canonical_design)<commit_msg>don't instantiate something that's already instantiated<commit_after>require 'pathname'
module Exegesis
  class Design
    include Exegesis::Document
    
    def self.design_directory= dir
      @design_directory = Pathname.new(dir)
    end
    
    def self.design_directory
      @design_directory ||= Pathname.new("designs/#{design_name}")
    end
    
    def self.design_name= n
      @design_name = n.to_s
    end
    
    def self.design_name
      @design_name ||= name.scan(/^(?:[A-Za-z0-9\:]+::)([A-Za-z0-9]+)Design$/).first.first.downcase
    end
    
    def self.compose_canonical
      Dir[design_directory + 'views' + '**/*.js'].each do |view_func|
        path = view_func.split('/')
        func = path.pop.sub(/\.js$/,'')
        name = path.pop
        canonical_design['views'][name] ||= {}
        canonical_design['views'][name][func] = File.read(view_func)
      end
    end
    
    def self.canonical_design
      @canonical_design ||= {
        '_id' => "_design/#{design_name}",
        'views' => {}
      }
    end
    
    def self.view name, default_options={}
      define_method name do |key, *opts|
        view name, key, opts.first, default_options
      end
    end
    
    def self.docs name, default_options={}
      default_options = {:include_docs => true, :reduce => false}.merge(default_options)
      define_method name do |*opts|
        key = opts.shift
        options = parse_opts key, opts.first, default_options
        response = call_view name, options
        ids = []
        response.inject([]) do |memo, doc|
          unless ids.include?(doc['id'])
            ids << doc['id']
            memo << Exegesis.instantiate(doc['doc'], database)
          end
          memo
        end
      end
    end
    
    def self.hash name, default_options={}
      default_options = {:group => true}.merge(default_options)
      view_name = default_options.delete(:view) || name
      define_method name do |*opts|
        key = opts.shift
        options = parse_opts key, opts.first, default_options
        options.delete(:group) if options[:key]
        
        response = call_view view_name, options
        if response.size == 1 && response.first['key'].nil?
          response.first['value']
        else
          response.inject({}) do |memo, row|
            if ! memo.has_key?(row['key'])
              memo[row['key']] = row['value']
            end
            memo
          end
        end
      end
    end
    
    
    def initialize db
      begin
        super db.raw_get("_design/#{design_name}")
        self.database = db
      rescue RestClient::ResourceNotFound
        db.put("_design/#{design_name}", self.class.canonical_design)
{'fraction_non_alphanumeric': 0.1282051282051282, 'fraction_numerical': 0.0018716077110237693, 'mean_word_length': 2.108784176847004, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, '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': '6400452', 'n_tokens_mistral': 1633, 'n_tokens_neox': 1624, 'n_words': 347}