2019-03-16 09:37:35 +00:00
|
|
|
# frozen_string_literal: true
|
2019-03-15 01:32:57 +00:00
|
|
|
|
2010-12-03 11:25:21 +00:00
|
|
|
# Redmine - project management software
|
2024-02-26 22:55:54 +00:00
|
|
|
# Copyright (C) 2006- Jean-Philippe Lang
|
2010-12-03 11:25:21 +00:00
|
|
|
#
|
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
|
# of the License, or (at your option) any later version.
|
2011-09-01 02:10:25 +00:00
|
|
|
#
|
2010-12-03 11:25:21 +00:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU General Public License for more details.
|
2011-09-01 02:10:25 +00:00
|
|
|
#
|
2010-12-03 11:25:21 +00:00
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
|
|
module Redmine
|
|
|
|
|
module Views
|
|
|
|
|
module Builders
|
2024-06-07 04:47:53 +00:00
|
|
|
class Structure < BasicObject
|
2012-07-27 19:56:49 +00:00
|
|
|
def initialize(request, response)
|
2010-12-03 11:25:21 +00:00
|
|
|
@struct = [{}]
|
2018-11-03 08:11:22 +00:00
|
|
|
@request = request
|
|
|
|
|
@response = response
|
2010-12-03 11:25:21 +00:00
|
|
|
end
|
2011-09-01 02:10:25 +00:00
|
|
|
|
2024-08-21 00:46:15 +00:00
|
|
|
def array(tag, options={}, &)
|
2010-12-03 11:25:21 +00:00
|
|
|
@struct << []
|
2019-10-22 12:10:14 +00:00
|
|
|
yield(self)
|
2010-12-03 11:25:21 +00:00
|
|
|
ret = @struct.pop
|
|
|
|
|
@struct.last[tag] = ret
|
2010-12-11 13:13:49 +00:00
|
|
|
@struct.last.merge!(options) if options
|
2010-12-03 11:25:21 +00:00
|
|
|
end
|
2011-09-01 02:10:25 +00:00
|
|
|
|
2015-03-15 09:49:35 +00:00
|
|
|
def encode_value(value)
|
2024-06-07 04:47:53 +00:00
|
|
|
if value.is_a?(::Time)
|
2015-03-15 09:49:35 +00:00
|
|
|
# Rails uses a global setting to format JSON times
|
|
|
|
|
# Don't rely on it for the API as it could have been changed
|
|
|
|
|
value.xmlschema(0)
|
|
|
|
|
else
|
|
|
|
|
value
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2010-12-03 11:25:21 +00:00
|
|
|
def method_missing(sym, *args, &block)
|
2018-11-28 18:03:07 +00:00
|
|
|
if args.count > 0
|
2024-06-07 04:47:53 +00:00
|
|
|
if args.first.is_a?(::Hash)
|
|
|
|
|
if @struct.last.is_a?(::Array)
|
2010-12-04 11:20:20 +00:00
|
|
|
@struct.last << args.first unless block
|
2010-12-03 13:52:07 +00:00
|
|
|
else
|
|
|
|
|
@struct.last[sym] = args.first
|
2010-12-03 11:25:21 +00:00
|
|
|
end
|
|
|
|
|
else
|
2015-03-15 09:49:35 +00:00
|
|
|
value = encode_value(args.first)
|
2024-06-07 04:47:53 +00:00
|
|
|
if @struct.last.is_a?(::Array)
|
2023-01-11 13:20:52 +00:00
|
|
|
if args.size == 1 && !block
|
2015-03-15 09:49:35 +00:00
|
|
|
@struct.last << value
|
2012-01-29 18:29:09 +00:00
|
|
|
else
|
2015-03-15 09:49:35 +00:00
|
|
|
@struct.last << (args.last || {}).merge(:value => value)
|
2012-01-29 18:29:09 +00:00
|
|
|
end
|
2010-12-03 11:25:21 +00:00
|
|
|
else
|
2015-03-15 09:49:35 +00:00
|
|
|
@struct.last[sym] = value
|
2010-12-03 11:25:21 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2023-01-11 13:20:52 +00:00
|
|
|
if block
|
2024-06-07 04:47:53 +00:00
|
|
|
@struct << (args.first.is_a?(::Hash) ? args.first : {})
|
2019-10-22 12:10:14 +00:00
|
|
|
yield(self)
|
2010-12-03 11:25:21 +00:00
|
|
|
ret = @struct.pop
|
2024-06-07 04:47:53 +00:00
|
|
|
if @struct.last.is_a?(::Array)
|
2010-12-03 11:25:21 +00:00
|
|
|
@struct.last << ret
|
|
|
|
|
else
|
2024-06-07 04:47:53 +00:00
|
|
|
if @struct.last.has_key?(sym) && @struct.last[sym].is_a?(::Hash)
|
2010-12-04 10:41:31 +00:00
|
|
|
@struct.last[sym].merge! ret
|
|
|
|
|
else
|
|
|
|
|
@struct.last[sym] = ret
|
|
|
|
|
end
|
2010-12-03 11:25:21 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2011-09-01 02:10:25 +00:00
|
|
|
|
2010-12-03 11:25:21 +00:00
|
|
|
def output
|
|
|
|
|
raise "Need to implement #{self.class.name}#output"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|