cynic 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +3 -0
- data/Guardfile +9 -0
- data/README.md +4 -2
- data/bin/cynic +11 -3
- data/lib/cynic/app.rb +6 -1
- data/lib/cynic/controller.rb +40 -2
- data/lib/cynic/renderer.rb +7 -2
- data/lib/cynic/routing.rb +9 -4
- data/lib/cynic/version.rb +1 -1
- data/spec/lib/cynic/controller_spec.rb +70 -2
- data/spec/lib/cynic/renderer_spec.rb +10 -1
- data/spec/lib/cynic/routing_spec.rb +3 -3
- data/spec/spec_helper.rb +6 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 386528d8ad4483f347d5b07827b8858c31fb919f
|
4
|
+
data.tar.gz: 8b6b303da8866422bc0aa8bbf820c4e00ddfde48
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d1abf0e9a88251822232e472e5fc4cfefe222901b74e7fe00e10fc02a14bcc792da028409a302cba9b597eff2c50f94bfdcf538059459e68b638dab5c7a9f542
|
7
|
+
data.tar.gz: 9250c5e4df267f107920b0a3e446648a7dd9ea4318d7697b2613342707d22587d05722ff61ab4a4bba8bc5c4906f089e616ab82602c168488ec2b47732e75cea
|
data/Gemfile
CHANGED
data/Guardfile
ADDED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Cynic
|
2
2
|
|
3
|
-
|
3
|
+
Cynic is a Lightweight Rack-based framework. Cynic provides a framework with a few tools built on the ideas of rails.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -18,7 +18,9 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
|
21
|
+
```bash
|
22
|
+
cynic new <name>
|
23
|
+
```
|
22
24
|
|
23
25
|
## Contributing
|
24
26
|
|
data/bin/cynic
CHANGED
@@ -51,9 +51,6 @@ module #{@app_name}
|
|
51
51
|
# Your code here
|
52
52
|
end
|
53
53
|
end
|
54
|
-
|
55
|
-
|
56
|
-
|
57
54
|
HEREDOC
|
58
55
|
end
|
59
56
|
end
|
@@ -69,6 +66,16 @@ run #{self.name}
|
|
69
66
|
end
|
70
67
|
end
|
71
68
|
|
69
|
+
def create_controller
|
70
|
+
make_file("app/controllers/application_controller.rb") do |file|
|
71
|
+
file.write <<-HEREDOC
|
72
|
+
class ApplicationController < Cynic::Controller
|
73
|
+
# Application wide methods and before_actions go here
|
74
|
+
end
|
75
|
+
HEREDOC
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
72
79
|
def create_layout
|
73
80
|
Dir.mkdir([self.name, "app", "views", "layouts"].join("/"))
|
74
81
|
make_file("app/views/layouts/application.html.erb") do |file|
|
@@ -102,6 +109,7 @@ when "new"
|
|
102
109
|
Dir.mkdir([generator.name, "public", "images"].join("/"))
|
103
110
|
generator.create_config
|
104
111
|
generator.create_application
|
112
|
+
generator.create_controller
|
105
113
|
generator.create_gemfile
|
106
114
|
generator.create_routes
|
107
115
|
generator.create_layout
|
data/lib/cynic/app.rb
CHANGED
@@ -30,7 +30,12 @@ module Cynic
|
|
30
30
|
end
|
31
31
|
|
32
32
|
def response
|
33
|
-
Response.new(
|
33
|
+
Response.new(execute_controller_actions)
|
34
|
+
end
|
35
|
+
|
36
|
+
def execute_controller_actions
|
37
|
+
object, method = self.routing_to_request
|
38
|
+
object.response(method)
|
34
39
|
end
|
35
40
|
|
36
41
|
def status
|
data/lib/cynic/controller.rb
CHANGED
@@ -1,16 +1,54 @@
|
|
1
1
|
module Cynic
|
2
2
|
class Controller
|
3
|
+
class << self
|
4
|
+
attr_accessor :before_actions
|
5
|
+
def before_actions
|
6
|
+
@before_actions ||= Hash.new { Set.new }
|
7
|
+
end
|
8
|
+
|
9
|
+
def before_action(method, options={})
|
10
|
+
if options.has_key? :only
|
11
|
+
[options[:only]].flatten.each { |action| add_before_action(method, action) }
|
12
|
+
else
|
13
|
+
add_before_action(method)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def add_before_action(method, action=:all)
|
18
|
+
before_actions[action] <<= method
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
3
22
|
def render(action)
|
4
23
|
@action = action
|
5
|
-
Renderer.new(full_path).body
|
24
|
+
Renderer.new(full_path, self).body
|
25
|
+
end
|
26
|
+
|
27
|
+
def response(method)
|
28
|
+
eval_set_of_actions
|
29
|
+
eval_set_of_actions(method)
|
30
|
+
send method
|
6
31
|
end
|
7
32
|
|
33
|
+
private
|
34
|
+
|
35
|
+
# The +name+ split and joined into a string seperated by "/"'s
|
8
36
|
def path
|
9
|
-
|
37
|
+
name.split("::").join("/").downcase
|
10
38
|
end
|
11
39
|
|
40
|
+
# The +path+ plus the current action.html.erb
|
12
41
|
def full_path
|
13
42
|
path + "/#{@action}.html.erb"
|
14
43
|
end
|
44
|
+
|
45
|
+
# The Controllers class name without the word `controller` in it.
|
46
|
+
def name
|
47
|
+
self.class.to_s.gsub(/controller$/i, "")
|
48
|
+
end
|
49
|
+
|
50
|
+
def eval_set_of_actions(set=:all)
|
51
|
+
self.class.before_actions[set].each {|action| eval(action.to_s)}
|
52
|
+
end
|
15
53
|
end
|
16
54
|
end
|
data/lib/cynic/renderer.rb
CHANGED
@@ -1,12 +1,17 @@
|
|
1
1
|
module Cynic
|
2
2
|
class Renderer
|
3
|
-
def initialize(path)
|
3
|
+
def initialize(path, controller=nil)
|
4
|
+
|
5
|
+
controller.instance_variables.each do |method|
|
6
|
+
self.instance_variable_set(method, controller.instance_variable_get(method))
|
7
|
+
end
|
8
|
+
|
4
9
|
@full_path = path_prefix + path
|
5
10
|
@layout = path_prefix + "layouts/application.html.erb"
|
6
11
|
end
|
7
12
|
|
8
13
|
def body
|
9
|
-
layout { ERB.new(File.read(@full_path)).result }
|
14
|
+
layout { ERB.new(File.read(@full_path)).result(binding) }
|
10
15
|
end
|
11
16
|
|
12
17
|
def layout
|
data/lib/cynic/routing.rb
CHANGED
@@ -12,10 +12,15 @@ module Cynic
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def go_to(request_method, request_path)
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
15
|
+
if action(request_method, request_path)
|
16
|
+
action
|
17
|
+
else
|
18
|
+
raise Error, "undefined routing #{request_method.upcase} '#{request_path}'"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def action(request_method=nil, request_path=nil)
|
23
|
+
@action ||= @routings[request_method.to_sym][request_path]
|
19
24
|
end
|
20
25
|
|
21
26
|
def get(path, options={})
|
data/lib/cynic/version.rb
CHANGED
@@ -1,13 +1,81 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
-
|
2
|
+
|
3
|
+
class CynicController < Cynic::Controller
|
4
|
+
def index
|
5
|
+
@greeting = "hello"
|
6
|
+
render :template
|
7
|
+
end
|
8
|
+
|
9
|
+
def do_i_live_in_a_tub?
|
10
|
+
@answer = "not yet"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
3
14
|
describe Cynic::Controller do
|
4
15
|
let(:controller) { CynicController.new }
|
5
|
-
|
16
|
+
before { CynicController.instance_variable_set(:@before_actions, nil)}
|
6
17
|
describe "#render" do
|
7
18
|
it "finds a file" do
|
8
19
|
Cynic::Renderer.any_instance.stub(:layout_file).and_return("<%= yield %>")
|
9
20
|
File.should_receive(:read).with("app/views/cynic/index.html.erb").once.and_return('This is erb <%= "olleh".reverse %>')
|
10
21
|
expect(controller.render(:index)).to eq "This is erb hello"
|
11
22
|
end
|
23
|
+
|
24
|
+
it "controller instance vars are available to the view" do
|
25
|
+
Cynic::Renderer.any_instance.stub(:layout_file).and_return("<%= yield %>")
|
26
|
+
File.should_receive(:read).with("app/views/cynic/template.html.erb").once.and_return('This is erb <%= @greeting %>')
|
27
|
+
expect(controller.index).to eq "This is erb hello"
|
28
|
+
end
|
29
|
+
|
30
|
+
it "controller instance vars are available to the layout" do
|
31
|
+
Cynic::Renderer.any_instance.stub(:layout_file).and_return("<%= @greeting %> <%= yield %>")
|
32
|
+
File.should_receive(:read).with("app/views/cynic/template.html.erb").once.and_return('This is erb.')
|
33
|
+
expect(controller.index).to eq "hello This is erb."
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe ".before_action" do
|
38
|
+
it "responds to" do
|
39
|
+
expect(CynicController.respond_to?(:before_action)).to be_true
|
40
|
+
end
|
41
|
+
|
42
|
+
it "assigns each of the things to an array" do
|
43
|
+
CynicController.before_action :do_i_live_in_a_tub?
|
44
|
+
expect(CynicController.before_actions[:all]).to include :do_i_live_in_a_tub?
|
45
|
+
end
|
46
|
+
|
47
|
+
context ":all" do
|
48
|
+
it "calls each before action within :all" do
|
49
|
+
CynicController.before_action :do_i_live_in_a_tub?
|
50
|
+
File.should_receive(:read).with("app/views/cynic/template.html.erb").once.and_return('This is erb.')
|
51
|
+
controller.should_receive(:do_i_live_in_a_tub?).once.and_return(true)
|
52
|
+
controller.response(:index)
|
53
|
+
end
|
54
|
+
|
55
|
+
it "can use a instance var defined in a before action" do
|
56
|
+
CynicController.before_action :do_i_live_in_a_tub?
|
57
|
+
File.should_receive(:read).with("app/views/cynic/template.html.erb").once.and_return('Do I live in a tub? <%= @answer %>.')
|
58
|
+
expect(controller.response(:index)).to eq 'Do I live in a tub? not yet.'
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context "when using the :only option" do
|
63
|
+
it "assigns the action name it should be nested under" do
|
64
|
+
CynicController.before_action :do_i_live_in_a_tub?, only: :show
|
65
|
+
expect(CynicController.before_actions[:show]).to include :do_i_live_in_a_tub?
|
66
|
+
end
|
67
|
+
|
68
|
+
it "assigns the action names it should be nested under" do
|
69
|
+
CynicController.before_action :do_i_live_in_a_tub?, only: [:show]
|
70
|
+
expect(CynicController.before_actions[:show]).to include :do_i_live_in_a_tub?
|
71
|
+
end
|
72
|
+
|
73
|
+
it "calls each before action within :all" do
|
74
|
+
CynicController.before_action :do_i_live_in_a_tub?, only: :index
|
75
|
+
File.should_receive(:read).with("app/views/cynic/template.html.erb").once.and_return('This is erb.')
|
76
|
+
controller.should_receive(:do_i_live_in_a_tub?).once.and_return(true)
|
77
|
+
controller.response(:index)
|
78
|
+
end
|
79
|
+
end
|
12
80
|
end
|
13
81
|
end
|
@@ -2,9 +2,9 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Cynic::Renderer do
|
4
4
|
path = "cynic/controller/index.html.erb"
|
5
|
-
let(:renderer) { Cynic::Renderer.new(path) }
|
6
5
|
|
7
6
|
describe "#render" do
|
7
|
+
let(:renderer) { Cynic::Renderer.new(path) }
|
8
8
|
it "finds a file" do
|
9
9
|
File.should_receive(:read).with("app/views/" + path).and_return('This is erb <%= "olleh".reverse %>')
|
10
10
|
expect(renderer.body).to eq "This is erb hello"
|
@@ -15,6 +15,15 @@ describe Cynic::Renderer do
|
|
15
15
|
File.should_receive(:read).with("app/views/" + path).and_return('This is erb <%= "olleh".reverse %>')
|
16
16
|
expect(renderer.body).to eq "Layout: This is erb hello :file"
|
17
17
|
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "controller" do
|
21
|
+
let(:controller) { Cynic::Controller.new }
|
22
|
+
before { controller.instance_variable_set(:@test, "Test")}
|
23
|
+
let(:renderer) { Cynic::Renderer.new(path, controller) }
|
18
24
|
|
25
|
+
it "assigns the cynic controllers instance methods" do
|
26
|
+
expect(renderer.instance_variable_get(:@test)).to eq "Test"
|
27
|
+
end
|
19
28
|
end
|
20
29
|
end
|
@@ -5,16 +5,16 @@ describe Cynic::Routing do
|
|
5
5
|
let(:routing) { defined_routing }
|
6
6
|
|
7
7
|
it "calls the method to be returned" do
|
8
|
-
expect(routing.go_to(:get, "/")).to eq
|
8
|
+
expect(routing.go_to(:get, "/")).to eq [String, :new]
|
9
9
|
end
|
10
10
|
|
11
11
|
it "calls the method to be returned" do
|
12
|
-
expect(routing.go_to(:get, "/blog")).to
|
12
|
+
expect(routing.go_to(:get, "/blog").last).to eq :rand
|
13
13
|
end
|
14
14
|
|
15
15
|
it "returns the index" do
|
16
16
|
Cynic::Controller.any_instance.stub(:index).and_return("This is erb hello")
|
17
|
-
expect(routing.go_to(:get, "/cynic")).to
|
17
|
+
expect(routing.go_to(:get, "/cynic").first).to be_an_instance_of Cynic::Controller
|
18
18
|
end
|
19
19
|
|
20
20
|
it "raises an Routing::Error when the path doesn't exist" do
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cynic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bookis Smuin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-01-
|
11
|
+
date: 2014-01-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|
@@ -62,6 +62,7 @@ extra_rdoc_files: []
|
|
62
62
|
files:
|
63
63
|
- .gitignore
|
64
64
|
- Gemfile
|
65
|
+
- Guardfile
|
65
66
|
- LICENSE.txt
|
66
67
|
- README.md
|
67
68
|
- Rakefile
|