Created
June 5, 2011 06:18
-
-
Save melborne/1008708 to your computer and use it in GitHub Desktop.
Another syntax Sinatra
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require './chinatra' | |
| require 'haml' | |
| require 'myroute' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require "./shynatra" | |
| require "haml" | |
| R/nil./ { | |
| @message = message "Shynatra" | |
| haml :index | |
| } | |
| R/:hello./ { | |
| "You are in: '/hello'" | |
| } | |
| R/:foo_bar./ { | |
| "You are in: '/foo/bar'" | |
| } | |
| R/:@name./ { | |
| "You are in: '/:name' with '#{params[:name]}'" | |
| } | |
| R/:foo_6name./ { | |
| "You are in: '/foo/:name' with '#{params[:name]}'" | |
| } | |
| R/:foo_6bar_baz_6name./ { | |
| "You are in '/foo/:bar/baz/:name' with :bar => #{params[:bar]}, :name => #{params[:name]}" | |
| } | |
| H/nil./ { | |
| def message(app) | |
| "Welcome to #{app}!" | |
| end | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.inject([]) do |mem, line| | |
| 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! } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| %h1= @message |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| get / | |
| @message = "Welcome to Chinatra!" | |
| haml :index | |
| get /hi | |
| 'hi, my friend!' | |
| get /:name | |
| hello(params[:name]) | |
| helpers | |
| - hello(name) | |
| "Wow you are #{name}!" - |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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