Class: Tagz::Namespace::Element

Inherits:
String
  • Object
show all
Defined in:
lib/tagz.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, *argv, &block) ⇒ Element

Returns a new instance of Element.



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/tagz.rb', line 328

def initialize(name, *argv, &block)
  options = {}
  content = []

  argv.each do |arg|
    case arg
      when Hash
        options.update arg
      else
        content.push arg
    end
  end

  content.push block.call if block
  content.compact!

  @name = name.to_s

  if content.empty?
    replace "<#{ @name }#{ Element.attributes options }>"
  else
    replace "<#{ @name }#{ Element.attributes options }>#{ content.join }</#{ name }>"
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



326
327
328
# File 'lib/tagz.rb', line 326

def name
  @name
end

Class Method Details

.attributes(options) ⇒ Object



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/tagz.rb', line 306

def Element.attributes(options)
  unless options.empty?
    ' ' << 
      options.map do |key, value|
        key = Tagz.escape_key(key)
        value = Tagz.escape_value(value)
        if value =~ %r/"/
          raise ArgumentError, value if value =~ %r/'/
          value = "'#{ value }'"
        else
          raise ArgumentError, value if value =~ %r/"/
          value = "\"#{ value }\""
        end
        [key, value].join('=')
      end.join(' ')
  else
    ''
  end
end