From d8a365736500d32680004c9459365b69ca6470a1 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Thu, 30 Jul 2015 10:09:04 +0300 Subject: [PATCH] some real models --- app/main/controllers/main_controller.rb | 11 ++++++++--- app/main/models/blocks_model.rb | 8 ++++++++ app/main/models/register_model.rb | 10 ++++++++++ app/main/models/user.rb | 12 ------------ app/main/views/main/index.html | 21 ++++++++++----------- 5 files changed, 36 insertions(+), 26 deletions(-) create mode 100644 app/main/models/blocks_model.rb create mode 100644 app/main/models/register_model.rb delete mode 100644 app/main/models/user.rb diff --git a/app/main/controllers/main_controller.rb b/app/main/controllers/main_controller.rb index edb0b96..d854f33 100644 --- a/app/main/controllers/main_controller.rb +++ b/app/main/controllers/main_controller.rb @@ -2,12 +2,14 @@ require "salama" require_relative "interpreter" + module Main class MainController < Volt::ModelController def index init_machine init_classes init_registers + init_blocks end def about @@ -26,9 +28,7 @@ module Main def init_registers page._registers!.clear @interpreter.registers.each do |reg , val| - r = Volt::Model.new :name => reg - r._value = val - page._registers << r + page._registers << RegisterModel.new( :name => reg , :value => val) end end def init_classes @@ -39,6 +39,11 @@ module Main page._classes << c end end + def init_blocks + blocks = BlocksModel.new + page._blocks = blocks + @interpreter.register_event(:instruction_changed, blocks) + end # The main template contains a #template binding that shows another # template. This is the path to that template. It may change based # on the params._component, params._controller, and params._action values. diff --git a/app/main/models/blocks_model.rb b/app/main/models/blocks_model.rb new file mode 100644 index 0000000..812999f --- /dev/null +++ b/app/main/models/blocks_model.rb @@ -0,0 +1,8 @@ +class BlocksModel < Volt::ArrayModel + + def instruction_changed old , ins + self.last._class_name = "" if( page._blocks.length > 0) + self << { :name => ins.to_s , :class_name => "active" } + end + +end diff --git a/app/main/models/register_model.rb b/app/main/models/register_model.rb new file mode 100644 index 0000000..c54ccf4 --- /dev/null +++ b/app/main/models/register_model.rb @@ -0,0 +1,10 @@ +class RegisterModel < Volt::Model + field :name + field :value + + def register_changed reg , old , ins + self.last._class_name = "" if( page._blocks.length > 0) + self << { :name => ins.to_s , :class_name => "active" } + end + +end diff --git a/app/main/models/user.rb b/app/main/models/user.rb deleted file mode 100644 index 1dbf70a..0000000 --- a/app/main/models/user.rb +++ /dev/null @@ -1,12 +0,0 @@ -# By default Volt generates this User model which inherits from Volt::User, -# you can rename this if you want. -class User < Volt::User - # login_field is set to :email by default and can be changed to :username - # in config/app.rb - field login_field - field :name - - validate login_field, unique: true, length: 8 - validate :email, email: true - -end diff --git a/app/main/views/main/index.html b/app/main/views/main/index.html index 9ee3683..7a01b7d 100644 --- a/app/main/views/main/index.html +++ b/app/main/views/main/index.html @@ -19,33 +19,32 @@

Virtual Machine Instruction

page.sources.each do |s|
s
- end
-

method_name

-

Block: #{block_name}

+

{{page._method_name}}

+

Block: {{page._block_name}}

- block.each do |code| - InstructionView :interpreter => interpreter , :instruction => code - end + {{ page._blocks.each do |b| }} +
+ {{b._string}} + {{ end }}
button.bright { "next" }.on(:click) { interpreter.tick } - " ".br

Status

-
state.to_s
+
{{@interpreter.state}}

Stdout

-
interpreter.stdout
+
{{@interpreter.stdout}}
@@ -53,10 +52,10 @@ {{ page._registers.each do |reg| }}
- {{ reg._name }} + {{ reg.name }}
- {{ reg._value }} + {{ reg.value }}
{{end}}