nokogiri

###
240 # Create a builder with an existing root object. This is for use when
241 # you have an existing document that you would like to augment with
242 # builder methods. The builder context created will start with the
243 # given +root+ node.
244 #
245 # For example:
246 #
247 # doc = Nokogiri::XML(open('somedoc.xml'))
248 # Nokogiri::XML::Builder.with(doc.at('some_tag')) do |xml|
249 # # ... Use normal builder methods here ...
250 # xml.awesome # add the "awesome" tag below "some_tag"
251 # end
252 #
253 def self.with root, &block
254 new({}, root, &block)
255 end
256
257 ###
258 # Create a new Builder object. +options+ are sent to the top level
259 # Document that is being built.
260 #
261 # Building a document with a particular encoding for example:
262 #
263 # Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
264 # ...
265 # end
266 def initialize options = {}, root = nil, &block
267
268 if root
269 @doc = root.document
270 @parent = root
271 else
272 namespace = self.class.name.split('::')
273 namespace[-1] = 'Document'
274 @doc = eval(namespace.join('::')).new
275 @parent = @doc
276 end
277
278 @context = nil
279 @arity = nil
280 @ns = nil
281
282 options.each do |k,v|
283 @doc.send(:"#{k}=", v)
284 end
285
286 return unless block_given?
287
288 @arity = block.arity
289 if @arity <= 0
290 @context = eval('self', block.binding)
291 instance_eval(&block)
292 else
293 yield self
294 end
295
296 @parent = @doc
297 end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值