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.



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

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



420
421
422
423
424
425
426
# File 'lib/goat.rb', line 420

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.



410
411
412
# File 'lib/goat.rb', line 410

def delegate
  @delegate
end

#paramsObject (readonly)

Returns the value of attribute params.



409
410
411
# File 'lib/goat.rb', line 409

def params
  @params
end

#requestObject (readonly)

Returns the value of attribute request.



409
410
411
# File 'lib/goat.rb', line 409

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



409
410
411
# File 'lib/goat.rb', line 409

def response
  @response
end

Instance Method Details

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



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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# File 'lib/goat.rb', line 428

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.config.get!(:root), 'views', "#{name}.erb")
  end

  layf = File.join(Goat.config.get!(: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



475
476
477
# File 'lib/goat.rb', line 475

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

#take_delegate_ivarsObject



412
413
414
415
416
417
418
# File 'lib/goat.rb', line 412

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