Class: Jellyfish::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/jellyfish/builder.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app = nil, &block) ⇒ Builder

Returns a new instance of Builder.



11
12
13
14
# File 'lib/jellyfish/builder.rb', line 11

def initialize app=nil, &block
  @use, @map, @run, @warmup = [], nil, app, nil
  instance_eval(&block) if block_given?
end

Class Method Details

.app(app = nil, from = nil, to = nil, &block) ⇒ Object



7
8
9
# File 'lib/jellyfish/builder.rb', line 7

def self.app app=nil, from=nil, to=nil, &block
  new(app, &block).to_app(from, to)
end

Instance Method Details

#listen(host, &block) ⇒ Object



37
38
39
# File 'lib/jellyfish/builder.rb', line 37

def listen host, &block
  map('', host: host, &block)
end

#map(path, to: nil, host: nil, &block) ⇒ Object



32
33
34
35
# File 'lib/jellyfish/builder.rb', line 32

def map path, to: nil, host: nil, &block
  key = if host then "http://#{File.join(host, path)}" else path end
  (@map ||= {})[key] = [block, path, to]
end

#rewrite(rules, &block) ⇒ Object



41
42
43
44
45
# File 'lib/jellyfish/builder.rb', line 41

def rewrite rules, &block
  rules.each do |path, to|
    map(path, :to => to, &block)
  end
end

#run(app) ⇒ Object



24
25
26
# File 'lib/jellyfish/builder.rb', line 24

def run app
  @run = app
end

#to_app(from = nil, to = nil) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/jellyfish/builder.rb', line 47

def to_app from=nil, to=nil
  run = if @map then generate_map(@map, @run) else @run end
  fail 'missing run or map statement' unless run
  app = @use.inject(run){ |a, m| m.call(a) }
  result = if to then Rewrite.new(app, from, to) else app end
  @warmup.call(result) if @warmup
  result
end

#use(middleware, *args, &block) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/jellyfish/builder.rb', line 16

def use middleware, *args, &block
  if @map
    current_map, @map = @map, nil
    @use.unshift(lambda{ |app| generate_map(current_map, app) })
  end
  @use.unshift(lambda{ |app| middleware.new(app, *args, &block) })
end

#warmup(lam = nil, &block) ⇒ Object



28
29
30
# File 'lib/jellyfish/builder.rb', line 28

def warmup lam=nil, &block
  @warmup = lam || block
end