Class: VersatileRJS::Page

Inherits:
Object
  • Object
show all
Includes:
Container
Defined in:
lib/versatile_rjs/page.rb

Defined Under Namespace

Classes: ContainerStack

Constant Summary

Constants included from Container

Container::DELIMITER_WITHOUT_NEW_LINE, Container::DELIMITER_WITH_NEW_LINE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Container

#add_statement, #statements, #to_script

Constructor Details

#initialize(view) ⇒ Page

Returns a new instance of Page.



25
26
27
28
29
# File 'lib/versatile_rjs/page.rb', line 25

def initialize(view)
  @view = view
  @stack = ContainerStack.new
  @stack.push self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/versatile_rjs/page.rb', line 8

def method_missing(name, *args)
  if name.to_s =~ /=$/
    assign(name, args.first)
  else
    view.__send__ name, *args
  end
end

Instance Attribute Details

#viewObject (readonly)

Returns the value of attribute view.



22
23
24
# File 'lib/versatile_rjs/page.rb', line 22

def view
  @view
end

Instance Method Details

#<<(statement) ⇒ Object



44
45
46
# File 'lib/versatile_rjs/page.rb', line 44

def <<(statement)
  append_statement(ActiveSupport::JSON::Variable.new(statement))
end

#[](id) ⇒ Object



48
49
50
# File 'lib/versatile_rjs/page.rb', line 48

def [](id)
  VersatileRJS::Proxy::ElementByIdProxy.new_instance(self, id)
end

#append_statement(statement) ⇒ Object



39
40
41
42
# File 'lib/versatile_rjs/page.rb', line 39

def append_statement(statement)
  stack.peek.add_statement statement
  statement
end

#assign(variable, value) ⇒ Object



56
57
58
# File 'lib/versatile_rjs/page.rb', line 56

def assign(variable, value)
  self << "var #{variable} = #{value.to_json}"
end

#evaluate(&block) ⇒ Object



76
77
78
79
# File 'lib/versatile_rjs/page.rb', line 76

def evaluate(&block)
  view.instance_exec(self, &block)
  self
end

#execute_rendering(*args_for_rendering) ⇒ Object



81
82
83
84
85
86
# File 'lib/versatile_rjs/page.rb', line 81

def execute_rendering(*args_for_rendering)
  return '' if args_for_rendering.size == 0
  return args_for_rendering.first if args_for_rendering.size == 1 &&
    args_for_rendering.first.kind_of?(String)
  return render_on_view(*args_for_rendering)
end

#insert_html(id, *args) ⇒ Object



68
69
70
# File 'lib/versatile_rjs/page.rb', line 68

def insert_html(id, *args)
  self[id].insert_html(*args)
end

#pop_containerObject



35
36
37
# File 'lib/versatile_rjs/page.rb', line 35

def pop_container
  stack.pop
end

#push_container(container) ⇒ Object



31
32
33
# File 'lib/versatile_rjs/page.rb', line 31

def push_container(container)
  stack.push container
end

#remove(id, *args) ⇒ Object



72
73
74
# File 'lib/versatile_rjs/page.rb', line 72

def remove(id, *args)
  self[id].remove(*args)
end

#replace(id, *args) ⇒ Object



64
65
66
# File 'lib/versatile_rjs/page.rb', line 64

def replace(id, *args)
  self[id].replace(*args)
end

#replace_html(id, *args) ⇒ Object



60
61
62
# File 'lib/versatile_rjs/page.rb', line 60

def replace_html(id, *args)
  self[id].replace_html(*args)
end

#select(selector) ⇒ Object



52
53
54
# File 'lib/versatile_rjs/page.rb', line 52

def select(selector)
  VersatileRJS::Proxy::SelectorProxy.new_instance(self, selector)
end

#statement_prefix(statement) ⇒ Object



88
89
90
# File 'lib/versatile_rjs/page.rb', line 88

def statement_prefix(statement)
  "try{" if VersatileRJS.debug_rjs?
end

#statement_suffix(statement) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/versatile_rjs/page.rb', line 92

def statement_suffix(statement)
  if VersatileRJS.debug_rjs
    <<-EOS
    }catch(e){
      alert('RJS Error: ' + e);
      alert('#{view.__send__(:escape_javascript, statement)}');
      throw e;
    }
    EOS
  end
end