`

nokogiri

    博客分类:
  • ruby
 
阅读更多
       ###
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
                                          
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics