Module: Plezi::Controller::ClassMethods

Defined in:
lib/plezi/controller/controller_class.rb

Constant Summary collapse

RESERVED_METHODS =

This is used internally by Plezi, do not use.

[:delete, :create, :update, :new, :show, :pre_connect, :on_open, :on_close, :on_shutdown, :on_message].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object

A Ruby callback used to initialize class data for new Controllers.


6
7
8
# File 'lib/plezi/controller/controller_class.rb', line 6

def self.extended(base)
  base._pl_init_class_data
end

Instance Method Details

#_pl_ad_mapObject

This function is used internally by Plezi, do not call.


118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/plezi/controller/controller_class.rb', line 118

def _pl_ad_map
  return @_pl_ad_map if @_pl_ad_map

  @_pl_ad_map = {}
  mths = public_instance_methods false
  mths.delete_if { |m| m.to_s[0] == '_' || ![-2, -1, 1].freeze.include?(instance_method(m).arity) }
  mths.delete :index
  RESERVED_METHODS.each { |m| mths.delete m }
  mths.each { |m| @_pl_ad_map[m.to_s.freeze] = m; @_pl_ad_map[m] = m }

  @_pl_ad_map
end

#_pl_get_mapObject

This function is used internally by Plezi, do not call.


47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/plezi/controller/controller_class.rb', line 47

def _pl_get_map
  return @_pl_get_map if @_pl_get_map

  @_pl_get_map = {}
  mths = public_instance_methods false
  mths.delete_if { |m| m.to_s[0] == '_' || !(-1..0).cover?(instance_method(m).arity) }
  @_pl_get_map[nil] = :index if mths.include?(:index)
  RESERVED_METHODS.each { |m| mths.delete m }
  mths.each { |m| @_pl_get_map[m.to_s.freeze] = m }

  @_pl_get_map
end

#_pl_has_createObject

This function is used internally by Plezi, do not call.


74
75
76
# File 'lib/plezi/controller/controller_class.rb', line 74

def _pl_has_create
  @_pl_has_create
end

#_pl_has_deleteObject

This function is used internally by Plezi, do not call.


62
63
64
# File 'lib/plezi/controller/controller_class.rb', line 62

def _pl_has_delete
  @_pl_has_delete
end

#_pl_has_newObject

This function is used internally by Plezi, do not call.


80
81
82
# File 'lib/plezi/controller/controller_class.rb', line 80

def _pl_has_new
  @_pl_has_new
end

#_pl_has_showObject

This function is used internally by Plezi, do not call.


86
87
88
# File 'lib/plezi/controller/controller_class.rb', line 86

def _pl_has_show
  @_pl_has_show
end

#_pl_has_updateObject

This function is used internally by Plezi, do not call.


68
69
70
# File 'lib/plezi/controller/controller_class.rb', line 68

def _pl_has_update
  @_pl_has_update
end

#_pl_init_class_dataObject

This function is used internally by Plezi, do not call.


159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/plezi/controller/controller_class.rb', line 159

def _pl_init_class_data
  @auto_dispatch ||= nil
  @_pl_get_map = nil
  @_pl_ad_map = nil
  @_pl_ws_map = nil
  @_pl_has_show = public_instance_methods(false).include?(:show)
  @_pl_has_new = public_instance_methods(false).include?(:new)
  @_pl_has_create = public_instance_methods(false).include?(:create)
  @_pl_has_update = public_instance_methods(false).include?(:update)
  @_pl_has_delete = public_instance_methods(false).include?(:delete)
  @_pl_is_websocket = (instance_variable_defined?(:@auto_dispatch) && instance_variable_get(:@auto_dispatch)) || instance_methods(false).include?(:on_message)
  _pl_get_map
  _pl_ad_map
  _pl_ws_map
end

#_pl_is_ad?Boolean

This function is used internally by Plezi, do not call.


98
99
100
# File 'lib/plezi/controller/controller_class.rb', line 98

def _pl_is_ad?
  @auto_dispatch
end

#_pl_is_websocket?Boolean

This function is used internally by Plezi, do not call.


92
93
94
# File 'lib/plezi/controller/controller_class.rb', line 92

def _pl_is_websocket?
  @_pl_is_websocket
end

#_pl_params2method(params, env) ⇒ Object

This function is used internally by Plezi, do not call.


133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/plezi/controller/controller_class.rb', line 133

def _pl_params2method(params, env)
  par_id = params['id'.freeze]
  meth_id = _pl_get_map[par_id]
  return meth_id if par_id && meth_id  # puts "matching against #{params}"

  case params['_method'.freeze]
  when :get # since this is common, it's pushed upwards.
    if env['HTTP_UPGRADE'.freeze] && _pl_is_websocket? && env['HTTP_UPGRADE'.freeze].downcase.start_with?('websocket'.freeze)
      @_pl_init_global_data ||= ::Plezi.plezi_initialize # wake up pub/sub drivers in case of `fork`
      return :preform_upgrade
    end
    return :new if _pl_has_new && par_id == 'new'.freeze
    return meth_id || (_pl_has_show && :show) || nil
  when :put, :patch
    return :create if _pl_has_create && (par_id.nil? || par_id == 'new'.freeze)
    return :update if _pl_has_update
  when :post
    return :create if _pl_has_create
  when :delete
    return :delete if _pl_has_delete
  end
  meth_id || (_pl_has_show && :show) || nil
end

#_pl_ws_mapObject

This function is used internally by Plezi, do not call.


104
105
106
107
108
109
110
111
112
113
114
# File 'lib/plezi/controller/controller_class.rb', line 104

def _pl_ws_map
  return @_pl_ws_map if @_pl_ws_map

  @_pl_ws_map = {}
  mths = instance_methods false
  mths.delete :index
  RESERVED_METHODS.each { |m| mths.delete m }
  mths.each { |m| @_pl_ws_map[m.to_s.freeze] = m; @_pl_ws_map[m] = m }

  @_pl_ws_map
end

#broadcast(event_method, *args) ⇒ Object

Invokes a method on every websocket connection that belongs to this Controller / Type. When using Iodine, the method is invoked asynchronously.

self.broadcast :my_method, "argument 1", "argument 2", 3

Methods invoked using #unicast, #broadcast or #multicast will quitely fail if the connection was lost, the requested method is undefined or the 'target' was invalid.


29
30
31
# File 'lib/plezi/controller/controller_class.rb', line 29

def broadcast(event_method, *args)
  ::Plezi::Base::MessageDispatch.broadcast(self, event_method, args)
end

#multicast(event_method, *args) ⇒ Object

Invokes a method on every websocket connection in the application.

self.multicast :my_method, "argument 1", "argument 2", 3

Methods invoked using #unicast, #broadcast or #multicast will quitely fail if the connection was lost, the requested method is undefined or the 'target' was invalid.


38
39
40
# File 'lib/plezi/controller/controller_class.rb', line 38

def multicast(event_method, *args)
  ::Plezi::Base::MessageDispatch.multicast(self, event_method, args)
end

#unicast(target, event_method, *args) ⇒ Object

Invokes a method on the `target` websocket connection. When using Iodine, the method is invoked asynchronously.

self.unicast target, :my_method, "argument 1"

Methods invoked using #unicast, #broadcast or #multicast will quitely fail if the connection was lost, the requested method is undefined or the 'target' was invalid.


20
21
22
# File 'lib/plezi/controller/controller_class.rb', line 20

def unicast(target, event_method, *args)
  ::Plezi::Base::MessageDispatch.unicast(self, target, event_method, args)
end

#url_for(func, params = {}) ⇒ Object

Returns a relative URL for the controller, placing the requested parameters in the URL (inline, where possible and as query data when not possible).


11
12
13
# File 'lib/plezi/controller/controller_class.rb', line 11

def url_for(func, params = {})
  ::Plezi::Base::Router.url_for self, func, params
end