<commit_before>#--
###############################################################################
#                                                                             #
# A component of brice, the extra cool IRb goodness donator                   #
#                                                                             #
# Copyright (C) 2008-2011 <NAME>                                          #
#                                                                             #
# Authors:                                                                    #
#     <NAME> <<EMAIL>>                                    #
#                                                                             #
# brice is free software: you can redistribute it and/or modify it under the  #
# terms of the GNU Affero General Public License as published by the Free     #
# Software Foundation, either version 3 of the License, or (at your option)   #
# any later version.                                                          #
#                                                                             #
# brice is distributed in the hope that it will be useful, but WITHOUT ANY    #
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS   #
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for     #
# more details.                                                               #
#                                                                             #
# You should have received a copy of the GNU Affero General Public License    #
# along with brice. If not, see <http://www.gnu.org/licenses/>.               #
#                                                                             #
###############################################################################
#++

require 'nuggets/object/silence_mixin'
require 'nuggets/env/set'

require 'brice'

module Brice

  # Certain global helper methods for use inside IRb extensions. Also
  # available inside the IRb session.

  module DSL

    include Nuggets::Object::SilenceMixin

    # call-seq:
    #   irb_rc { ... }
    #
    # Add IRB_RC proc (to be executed whenever a (sub-)session is started).
    def irb_rc(&block)
      Brice.irb_rc << block
    end

    # call-seq:
    #   irb_def(symbol) { ... }
    #   irb_def(symbol, method)
    #
    # Define a method for use inside the IRb session.
    def irb_def(symbol, method = nil, &block)
      irb_rc {
        Object.instance_eval {
          define_method(symbol, method || block)
        }
      }
    end

    alias_method :define_irb_method, :irb_def

    # call-seq:
    #   brice_rescue(what, args = [], error = Exception)
    #
    # Call +what+ with +args+ and rescue potential +error+, optionally
    # executing block in case of success. Gives a nicer error location
    # instead of the full backtrace.
    #
    # Returns either the result of the executed method or of the block.
    def brice_rescue(what, args = [], error = Exception)
      res = send(what, *args)

      block_given? ? yield : res
    rescue Exception => err
      raise unless err.is_a?(error)

      unless Brice.quiet
        # FIXME: ideally, we'd want the __FILE__ and __LINE__ of the
        # rc file where the error occurred.
        location = caller.find { |c| c !~ %r{(?:\A|/)lib/brice[/.]} }
        warn "#{err.class}: #{err} [#{location}]"
      end<commit_msg>lib/brice/dsl.rb (brice_require): Print backtrace if verbose.<commit_after>#--
###############################################################################
#                                                                             #
# A component of brice, the extra cool IRb goodness donator                   #
#                                                                             #
# Copyright (C) 2008-2011 <NAME>                                          #
#                                                                             #
# Authors:                                                                    #
#     <NAME> <<EMAIL>>                                    #
#                                                                             #
# brice is free software: you can redistribute it and/or modify it under the  #
# terms of the GNU Affero General Public License as published by the Free     #
# Software Foundation, either version 3 of the License, or (at your option)   #
# any later version.                                                          #
#                                                                             #
# brice is distributed in the hope that it will be useful, but WITHOUT ANY    #
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS   #
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for     #
# more details.                                                               #
#                                                                             #
# You should have received a copy of the GNU Affero General Public License    #
# along with brice. If not, see <http://www.gnu.org/licenses/>.               #
#                                                                             #
###############################################################################
#++

require 'nuggets/object/silence_mixin'
require 'nuggets/env/set'

require 'brice'

module Brice

  # Certain global helper methods for use inside IRb extensions. Also
  # available inside the IRb session.

  module DSL

    include Nuggets::Object::SilenceMixin

    # call-seq:
    #   irb_rc { ... }
    #
    # Add IRB_RC proc (to be executed whenever a (sub-)session is started).
    def irb_rc(&block)
      Brice.irb_rc << block
    end

    # call-seq:
    #   irb_def(symbol) { ... }
    #   irb_def(symbol, method)
    #
    # Define a method for use inside the IRb session.
    def irb_def(symbol, method = nil, &block)
      irb_rc {
        Object.instance_eval {
          define_method(symbol, method || block)
        }
      }
    end

    alias_method :define_irb_method, :irb_def

    # call-seq:
    #   brice_rescue(what, args = [], error = Exception)
    #
    # Call +what+ with +args+ and rescue potential +error+, optionally
    # executing block in case of success. Gives a nicer error location
    # instead of the full backtrace.
    #
    # Returns either the result of the executed method or of the block.
    def brice_rescue(what, args = [], error = Exception)
      res = send(what, *args)

      block_given? ? yield : res
    rescue Exception => err
      raise unless err.is_a?(error)

      unless Brice.quiet
        # FIXME: ideally, we'd want the __FILE__ and __LINE__ of the
        # rc file where the error occurred.
        location = caller.find { |c| c !~ %r{(?:\A|/)lib/brice[/.]} }
        warn "#{err.class}: #{err} [#{location}]"

        warn err.backtrace.map { |line|
          "        from #{line}"
        }.join("\n") if Brice.verbose
      end
{'fraction_non_alphanumeric': 0.12230113636363636, 'fraction_numerical': 0.002556818181818182, 'mean_word_length': 1.3062561415001637, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 58, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '458959', 'n_tokens_mistral': 1658, 'n_tokens_neox': 1594, 'n_words': 564}