1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 require 'blankslate'
19
20 module Redmine
21 module Views
22 module Builders
23 class Structure < BlankSlate
24 def initialize
25 @struct = [{}]
26 end
27
28 def array(tag, options={}, &block)
29 @struct << []
30 block.call(self)
31 ret = @struct.pop
32 @struct.last[tag] = ret
33 @struct.last.merge!(options) if options
34 end
35
36 def method_missing(sym, *args, &block)
37 if args.any?
38 if args.first.is_a?(Hash)
39 if @struct.last.is_a?(Array)
40 @struct.last << args.first unless block
41 else
42 @struct.last[sym] = args.first
43 end
44 else
45 if @struct.last.is_a?(Array)
46 if args.size == 1 && !block_given?
47 @struct.last << args.first
48 else
49 @struct.last << (args.last || {}).merge(:value => args.first)
50 end
51 else
52 @struct.last[sym] = args.first
53 end
54 end
55 end
56
57 if block
58 @struct << (args.first.is_a?(Hash) ? args.first : {})
59 block.call(self)
60 ret = @struct.pop
61 if @struct.last.is_a?(Array)
62 @struct.last << ret
63 else
64 if @struct.last.has_key?(sym) && @struct.last[sym].is_a?(Hash)
65 @struct.last[sym].merge! ret
66 else
67 @struct.last[sym] = ret
68 end
69 end
70 end
71 end
72
73 def output
74 raise "Need to implement #{self.class.name}#output"
75 end
76 end
77 end
78 end
79 end