| Total Complexity | 5 |
| Total Lines | 35 |
| Duplicated Lines | 0 % |
| 1 | # ADDED in midb-2.0.0a by unrar # |
||
| 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 |
||
| 47 |