shot_mvc 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,7 +10,8 @@ require 'shot_mvc'
10
10
 
11
11
  class ViewController < Controller
12
12
  def create(view)
13
- contents = <<-eos
13
+ if @app.config['server']['Security']['VisualErrors']
14
+ contents = <<-eos
14
15
  <br /><br />
15
16
  <div class="container">
16
17
  <div class="row">
@@ -21,10 +22,11 @@ class ViewController < Controller
21
22
  </div>
22
23
  </div>
23
24
  </div>
24
- eos
25
+ eos
25
26
 
26
- File.write "application/views/#{view}.erb", contents
27
+ File.write "application/views/#{view}.erb", contents
27
28
 
28
- get('controller', 'Home').index
29
+ get('controller', 'Home').index
30
+ end
29
31
  end
30
32
  end
@@ -0,0 +1,42 @@
1
+ # ControllerDelegate
2
+ #
3
+ # Allows you to perform various actions on controllers, including creating, deleting,
4
+ # and modifying them.
5
+ #
6
+ # Shot Framework - Copyright (c) Jesse Aaron Dunlap <me@jessedunlap.me>
7
+ # Licensed under the MIT License. For full licensing information, please
8
+ # see LICENSE.md. http://github.com/JesseDunlap/shot/
9
+
10
+ require 'erb'
11
+
12
+ require_relative './controller_not_found_exception'
13
+
14
+ class ControllerDelegate
15
+ def self.create_controller(controller_name, *actions)
16
+ template = <<eof
17
+ require 'shot_mvc'
18
+
19
+ class #{controller_name}Controller < Controller
20
+ def setup()
21
+
22
+ end
23
+ <% actions.each do |action| %>
24
+ def <%=action%>
25
+
26
+ end
27
+ <% end %>
28
+ end
29
+ eof
30
+
31
+ template = ERB.new template
32
+ File.write "application/controllers/#{controller_name.underscore}_controller.rb", template.result(binding)
33
+ end
34
+
35
+ def delete_controller(controller_name)
36
+ if File.exists? "application/controllers/#{controller_name.underscore}_controller.rb"
37
+ File.unlink "application/controllers/#{controller_name.underscore}_controller.rb"
38
+ else
39
+ raise ControllerNotFoundException.new "Could not find controller named #{controller_name}"
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,14 @@
1
+ # What is this item?
2
+ #
3
+ # What does this item do?
4
+ #
5
+ # Shot Framework - Copyright (c) Jesse Aaron Dunlap <me@jessedunlap.me>
6
+ # Licensed under the MIT License. For full licensing information, please
7
+ # see LICENSE.md. http://github.com/JesseDunlap/shot/
8
+
9
+
10
+ class ControllerNotFoundException < Exception
11
+ def initialize(message)
12
+ super(message)
13
+ end
14
+ end
@@ -13,6 +13,7 @@ class MVCApplication < Application
13
13
  def initialize(config = { :host => '127.0.0.1', :port => 8080 })
14
14
  super(config)
15
15
 
16
+ add_router SessionRouter
16
17
  add_router ControllerRouter
17
18
  add_loader ControllerLoader
18
19
  add_loader ElementLoader
@@ -0,0 +1,68 @@
1
+ # SessionRouter
2
+ #
3
+ # Allows the application to modify session data for the client.
4
+ #
5
+ # Shot Framework - Copyright (c) Jesse Aaron Dunlap <me@jessedunlap.me>
6
+ # Licensed under the MIT License. For full licensing information, please
7
+ # see LICENSE.md. http://github.com/JesseDunlap/shot/
8
+
9
+ require 'shot'
10
+ require 'json'
11
+
12
+ class ApplicationInstance
13
+ def session_get(key)
14
+ @config[:session] = (@config[:session] or {})
15
+ @config[:session][key]
16
+ end
17
+
18
+ def session_set(key, value)
19
+ @config[:session] = (@config[:session] or {})
20
+ @config[:session][key] = value
21
+ session_save
22
+ end
23
+
24
+ def session_save
25
+ File.write "application/sessions/#{@config[:session_key]}.json", @config[:session].to_json
26
+ end
27
+ end
28
+
29
+ class SessionRouter < Router
30
+ def initialize(client)
31
+ super(client)
32
+
33
+ client.on 'session_key' do |key|
34
+ create_session_folder unless Dir.exists? 'application/sessions'
35
+ create_session_file key unless File.exists? "application/sessions/#{key}.json"
36
+
37
+ client.config[:session_key] = key
38
+ client.config[:session] = JSON.parse File.read "application/sessions/#{key}.json"
39
+
40
+ client.emit 'session_ready'
41
+ end
42
+
43
+ client.on 'request_session_key' do
44
+ session_key = SecureRandom.uuid
45
+ client.emit 'assign_session_key', :key => session_key, :session_hash => client.config['server']['Security']['SessionHash']
46
+
47
+ create_session_folder unless Dir.exists? 'application/sessions'
48
+ create_session_file session_key unless File.exists? "application/sessions/#{session_key}.json"
49
+
50
+ client.config[:session_key] = session_key
51
+ client.config[:session] = JSON.parse File.read "application/sessions/#{session_key}.json"
52
+
53
+ client.emit 'session_ready'
54
+ end
55
+
56
+ client.emit 'provide_session_key', client.config['server']['Security']['SessionHash']
57
+ end
58
+
59
+ private
60
+
61
+ def create_session_folder
62
+ Dir.mkdir 'application/sessions'
63
+ end
64
+
65
+ def create_session_file(key)
66
+ File.write "application/sessions/#{key}.json", '{}'
67
+ end
68
+ end
@@ -28,12 +28,16 @@ class TemplateLoader < Loader
28
28
  Template.new "./application/views/#{name}.erb"
29
29
  end
30
30
  else
31
- begin
31
+ if @client.config['server']['Security']['VisualErrors'] then
32
+ begin
33
+ raise TemplateLoadException.new "Could not load #{name}. Verify it exists at application/views/#{name}.erb"
34
+ rescue Exception => ex
35
+ template = Template.new "#{gem_root}/application/views/view_not_found.erb"
36
+ template.data = { :requested_view => name, :exception => ex }
37
+ return template
38
+ end
39
+ else
32
40
  raise TemplateLoadException.new "Could not load #{name}. Verify it exists at application/views/#{name}.erb"
33
- rescue Exception => ex
34
- template = Template.new "#{gem_root}/application/views/view_not_found.erb"
35
- template.data = { :requested_view => name, :exception => ex }
36
- return template
37
41
  end
38
42
  end
39
43
  end
data/lib/shot_mvc.rb CHANGED
@@ -1,7 +1,9 @@
1
1
  require_relative './shot_mvc/controller_loader'
2
2
  require_relative './shot_mvc/controller_load_exception'
3
+ require_relative './shot_mvc/controller_not_found_exception'
3
4
  require_relative './shot_mvc/controller_router'
4
5
  require_relative './shot_mvc/controller'
6
+ require_relative './shot_mvc/controller_delegate'
5
7
 
6
8
  require_relative './shot_mvc/element'
7
9
  require_relative './shot_mvc/element_loader'
@@ -10,4 +12,6 @@ require_relative './shot_mvc/template_loader'
10
12
  require_relative './shot_mvc/template'
11
13
  require_relative './shot_mvc/template_load_exception'
12
14
 
15
+ require_relative './shot_mvc/session_router'
16
+
13
17
  require_relative './shot_mvc/mvc_application'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shot_mvc
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -52,12 +52,15 @@ extensions: []
52
52
  extra_rdoc_files: []
53
53
  files:
54
54
  - lib/shot_mvc/controller.rb
55
+ - lib/shot_mvc/controller_delegate.rb
55
56
  - lib/shot_mvc/controller_loader.rb
56
57
  - lib/shot_mvc/controller_load_exception.rb
58
+ - lib/shot_mvc/controller_not_found_exception.rb
57
59
  - lib/shot_mvc/controller_router.rb
58
60
  - lib/shot_mvc/element.rb
59
61
  - lib/shot_mvc/element_loader.rb
60
62
  - lib/shot_mvc/mvc_application.rb
63
+ - lib/shot_mvc/session_router.rb
61
64
  - lib/shot_mvc/template.rb
62
65
  - lib/shot_mvc/template_loader.rb
63
66
  - lib/shot_mvc/template_load_exception.rb