<commit_before>module ActiveForce
  class ModelGenerator < Rails::Generators::NamedBase
    desc 'This generator loads the table fields from SFDC and generates the fields for the SObject with a more ruby names'

    source_root File.expand_path('../templates', __FILE__)

    def create_model_file
      @table_name = file_name.capitalize
      @class_name = @table_name.gsub '__c', ''
      @attributes = create_attributes 
      template "model.rb.erb", "app/models/#{@class_name.downcase}.rb"
    end

    protected

    Attribute = Struct.new :local_name, :remote_name

    def create_attributes 
      sfdc_field_names.map do |field|
        Attribute.new sf_name_to_symbol(field) ,field
      end
    end

    def sfdc_field_names 
      ActiveForce::SObject.sfdc_client.describe(@table_name).fields.map do |field|
        field.name
      end
    end

    def sf_name_to_symbol sf_name
      sf_name = sf_name.underscore
      sf_name = sf_name[0..-4] if sf_name.include? "__c"
      sf_name.to_sym
    end

    class String
      def underscore
        self.gsub(/::/, '/').
        gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
        gsub(/([a-z\d])([A-Z])/,'\1_\2').
        tr("-", "_").
        downcase
      end
    end

  end
end<commit_msg>Error message when table is not found.<commit_after>module ActiveForce
  class ModelGenerator < Rails::Generators::NamedBase
    desc 'This generator loads the table fields from SFDC and generates the fields for the SObject with a more ruby names'

    source_root File.expand_path('../templates', __FILE__)

    def create_model_file
      @table_name = file_name.capitalize
      @class_name = @table_name.gsub '__c', ''
      @attributes = create_attributes 
      template "model.rb.erb", "app/models/#{@class_name.downcase}.rb"
    end

    protected

    Attribute = Struct.new :local_name, :remote_name

    def create_attributes 
      sfdc_field_names.map do |field|
        Attribute.new sf_name_to_symbol(field) ,field
      end
    end

    def sfdc_field_names 
      begin
        ActiveForce::SObject.sfdc_client.describe(@table_name).fields.map do |field|
          field.name
        end
      rescue 
        puts "The specified table name is not found. Be sure to append __c if it's custom"
      end
    end

    def sf_name_to_symbol sf_name
      sf_name = sf_name.underscore
      sf_name = sf_name[0..-4] if sf_name.include? "__c"
      sf_name.to_sym
    end

    class String
      def underscore
        self.gsub(/::/, '/').
        gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
        gsub(/([a-z\d])([A-Z])/,'\1_\2').
        tr("-", "_").
        downcase
      end
    end

  end
end
{'fraction_non_alphanumeric': 0.11550037622272386, 'fraction_numerical': 0.004514672686230248, 'mean_word_length': 2.457737321196359, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '974520', 'n_tokens_mistral': 899, 'n_tokens_neox': 870, 'n_words': 204}