Hooks   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %
Metric Value
dl 0
loc 35
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 3 1
A initialize() 0 5 1
A format_field() 0 9 2
A after_get_all_entries() 0 6 1
1
# ADDED in midb-2.0.0a  by unrar #
2
#
3
# The "hooks" part of the MIDB::API module allows programmers to customize their API.
4
# Hooks are methods that are run in the API; overriding them allows you to run a custom API.
5
#
6
# This hooking technology has been developed in its-hookable.
7
#
8
module MIDB
9
  module API
10
    class Hooks
11
      attr_accessor :hooks
12
      def initialize()
13
        @hooks = Hash.new
14
        @hooks["after_get_all_entries"] = []
15
        @hooks["format_field"] = []
16
      end
17
18
      # This method adds a method _reference_ (:whatever) to the hash defined above.
19
      def register(hook, method)
20
        @hooks[hook].push method
21
      end
22
23
      # These are the methods that are ran when the hook is called from the main class.
24
      # The code can be slightly modified depending on the arguments that each hook takes,
25
      # but that's up to the original developer - not the one who does the customization.
26
27
28
      def after_get_all_entries(n_entries)
29
        @hooks["after_get_all_entries"].each do |f|
30
          # Just run :f whenever this method is called, no arguments.
31
          Object.send(f, n_entries)
32
        end
33
      end
34
35
      def format_field(field, what)
36
        if @hooks["format_field"] == []
37
          return what
38
        else
39
          @hooks["format_field"].each do |f|
40
            return Object.send(f, field, what)
41
          end
42
        end
43
      end
44
    end
45
  end
46
end
47