markbates / mack-more

All the extra stuff you could want for the Mack Framework.

This URL has Read+Write access

mack-more / mack-facets / lib / mack-facets / utils / registry_map.rb
100644 91 lines (73 sloc) 2.506 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
require 'singleton'
module Mack
  module Utils
 
    #
    # Provides a convenient way to register items in a map.
    #
    # ds - july 2008
    #
    class RegistryMap
      include Singleton
      include Extlib::Hook
      
      # The list of registered items
      attr_reader :registered_items
      
      def initialize # :nodoc:
        reset!
      end
      
      # Override this method to set the initial state of the registered_items Array.
      # By default this list is empty.
      def initial_state
        {}
      end
      
      # Resets the registered_items list to the list specified by the initial_state method.
      def reset!
        @registered_items = self.initial_state.dup
      end
      
      # Adds an object to the list at a specified position. By default the position is last.
      def add(tag, klass, position = -1)
        @registered_items[tag] ||= []
        arr = self.registered_items[tag]
        position = arr.size if position == -1
        
        arr.insert(position, klass)
        arr.uniq!
        arr.compact!
      end
      
      # Removes an object from the list.
      def remove(tag, klass)
        return false if @registered_items[tag] == nil
        self.registered_items[tag].delete(klass)
      end
      
      class << self
        
        # Returns the list of registered items.
        def registered_items
          self.instance.registered_items
        end
        
        # Emptys out the list of registered_items.
        def clear!
          registered_items.clear
        end
        
        # Resets the registered_items list to the list specified by the initial_state method.
        def reset!
          self.instance.reset!
        end
        
        # Adds an object to the list at a specified position. By default the position is last.
        def add(tag, klass, position = -1)
          self.instance.add(tag, klass, position)
        end
        
        # Removes an object from the list.
        def remove(tag, klass)
          self.instance.remove(tag, klass)
        end
        
        # Moves an object to the top of the registered_items list.
        def move_to_top(tag, klass)
          self.instance.add(tag, klass, 0)
        end
        
        # Moves an object to the bottom of the registered_items list.
        def move_to_bottom(tag, klass)
          remove(tag, klass)
          self.instance.add(tag, klass)
        end
        
      end
      
    end # RegistryMap
  end # Utils
end # Mack