Module: Shitceptions

Defined in:
lib/shitceptions.rb,
lib/shitceptions/error.rb,
lib/shitceptions/version.rb

Overview

Author:

Defined Under Namespace

Classes: ShittyArgumentError, ShittyEmojiError

Constant Summary collapse

VERSION =
'1.0.1'

Class Method Summary collapse

Class Method Details

.config_option(name, default_value) ⇒ Object

Set a config option for the module. This will define a getter and setter method dynamically.

Parameters:

  • name (#to_s)

    the name of the config option

  • default_value

    the default value to return if it’s not set



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/shitceptions.rb', line 34

def config_option(name, default_value)
  normalized_name = name.to_s.downcase.gsub(/[[:space:]]/, '_').to_sym

  instance_eval "    def \#{normalized_name}=(value)\n      @\#{normalized_name} = value\n    end\n\n    def \#{normalized_name}?\n      @\#{normalized_name}.nil? ? \#{default_value} : @\#{normalized_name}\n    end\n  RUBY\nend\n"

.configure {|_self| ... } ⇒ Object

Configure the behavior of Shitceptions

Examples:

Shitceptions.configure do |c|
  c.enabled = false
end
Shitceptions.configure do |c|
   c.include_original_error = true
end
Shitceptions.enabled = false
Shitceptions.include_original_error = true

Yields:

  • (_self)

Yield Parameters:

  • _self (Shitceptions)

    the object that the method was called on



23
24
25
# File 'lib/shitceptions.rb', line 23

def configure(&block)
  yield(self)
end

.shittify(klass, emoji_name) ⇒ Object

Add Shitceptions to the given class, with the given emoji.

Parameters:

  • klass (Class)

    the klass (most likely an error class) to apply Shitceptions to

  • the (#to_s)

    name of the emoji to use

Raises:



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/shitceptions.rb', line 58

def shittify(klass, emoji_name)
  return unless enabled?
  raise ShittyArgumentError.new(klass) unless klass.kind_of?(Class)

  emoji = EMOJIS[emoji_name.to_s.downcase.gsub(/[[:space:]]/, '_').to_sym]
  raise ShittyEmojiError(emoji_name) unless emoji

  if include_original_error?
    klass.class_eval "      alias_method :unshitty_to_s, :to_s\n\n      def to_s\n        \"\#{emoji}  \\\#{unshitty_to_s}\"\n      end\n    RUBY\n  else\n    klass.class_eval <<-RUBY\n      alias_method :unshitty_to_s, :to_s\n\n      def to_s\n        \"\#{emoji}  \"\n      end\n    RUBY\n  end\nend\n"