Skip to content

Instantly share code, notes, and snippets.

@Epictetus
Forked from melborne/app_for_chinatra.rb
Created June 7, 2011 20:50
Show Gist options
  • Save Epictetus/1013124 to your computer and use it in GitHub Desktop.
Save Epictetus/1013124 to your computer and use it in GitHub Desktop.
Another syntax Sinatra
require "sinatra"
module Chinatra
class Application
SINATRA_METHODS = Sinatra::Delegator.private_instance_methods(false)
def self.parse(fname)
file = open(fname) rescue open("#{fname}.rb")
code = file.lines.each_with_object([]) do |line, mem|
case line
when /^(#{SINATRA_METHODS.join('|')})\s*(.*)\s*$/
mem << "};" << %[ #{$1}("#{$2}") { ]
when /^\s+-\s+(.*)$/
mem << 'def ' + $1
when /^\s+(.*)\s+(-)$/
mem << $1 + ' end'
else
mem << line + ';'
end
end
eval code.rotate.join
end
end
end
module Kernel
alias :_require :require
def require(feature)
_require feature
rescue LoadError
Chinatra::Application.parse(feature)
end
end
at_exit { Sinatra::Application.run! }
require "sinatra"
module Shynatra
module Path
def parse(sym)
'/' + sym.to_s.gsub('_', '/').gsub('@', ':').gsub('/6', '/:')
end
end
CRUD = "CRUDH".each_char
M_TBL = Hash[CRUD.zip %w(post get put delete helpers)]
CRUD.each do |name|
klass = Class.new do
extend Path
def self./(body)
path, args, blk = body
Sinatra::Application.send(M_TBL[self.to_s[/\w+$/]], parse(path), *args, &blk)
end
end
const_set(name, klass)
end
end
[Symbol, NilClass].each do |klass|
klass.class_eval do
%w(| / _).each do |name|
define_method(name) { |*args, &blk| [self, args, blk] }
end
end
end
include Shynatra
END { Sinatra::Application.run! }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment