Class: XX::Template::Basic

Inherits:
Object
  • Object
show all
Defined in:
lib/xx.rb,
lib/xx-2.1.0.rb

Overview

–{{{

Direct Known Subclasses

File, Inline

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}, &b) ⇒ Basic

Returns a new instance of Basic.

Raises:

  • (ArgumentError)

655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
# File 'lib/xx.rb', line 655

def initialize opts = {}, &b
#--{{{
  @path = opts['path'] || opts[:path]
  @inline = opts['inline'] || opts[:inline]
  @type = opts['type'] || opts[:type]
  @object = opts['object'] || opts[:object]
  @pretty = opts['pretty'] || opts[:pretty]
  @port = opts['port'] || opts[:port] || STDOUT

  bool = lambda{|value| value ? true : false}
  raise ArgumentError unless(bool[@path] ^ bool[@inline])

  path_init(&b) if @path
  inline_init(&b) if @inline

  @type =
    case @type.to_s.downcase.strip
      when /^xhtml$/
        XHTML
      when /^xml$/
        XML
      when /^html4$/
        HTML4
      else
        XHTML
    end
#--}}}
end

Instance Method Details

#expand(binding, opts = {}) ⇒ Object Also known as: result

–}}}


695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
# File 'lib/xx.rb', line 695

def expand binding, opts = {}
#--{{{
  port = opts['port'] || opts[:port] || STDOUT
  pretty = opts['pretty'] || opts[:pretty]

  object = eval 'self', binding

  unless type === object 
    __m = type
    klass = object.class
    klass.module_eval{ include __m } 
  end

  doc = eval @template, binding 

  display = pretty ? 'pretty' : 'to_str'

  doc.send display, port
#--}}}
end

#inline_init(&b) ⇒ Object

–}}}


689
690
691
692
693
694
# File 'lib/xx.rb', line 689

def inline_init &b
#--{{{
  @template = (@inline || b.call).to_s 
  @type ||= XHTML
#--}}}
end

#path_init(&b) ⇒ Object

–}}}


683
684
685
686
687
688
# File 'lib/xx.rb', line 683

def path_init &b
#--{{{
  @template = IO.read @path
  @type = @path[%r/\.[^\.]+$/o] || XHTML unless @type
#--}}}
end