Class: Menilite::Controller

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

Defined Under Namespace

Classes: ActionInfo

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session, settings) ⇒ Controller

Returns a new instance of Controller.



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

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

Class Method Details

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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/menilite/controller.rb', line 33

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



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

def action_info
  @action_info ||= {}
end

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



60
61
62
# File 'lib/menilite/controller.rb', line 60

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

.before_action_handlersObject



27
28
29
# File 'lib/menilite/controller.rb', line 27

def before_action_handlers
  @before_action_handlers ||= []
end

Instance Method Details

#sessionObject



14
15
16
# File 'lib/menilite/controller.rb', line 14

def session
  @session
end

#settingsObject



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

def settings
  @settings
end