<commit_before>module Lucie
  class App

    class << self
      attr_accessor :raise_exception
      attr_accessor :log_level
      attr_accessor :root
      @raise_exception = false
      @log_level = :info
    end

    attr_reader :command
    attr_reader :root
    attr_reader :pwd

    def self.run(command = ARGV, root = nil)
      root ||= File.expand_path("..", File.dirname(Kernel.caller[0]))
      instance = self.init(command, root)
      self.start(instance)
    end

    def self.init(command = ARGV, root = nil)
      root ||= File.expand_path("..", File.dirname(Kernel.caller[0]))
      self.new(command, root)
    end

    def self.start(instance)
      instance.start
      instance.exit_value
    end

    def initialize(command, root)
      @root = root
      @command = CommandLineParser.new(command)
      @exit_value ||= 0
      @task = nil
      @pwd = <PASSWORD>["<PASSWORD>"]
    end

    def start
      help? ? call_help : call_method_invoking_process
    end

    def exit_value
      @exit_value
    end

private

    def call_method_invoking_process
      apply_validators
      pair_parameters
      call_action_on_controller
    rescue => e
      self.exception = e
      give_user_friendly_error_message
      write_backtrace
      raise if self.class.raise_exception
    end

    def help?
      @command.has_option?("-h") || @command.has_option?("--help") || @command.has_arg?("help") || task == "help"
    end

    def task
      @task ||= @command.shift
    end

    def action
      @action ||= @command.args.first
      @action ? @action.to_sym : :index
    end

    def controller
      @controller ||= controller_class.send(:new, command, self)
    end

    def controller_class
      if task
        @controller_class ||= [task.split("_").map{|i| i.capitalize}.join, "Controller"].join
        Object.const_get(@controller_class.to_sym)
      else
        include_controller_for "application"
        @controller_class = "ApplicationController"
        @action = "help"
        Object.const_get(@controller_class.to_sym)
      end
    rescue NameError
      include_controller_for(task)
      Object.const_get(@controller_class.to_sym)
    end

    def include_controller_for(task)
      require File.join([root, "app/controllers", "#{task}_controller"])
    rescue LoadError
      self.exit_value = 255
      raise ControllerNotFound, task
    end

    def call_action_on_controller<commit_msg>Cache the value of action in App<commit_after>module Lucie
  class App

    class << self
      attr_accessor :raise_exception
      attr_accessor :log_level
      attr_accessor :root
      @raise_exception = false
      @log_level = :info
    end

    attr_reader :command
    attr_reader :root
    attr_reader :pwd

    def self.run(command = ARGV, root = nil)
      root ||= File.expand_path("..", File.dirname(Kernel.caller[0]))
      instance = self.init(command, root)
      self.start(instance)
    end

    def self.init(command = ARGV, root = nil)
      root ||= File.expand_path("..", File.dirname(Kernel.caller[0]))
      self.new(command, root)
    end

    def self.start(instance)
      instance.start
      instance.exit_value
    end

    def initialize(command, root)
      @root = root
      @command = CommandLineParser.new(command)
      @exit_value ||= 0
      @task = nil
      @pwd = ENV["<PASSWORD>"]
    end

    def start
      help? ? call_help : call_method_invoking_process
    end

    def exit_value
      @exit_value
    end

private

    def call_method_invoking_process
      apply_validators
      pair_parameters
      call_action_on_controller
    rescue => e
      self.exception = e
      give_user_friendly_error_message
      write_backtrace
      raise if self.class.raise_exception
    end

    def help?
      @command.has_option?("-h") || @command.has_option?("--help") || @command.has_arg?("help") || task == "help"
    end

    def task
      @task ||= @command.shift
    end

    def action
      @action ||= begin
        @command.args.first ? @command.args.first.to_sym : :index
      end
    end

    def controller
      @controller ||= controller_class.send(:new, command, self)
    end

    def controller_class
      if task
        @controller_class ||= [task.split("_").map{|i| i.capitalize}.join, "Controller"].join
        Object.const_get(@controller_class.to_sym)
      else
        include_controller_for "application"
        @controller_class = "ApplicationController"
        @action = "help"
        Object.const_get(@controller_class.to_sym)
      end
    rescue NameError
      include_controller_for(task)
      Object.const_get(@controller_class.to_sym)
    end

    def include_controller_for(task)
      require File.join([root, "app/controllers", "#{task}_controller"])
    rescue LoadError
      self.exit_value = 255
      raise ControllerNotFound, task
    end

    def call_action_on_controller
{'fraction_non_alphanumeric': 0.1008557457212714, 'fraction_numerical': 0.0024449877750611247, 'mean_word_length': 2.5727802037845704, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 8, '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': '4090766', 'n_tokens_mistral': 1537, 'n_tokens_neox': 1492, 'n_words': 324}