Class: Menilite::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/menilite/controller.rb

Defined Under Namespace

Classes: ActionInfo

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session, settings, request) ⇒ Controller

Returns a new instance of Controller.



10
11
12
13
14
# File 'lib/menilite/controller.rb', line 10

def initialize(session, settings, request)
  @settings = settings
  @session = session
  @request = request
end

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request.



8
9
10
# File 'lib/menilite/controller.rb', line 8

def request
  @request
end

#sessionObject (readonly)

Returns the value of attribute session.



8
9
10
# File 'lib/menilite/controller.rb', line 8

def session
  @session
end

#settingsObject (readonly)

Returns the value of attribute settings.



8
9
10
# File 'lib/menilite/controller.rb', line 8

def settings
  @settings
end

Class Method Details

.action(name, options = {}, &block) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/menilite/controller.rb', line 28

def action(name, options = {}, &block)
  action_info[name.to_s] = ActionInfo.new(name, block.parameters, options)
  if RUBY_ENGINE == 'opal'
    method = Proc.new do |*args, &callback| # todo: should adopt keyword parameters
      action_url = self.respond_to?(:namespace) ? "api/#{self.namespace}/#{name}" : "api/#{name}"
      post_data = {}
      post_data[:args] = args
      Menilite::Http.post_json(action_url, post_data) do
        on :success do |res|
          callback.call(:success, res) if callback
        end

        on :failure do |res|
          callback.call(:failure, res) if callback
        end
      end
    end
    self.instance_eval do
      define_singleton_method(name, method)
    end
  else
    self.instance_eval do
      define_method(name, block)
    end
  end
end

.action_infoObject



18
19
20
# File 'lib/menilite/controller.rb', line 18

def action_info
  @action_info ||= {}
end

.before_action(options = {}, &block) ⇒ Object



55
56
57
# File 'lib/menilite/controller.rb', line 55

def before_action(options = {}, &block)
  before_action_handlers << { proc: block, options: options }
end

.before_action_handlersObject



22
23
24
# File 'lib/menilite/controller.rb', line 22

def before_action_handlers
  @before_action_handlers ||= []
end