<commit_before>require 'pathname'
require 'hanami/utils/hash'

module Hanami
  # Read the .hanamirc file in the root of the application
  #
  # @since 0.3.0
  # @api private
  class Hanamirc
    # Hanamirc name file
    #
    # @since 0.3.0
    # @api private
    #
    # @see Hanami::Hanamirc#path_file
    FILE_NAME = '.hanamirc'.freeze

    # Architecture default value
    #
    # @since 0.3.0
    # @api private
    #
    # @see Hanami::Hanamirc#options
    DEFAULT_ARCHITECTURE = 'container'.freeze

    # Application architecture value
    #
    # @since 0.6.0
    # @api private
    APP_ARCHITECTURE = 'app'.freeze

    # Architecture key for writing the hanamirc file
    #
    # @since 0.3.0
    # @api private
    #
    # @see Hanami::Hanamirc#default_options
    ARCHITECTURE_KEY = 'architecture'.freeze

    # Project name for writing the hanamirc file
    #
    # @since 0.8.0
    # @api private
    #
    # @see Hanami::Hanamirc#default_options
    PROJECT_NAME = 'project'.freeze

    # Test suite default value
    #
    # @since 0.3.0
    # @api private
    #
    # @see Hanami::Hanamirc#default_options
    DEFAULT_TEST_SUITE = 'minitest'.freeze

    # Test suite key for writing the hanamirc file
    #
    # @since 0.3.0
    # @api private
    #
    # @see Hanami::Hanamirc#default_options
    TEST_KEY = 'test'.freeze

    # Template default value
    #
    # @since 0.3.0
    # @api private
    #
    # @see Hanami::Hanamirc#default_options
    DEFAULT_TEMPLATE = 'erb'.freeze

    # Template key for writing the hanamirc file
    #
    # @since 0.3.0
    # @api private
    #
    # @see Hanami::Hanamirc#default_options
    TEMPLATE_KEY = 'template'.freeze

    # Key/value separator in hanamirc file
    #
    # @since 0.8.0
    # @api private
    SEPARATOR = '='.freeze

    # Initialize Hanamirc class with application's root and environment options.
    #
    # @param root [Pathname] Application's root
    #
    # @see Hanami::Environment#initialize
    def initialize(root)
      @root = root
    end

    # Read hanamirc file (if exists) and parse it's values or return default.
    #
    # @return [Hanami::Utils::Hash] parsed values
    #
    # @example Default values if file doesn't exist
    #   Hanami::Hanamirc.new(Pathname.new(Dir.pwd)).options
    #    # => { architecture: 'container', test: 'minitest', template: 'erb' }
    #
    # @example Custom values if file doesn't exist
    #   options = { architect: 'application', test: 'rspec', template: 'slim' }
    #   Hanami::Hanamirc.new(Pathname.new(Dir.pwd), options).options
    #    # => { architecture: 'application', test: 'rspec', template: 'slim' }
    def options
      @options ||= symbolize(default_options.merge(file_options))
    end

    # Default values for writing the hanamirc file
    #
    # @since 0.5.1
    # @api private
    #
    # @see Hanami::Hanamirc#options
    def default_options
      @default_options ||= Utils::Hash.new({
                                           ARCHITECTURE_KEY => DEFAULT_ARCHITECTURE,
                                           PROJECT_NAME     => project_name,
                                           TEST_KEY         => DEFAULT_TEST_SUITE,
                                           TEMPLATE_KEY     => DEFAULT_TEMPLATE
                                         }).symbolize!.freeze
    end

    # Check if hanamirc file exists
    #
    # @since 0.3.0
    # @api private
    #
    # @return [Boolean] hanamirc file's path existing
    def exists?
      path_file.exist?
    end

    private

    # @api private
    def symbolize(hash)
      Utils::Hash.new(hash).symbolize!
    end

    # @api private
    def file_options
      symbolize(exists? ? parse_file(path_file) : {})<commit_msg>Added hanamirc file_options method documentation<commit_after>require 'pathname'
require 'hanami/utils/hash'

module Hanami
  # Read the .hanamirc file in the root of the application
  #
  # @since 0.3.0
  # @api private
  class Hanamirc
    # Hanamirc name file
    #
    # @since 0.3.0
    # @api private
    #
    # @see Hanami::Hanamirc#path_file
    FILE_NAME = '.hanamirc'.freeze

    # Architecture default value
    #
    # @since 0.3.0
    # @api private
    #
    # @see Hanami::Hanamirc#options
    DEFAULT_ARCHITECTURE = 'container'.freeze

    # Application architecture value
    #
    # @since 0.6.0
    # @api private
    APP_ARCHITECTURE = 'app'.freeze

    # Architecture key for writing the hanamirc file
    #
    # @since 0.3.0
    # @api private
    #
    # @see Hanami::Hanamirc#default_options
    ARCHITECTURE_KEY = 'architecture'.freeze

    # Project name for writing the hanamirc file
    #
    # @since 0.8.0
    # @api private
    #
    # @see Hanami::Hanamirc#default_options
    PROJECT_NAME = 'project'.freeze

    # Test suite default value
    #
    # @since 0.3.0
    # @api private
    #
    # @see Hanami::Hanamirc#default_options
    DEFAULT_TEST_SUITE = 'minitest'.freeze

    # Test suite key for writing the hanamirc file
    #
    # @since 0.3.0
    # @api private
    #
    # @see Hanami::Hanamirc#default_options
    TEST_KEY = 'test'.freeze

    # Template default value
    #
    # @since 0.3.0
    # @api private
    #
    # @see Hanami::Hanamirc#default_options
    DEFAULT_TEMPLATE = 'erb'.freeze

    # Template key for writing the hanamirc file
    #
    # @since 0.3.0
    # @api private
    #
    # @see Hanami::Hanamirc#default_options
    TEMPLATE_KEY = 'template'.freeze

    # Key/value separator in hanamirc file
    #
    # @since 0.8.0
    # @api private
    SEPARATOR = '='.freeze

    # Initialize Hanamirc class with application's root and environment options.
    #
    # @param root [Pathname] Application's root
    #
    # @see Hanami::Environment#initialize
    def initialize(root)
      @root = root
    end

    # Read hanamirc file (if exists) and parse it's values or return default.
    #
    # @return [Hanami::Utils::Hash] parsed values
    #
    # @example Default values if file doesn't exist
    #   Hanami::Hanamirc.new(Pathname.new(Dir.pwd)).options
    #    # => { architecture: 'container', test: 'minitest', template: 'erb' }
    #
    # @example Custom values if file doesn't exist
    #   options = { architect: 'application', test: 'rspec', template: 'slim' }
    #   Hanami::Hanamirc.new(Pathname.new(Dir.pwd), options).options
    #    # => { architecture: 'application', test: 'rspec', template: 'slim' }
    def options
      @options ||= symbolize(default_options.merge(file_options))
    end

    # Default values for writing the hanamirc file
    #
    # @since 0.5.1
    # @api private
    #
    # @see Hanami::Hanamirc#options
    def default_options
      @default_options ||= Utils::Hash.new({
                                           ARCHITECTURE_KEY => DEFAULT_ARCHITECTURE,
                                           PROJECT_NAME     => project_name,
                                           TEST_KEY         => DEFAULT_TEST_SUITE,
                                           TEMPLATE_KEY     => DEFAULT_TEMPLATE
                                         }).symbolize!.freeze
    end

    # Check if hanamirc file exists
    #
    # @since 0.3.0
    # @api private
    #
    # @return [Boolean] hanamirc file's path existing
    def exists?
      path_file.exist?
    end

    private

    # @api private
    def symbolize(hash)
      Utils::Hash.new(hash).symbolize!
    end

    # Returns options from hanamirc file
    #
    # @since 0.6.0
    # @api private
    #
    # @return [Hash] hanamirc parsed values
    def file_options
      symbolize(exists? ? parse_file(path_file) : {})
{'fraction_non_alphanumeric': 0.10342115611482501, 'fraction_numerical': 0.01061738104600865, 'mean_word_length': 2.1117455138662318, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1512974', 'n_tokens_mistral': 2552, 'n_tokens_neox': 2385, 'n_words': 586}