Class: Goat::Page

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from FlashHelper

#flash

Constructor Details

#initialize(app) ⇒ Page

Returns a new instance of Page.



793
794
795
796
797
798
# File 'lib/goat.rb', line 793

def initialize(app)
  @request = app.request
  @channel = Channel.new
  @id = 'pg_' + String.random(10)
  @params = IndifferentHash.from_hash(request.params)
end

Instance Attribute Details

#canvasObject (readonly)

Returns the value of attribute canvas.



769
770
771
# File 'lib/goat.rb', line 769

def canvas
  @canvas
end

#channelObject (readonly)

Returns the value of attribute channel.



769
770
771
# File 'lib/goat.rb', line 769

def channel
  @channel
end

#idObject (readonly)

Returns the value of attribute id.



769
770
771
# File 'lib/goat.rb', line 769

def id
  @id
end

#layoutObject (readonly)

Returns the value of attribute layout.



769
770
771
# File 'lib/goat.rb', line 769

def layout
  @layout
end

#paramsObject (readonly)

Returns the value of attribute params.



769
770
771
# File 'lib/goat.rb', line 769

def params
  @params
end

#requestObject (readonly)

Returns the value of attribute request.



769
770
771
# File 'lib/goat.rb', line 769

def request
  @request
end

Class Method Details

.disable_live_updatesObject



773
# File 'lib/goat.rb', line 773

def self.disable_live_updates; @live_updates = false; end

.enable_live_updatesObject



774
# File 'lib/goat.rb', line 774

def self.enable_live_updates; @live_updates = true; end

.handle_request(app) ⇒ Object



785
786
787
788
789
790
791
# File 'lib/goat.rb', line 785

def self.handle_request(app)
  pg = self.new(app)

  raise "Page #{pg} has no id: did you forget to call super()?" unless pg.id

  pg.response
end

.live_updates_enabled?Boolean

Returns:

  • (Boolean)


775
# File 'lib/goat.rb', line 775

def self.live_updates_enabled?; @live_updates != false; end

Instance Method Details

#_domObject



815
# File 'lib/goat.rb', line 815

def _dom; self.dom; end

#empty_responseObject



800
801
802
# File 'lib/goat.rb', line 800

def empty_response
  Rack::Response.new
end

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



779
780
781
782
783
# File 'lib/goat.rb', line 779

def erb(name, opts={}, &blk)
  e = ERBRunner.new(@request, @response, @params)
  e.delegate = self
  e.erb(name, {:partial => false}.merge(opts), &blk)
end

#erb=(erb) ⇒ Object

TODO make it work properly, but not support embenned components



870
871
# File 'lib/goat.rb', line 870

def erb=(erb); @erb = erb; #TODO make it work properly, but not support embenned components
end

#haltObject

Raises:



804
805
806
# File 'lib/goat.rb', line 804

def halt
  raise Halt.new(empty_response)
end

#htmlObject



840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
# File 'lib/goat.rb', line 840

def html
  layout = self.layout

  canvas = PageCanvas.new
  canvas.title = @title

  if @erb
    canvas.html = html_from_erb
  elsif methods.include?('dom')
    inject_html_from_dom(canvas)
  else
    raise "You should over-ride Page#dom or supply an erb template"
  end

  render_to_layout(canvas, layout)
end

#html_from_erbObject



817
818
819
# File 'lib/goat.rb', line 817

def html_from_erb
  html = self.erb(@erb, :layout => false)
end

#inject_html_from_dom(canvas) ⇒ Object



821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
# File 'lib/goat.rb', line 821

def inject_html_from_dom(canvas)
  exp = nil
  helper = ExpansionHelper.new(@id)

  Dynamic.let(:expander => helper) do
    exp = DOMTools.expanded_dom(self._dom)
  end

  distiller = DOMDistiller.new(exp, helper.components)

  canvas.html = DOMTools::HTMLBuilder.new(exp).html
  canvas.style << distiller.style
  canvas.script << distiller.script

  distiller.all_component_classes.each {|c| canvas.script_files += c.script_files}

  EM.next_tick { register_page(distiller) }
end

#register_page(distiller) ⇒ Object



808
809
810
811
812
813
# File 'lib/goat.rb', line 808

def register_page(distiller)
  live = distiller.all_components.select(&:live_enabled?)
  pg_spec = live.map(&:skel)

  StateSrvClient.register_page(@id, self.user, pg_spec)
end

#render_to_layout(canvas, layout) ⇒ Object



861
862
863
864
865
866
867
868
# File 'lib/goat.rb', line 861

def render_to_layout(canvas, layout)
  layout ||= File.join(File.dirname(__FILE__), 'views/plain_layout.erb')

  erb(layout,
    :locals => {:page => self, :canvas => canvas},
    # don't want a layout in our layout so we can layout while we layout
    :layout => false) { canvas.html }
end

#responseObject



857
858
859
# File 'lib/goat.rb', line 857

def response
  Rack::Response.new(self.html, 200, {})
end

#title=(title) ⇒ Object



872
# File 'lib/goat.rb', line 872

def title=(title); @title = title; end

#userObject



874
# File 'lib/goat.rb', line 874

def user; end