Module: Tagful::ClassMethods

Defined in:
lib/tagful.rb

Instance Method Summary collapse

Instance Method Details

#tagful(method_id, error_module_or_class = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/tagful.rb', line 15

def tagful(method_id, error_module_or_class = nil)
  visibility =
    case
    when public_method_defined?(method_id)
      :public
    when protected_method_defined?(method_id)
      :protected
    when private_method_defined?(method_id)
      :private
    else
      raise ::Tagful::NoMethod
    end

  if error_module_or_class.nil?
    if @tagful_error_module_or_class.is_a?(Class)
      error_class = @tagful_error_module_or_class
    else
      error_module = @tagful_error_module_or_class
      error_module ||= 'Error'
    end
  else
    if error_module_or_class.is_a?(Class)
      error_class = error_module_or_class
    else
      error_module = error_module_or_class
    end
  end

  if error_class
    class_eval(<<-CODE)
      module TagfulMethods
        #{visibility}
        def #{method_id}(*args)
          super
        rescue => e
          raise #{error_class}, e.message
        end
      end

      prepend(TagfulMethods)
    CODE
  else
    class_eval(<<-CODE)
      unless defined?(#{error_module})
        module #{error_module}; end
      end

      module TagfulMethods
        #{visibility}
        def #{method_id}(*args)
          super
        rescue => e
          e.extend(#{error_module}) and raise
        end
      end

      prepend(TagfulMethods)
    CODE
  end
  method_id
end

#tagful_with(error_module_or_class) ⇒ Object



11
12
13
# File 'lib/tagful.rb', line 11

def tagful_with(error_module_or_class)
  @tagful_error_module_or_class = error_module_or_class
end