Pārlūkot izejas kodu

3-tier w/ behaviours

master
Ludovic 'Archivist' Lagouardette pirms 3 gadiem
vecāks
revīzija
91402d2ad2
6 mainītis faili ar 52 papildinājumiem un 13 dzēšanām
  1. +13
    -0
      scripts/my_func.lua
  2. +4
    -0
      shard.lock
  3. +2
    -0
      shard.yml
  4. +10
    -4
      src/app/business.cr
  5. +1
    -1
      src/app/button.ecr
  6. +22
    -8
      src/app/view.cr

+ 13
- 0
scripts/my_func.lua Parādīt failu

@ -0,0 +1,13 @@
function run_event(name)
print("run " .. name)
if(name == "archivist") then
Business.fetch_add(10)
else
Business.fetch_add(1)
end
end
function display_event(name)
print("display " .. name)
return Business.fetch_add(0)
end

+ 4
- 0
shard.lock Parādīt failu

@ -16,6 +16,10 @@ shards:
git: https://github.com/jeromegn/kilt.git
version: 0.6.1
lua:
git: https://github.com/veelenga/lua.cr.git
version: 0.2.0
radix:
git: https://github.com/luislavena/radix.git
version: 0.4.1

+ 2
- 0
shard.yml Parādīt failu

@ -11,6 +11,8 @@ targets:
dependencies:
kemal:
github: kemalcr/kemal
lua:
github: veelenga/lua.cr
crystal: 1.0.0

+ 10
- 4
src/app/business.cr Parādīt failu

@ -1,9 +1,15 @@
require "./model.cr"
require "lua"
class Business
def self.fetch_add(v = 1)
v += Model.get;
Model.set v;
return v;
include LuaCallable
def fetch_add(v = 1)
if v.is_a?(Float64)
v += Model.get;
Model.set v.to_i!;
return v.to_i!;
else
raise "Danger: wrong return value"
end
end
end

+ 1
- 1
src/app/button.ecr Parādīt failu

@ -2,7 +2,7 @@
<html>
<head></head>
<body>
<div> <%= count %> </div>
<div> <%= display_event.as(Lua::Function).call(name).as(Float64).to_i32!; %> </div>
<form method="POST" action=<%= "/"+name %>>
<input type="submit" value="Go">
</form>

+ 22
- 8
src/app/view.cr Parādīt failu

@ -3,16 +3,30 @@ require "./business.cr"
get "/:name" do |env|
name = env.params.url["name"]
count = Business.fetch_add(0);
render "./src/app/button.ecr";
stack = Lua.load;
stack.set_global("Business", Business.new);
stack.run File.new("./scripts/my_func.lua");
display_event = stack.run %q{
return display_event;
};
view = render "./src/app/button.ecr";
stack.close;
view;
end
post "/:name" do |env|
adder = 1;
name = env.params.url["name"]
if name == "archivist"
adder = 10;
end
count = Business.fetch_add(adder);
render "./src/app/button.ecr";
stack = Lua.load;
stack.set_global("Business", Business.new);
stack.run File.new("./scripts/my_func.lua");
run_event = stack.run %q{
return run_event;
};
display_event = stack.run %q{
return display_event;
};
run_event.as(Lua::Function).call(name);
view = render "./src/app/button.ecr";
stack.close;
view;
end

Notiek ielāde…
Atcelt
Saglabāt