OSDN Git Service

b074ac1386f0cfa7e44266e4eb874e8f9e14c306
[pettanr/pettanr.git] / lib / manifest / controller.rb
1
2 module Pettanr
3   class ControllerManager
4     class ActionList
5       attr :list
6       def initialize action_name, item_name, operators, list, conf, manifest
7         @action_name = action_name
8         @item_name = item_name
9         @operators = operators
10         @list = list
11         @conf = conf
12         @manifest = manifest
13       end
14       
15       def items 
16         @items ||= @list.items
17       end
18       
19       def filer opt =  nil
20         paginate = opt ? @list.paginate(opt) : @list.paginate
21         @filer ||= @manifest.filer_managers[@item_name].open(@item_name, @items, @operators, paginate)
22       end
23       
24     end
25     
26     class ActionShow
27       def initialize action_name, item_name, conf, manifest
28         @action_name = action_name
29         @item_name = item_name
30         @conf = conf
31         @manifest = manifest
32         
33       end
34       
35     end
36     
37     attr :controller_manifest, :item_name, :manifest, :controller_conf, 
38       :actions
39     @@types = {'list' => ActionList, 'show' => ActionShow}
40     def initialize controller_manifest
41       @controller_manifest = controller_manifest
42       @item_name = @controller_manifest.item_name
43       @manifest = @controller_manifest.manifest
44       @controller_conf = @controller_manifest.conf
45       @actions = {}
46       @controller_manifest.actions.each {|action_name, conf|
47         type = conf['type']
48         @actions[action_name] = @@types[type]
49       }
50     end
51     
52     def open action_name, params, operators
53       name = if @controller_manifest.actions[action_name]['item_name']
54         @controller_manifest.actions[action_name]['item_name']
55       else
56         @item_name
57       end
58       list_name = @controller_manifest.list_name action_name
59       @list = @manifest.list_managers[name].open(list_name, params[:page], params[:page_size], operators)
60       raise "undefined controller for #{@item_name}::#{action_name}\nconf:#{@controller_conf}\n" unless @actions[action_name]
61       @actions[action_name].new action_name, name, operators, @list, @controller_conf, @manifest
62     end
63     
64   end
65 end
66