Class: Goat::ERBRunner

Inherits:
Object show all
Includes:
FlashHelper, HTMLHelpers
Defined in:
lib/goat.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FlashHelper

#flash

Methods included from HTMLHelpers

#jsesc

Constructor Details

#initialize(req, resp, params) ⇒ ERBRunner

Returns a new instance of ERBRunner.



385
386
387
388
389
# File 'lib/goat.rb', line 385

def initialize(req, resp, params)
  @request = req
  @response = resp
  @params = params
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object



402
403
404
405
406
407
408
# File 'lib/goat.rb', line 402

def method_missing(meth, *args)
  if @delegate
    @delegate.send(meth, *args)
  else
    super
  end
end

Instance Attribute Details

#delegateObject

Returns the value of attribute delegate.



392
393
394
# File 'lib/goat.rb', line 392

def delegate
  @delegate
end

#paramsObject (readonly)

Returns the value of attribute params.



391
392
393
# File 'lib/goat.rb', line 391

def params
  @params
end

#requestObject (readonly)

Returns the value of attribute request.



391
392
393
# File 'lib/goat.rb', line 391

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



391
392
393
# File 'lib/goat.rb', line 391

def response
  @response
end

Instance Method Details

#erb(name, opts = {}, &blk) ⇒ Object



410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# File 'lib/goat.rb', line 410

def erb(name, opts={}, &blk)
  take_delegate_ivars if @delegate

  opts = {
    :partial => false,
    :layout => true,
    :locals => {}
  }.merge(opts)

  if self.kind_of?(Page)
    opts[:locals][:page] ||= self
  end

  partial = opts[:partial]
  use_layout = opts[:layout]
  locals = opts[:locals]

  if partial
    name = name.to_s
    d = File.dirname(name)
    d = d == '.' ? '' : "#{d}/"
    f = File.basename(name)

    # slashes are actually allowed in syms
    name = "#{d}_#{f}".to_sym
  end

  if name =~ /\.erb$/ # allow an absolute path to be passed
    erbf = name
  else
    erbf = File.join(Goat.setting!(:root), 'views', "#{name}.erb")
  end

  layf = File.join(Goat.setting!(:root), 'views', 'layout.erb')
  template = Tilt[:erb].new(erbf) { File.read(erbf) }

  layout = File.read(layf) if File.exists?(layf) && !partial && use_layout
  out = template.render(self, locals, &blk)

  if layout
    laytpl = Tilt[:erb].new(layf) { layout }
    laytpl.render(self, locals) { out }
  else
    out
  end
end

#partial_erb(name, opts = {}) ⇒ Object



457
458
459
# File 'lib/goat.rb', line 457

def partial_erb(name, opts={})
  erb(name, {:partial => true}.merge(opts))
end

#take_delegate_ivarsObject



394
395
396
397
398
399
400
# File 'lib/goat.rb', line 394

def take_delegate_ivars
  @delegate.instance_variables.each do |ivar|
    unless self.instance_variables.include?(ivar)
      self.instance_variable_set(ivar, @delegate.instance_variable_get(ivar))
    end
  end
end