class Fragment

Constants

VERSION

Attributes

to_s[RW]

Public Class Methods

create(&block) click to toggle source
# File lib/fragment.rb, line 6
def self.create(&block); self.new(&block).to_s; end
create_here(&block) click to toggle source

create_here is kept for backward compatibility

# File lib/fragment.rb, line 8
def self.create_here(&block); self.new(&block).to_s; end
new(outer_scope=false, &block) click to toggle source
# File lib/fragment.rb, line 10
def initialize outer_scope=false, &block
  # outer_scope argument is kept for backward compatibility
  # but it is not used
  # checking is simpler with arity instead
  @to_s = ""
  return self unless block_given?
  if block.arity==0
    instance_eval(&block)
  else
    block.call(self)
  end
  self
end
version() click to toggle source
# File lib/fragment/version.rb, line 3
def self.version
  VERSION.join('.')
end

Public Instance Methods

_fragment_escape_html(s) click to toggle source
# File lib/fragment.rb, line 53
def _fragment_escape_html(s)
  s.to_s.gsub(/&/, '&amp;').gsub(/"/, '&quot;').gsub(/'/, '&apos;').gsub(/</, '&lt;').gsub(/>/, '&gt;')
end
comment(s='') click to toggle source
# File lib/fragment.rb, line 52
def comment(s=''); write "\n<!-- #{s} -->\n"; end
doctype() click to toggle source
# File lib/fragment.rb, line 51
def doctype; write "<!DOCTYPE html>\n"; end
method_missing(meth, args={}, &block) click to toggle source
# File lib/fragment.rb, line 24
def method_missing(meth, args={}, &block); tag(meth, args, &block); end
p(args={}, &block) click to toggle source

Override Kernel methods

# File lib/fragment.rb, line 45
def p(args={}, &block); tag(:p, args, &block); end
select(args={}, &block) click to toggle source
# File lib/fragment.rb, line 46
def select(args={}, &block); tag(:select, args, &block); end
tag(name, attributes={}) { || ... } click to toggle source
# File lib/fragment.rb, line 26
def tag name, attributes={}
  @to_s << "<#{name}"
  if attributes.kind_of?(String)
    @to_s << ' ' << attributes
  else
    @to_s << attributes.delete_if{|k,v| v.nil? or v==false }.map{|(k,v)| " #{k}='#{_fragment_escape_html(v)}'" }.join
  end
  if block_given?
    @to_s << ">"
    text = yield
    @to_s << text.to_str if text != @to_s and text.respond_to?(:to_str)
    @to_s << "</#{name}>"
  else
    @to_s << ' />'
  end
end
write(s='') click to toggle source

Helpers

# File lib/fragment.rb, line 50
def write(s=''); @to_s << s; end