The Crystal programming language | http://crystal-lang.org | Fund Crystal's development: http://is.gd/X7PRtI | Docs: http://crystal-lang.org/docs/ | API: http://crystal-lang.org/api/
0.is_this_a_0_at_compile_time # should compile fine
1.is_this_a_0_at_compile_time # should throw a compile time error
module WebWare
abstract class Method
abstract def call(args)
end
class Api
@@store = {} of String => Method
def initialize
end
def self.register(name : String, method : Method)
@@store[name] = method
end
def self.run(context : HTTP::Server::Context)
begin
data = Tools.json_param(context)
finalResult = data.map do |key, value|
{key => @@store[key].call(value.as(Hash))}
end
return finalResult.to_json
rescue ex
# context.response.respond_with_error("Internal error", 500)
"Internal error in Method"
end
end
end
end
include WebWare
class Users < Method
def call(args)
begin
count = Db.scalar "select count(*) from restock.users"
data = [] of {id: String, name: String}
Db.query "select id, name from restock.users" do |rs|
rs.each do
id, name = rs.read(String, String)
data << {id: id, name: name}
end
end
{success: true, data: data}
rescue ex
{success: false, data: [] of {id: String, name: String}}
end
end
end
Api.register "user.list", Users.new
macro def
you have access to @type
(Int32
) but not self
(0
) at compile time