Class: DrunkMonkey::Base

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/drunkmonkey.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  path: "/drunkmonkey",
  controller_name: :default_controller
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app = nil, options = {}, &block) ⇒ Base



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/drunkmonkey.rb', line 40

def initialize app = nil, options = {}, &block
  options = DEFAULT_OPTIONS.merge(options)
  @controller = Celluloid::Actor[options[:controller_name]] ||
    Controller.new(options[:controller_name])
  
  instance_eval(&block) if block
  
  mapping = Hash.new
  mapping[options[:path]] = -> env do
    Transport.call env, options
  end
  
  @base_mapping = mapping.dup
  
  mapping["/"] = app if app
  
  @map = Rack::URLMap.new mapping
end

Class Method Details

.middlewareObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/drunkmonkey.rb', line 65

def self.middleware
  Class.new do
    class << self
      attr_accessor :base
    end
    
    def initialize app = nil, options = {}, &block
      if self.class.base
        self.class.base.remap app
      else
        self.class.base = Base.new(app,options,&block)
      end
    end
  
    def call env
      self.class.base.call env
    end
  end
end

Instance Method Details

#remap(app = nil) ⇒ Object



59
60
61
62
63
# File 'lib/drunkmonkey.rb', line 59

def remap app = nil
  mapping = @base_mapping.dup
  mapping["/"] = app if app
  @map = Rack::URLMap.new mapping
end