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.



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

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

Class Method Details

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



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
59
# File 'lib/menilite/controller.rb', line 34

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
      Browser::HTTP.post(action_url, post_data.to_json) 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



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

def action_info
  @action_info ||= {}
end

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



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

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

.before_action_handlersObject



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

def before_action_handlers
  @before_action_handlers ||= []
end

Instance Method Details

#sessionObject



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

def session
  @session
end

#settingsObject



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

def settings
  @settings
end