generic 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +4 -0
- data/README.md +31 -0
- data/Rakefile +2 -0
- data/bin/generic +8 -0
- data/lib/generic.rb +2 -0
- data/lib/generic/boot.rb +10 -0
- data/lib/generic/generic.rb +184 -0
- data/lib/generic/help.rb +3 -0
- data/lib/generic/routing.rb +76 -0
- data/lib/generic/target.rb +49 -0
- data/lib/generic/version.rb +3 -0
- metadata +84 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: dd6950356e997c63545003fcfdb3009fc75159b1
|
4
|
+
data.tar.gz: e75f52114b5311e2b398eb3a5ede128ae36caf74
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3dd444b799993ef39d8516d4cc917b7db0f7996d7f12dbae047d25056bd5cc9075d35bd928e89bdee1ff34e326d3644c5c2cdec5f00cf34843960f8b0981bb9e
|
7
|
+
data.tar.gz: ba594f139687a12ebdbc3118eb07f26e8c21ce7c083d9cf9b2462e465ee0a5de1fec2f1e56488b7a83e2f6cb74771fb53d9979719a6812ff654b44bcdb28d794
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# Generic
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'generic'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install generic
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
TODO: Write usage instructions here
|
24
|
+
|
25
|
+
## Contributing
|
26
|
+
|
27
|
+
1. Fork it ( https://github.com/[my-github-username]/generic/fork )
|
28
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
29
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
30
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
31
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/generic
ADDED
data/lib/generic.rb
ADDED
data/lib/generic/boot.rb
ADDED
@@ -0,0 +1,184 @@
|
|
1
|
+
require 'rack'
|
2
|
+
require 'active_support/hash_with_indifferent_access'
|
3
|
+
require 'active_support/core_ext/object/deep_dup'
|
4
|
+
require 'pathname'
|
5
|
+
require 'generic/target'
|
6
|
+
require 'generic/routing'
|
7
|
+
|
8
|
+
module Generic
|
9
|
+
|
10
|
+
class Request < Rack::Request
|
11
|
+
def params
|
12
|
+
@params ||= ActiveSupport::HashWithIndifferentAccess.new(super)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class InsEval
|
17
|
+
def self.instance(scope, &block)
|
18
|
+
scope.instance_eval &block
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.apply(scope, &handler)
|
22
|
+
scope.apply &handler
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.hand(scope_class, handler, before_actions, after_actions)
|
26
|
+
Proc.new do |env|
|
27
|
+
scope = scope_class.new(env)
|
28
|
+
response = catch (:halt) do
|
29
|
+
before_actions.each do |action|
|
30
|
+
Generic::InsEval.instance(scope, &action)
|
31
|
+
end
|
32
|
+
Generic::InsEval.apply(scope, &handler)
|
33
|
+
end
|
34
|
+
catch (:halt) do
|
35
|
+
after_actions.each do |action|
|
36
|
+
Generic::InsEval.apply(scope, &action)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
response.finish
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
class Response < Rack::Response
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.root
|
49
|
+
@root ||= Pathname.pwd
|
50
|
+
end
|
51
|
+
|
52
|
+
class Base
|
53
|
+
include Genericer::General
|
54
|
+
|
55
|
+
# General
|
56
|
+
general(:scope, Class.new(Target))
|
57
|
+
general(:rd, [])
|
58
|
+
general(:before_actions, [])
|
59
|
+
general(:after_actions, [])
|
60
|
+
general(:guard, {})
|
61
|
+
general(:routes, {})
|
62
|
+
general(:impt, [])
|
63
|
+
|
64
|
+
def initialize(app=nil)
|
65
|
+
# Rack Builder
|
66
|
+
builder = Rack::Builder.new
|
67
|
+
|
68
|
+
#Register Importing Classes
|
69
|
+
self.class.impt.each do |im, args, block|
|
70
|
+
builder.use(im, *args, &block)
|
71
|
+
end
|
72
|
+
|
73
|
+
# Register group nested
|
74
|
+
self.class.routes.each do |url, a_class|
|
75
|
+
builder.map(url) do
|
76
|
+
use a_class
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
builder.run Generic::Routing.new({
|
81
|
+
:scope => self.class.scope,
|
82
|
+
:rd => self.class.rd,
|
83
|
+
:before_actions => self.class.before_actions,
|
84
|
+
:after_actions => self.class.after_actions,
|
85
|
+
:fail => app
|
86
|
+
})
|
87
|
+
|
88
|
+
@app = builder.to_app
|
89
|
+
end
|
90
|
+
|
91
|
+
|
92
|
+
def call(env)
|
93
|
+
@app.call(env)
|
94
|
+
end
|
95
|
+
|
96
|
+
class << self
|
97
|
+
|
98
|
+
# Methods
|
99
|
+
|
100
|
+
def get(path, options={}, &block)
|
101
|
+
options[:constraints] = guard.merge(options[:constraints] || {})
|
102
|
+
options[:constraints].merge!(:request_method => "GET")
|
103
|
+
define_route(path, options, &block)
|
104
|
+
end
|
105
|
+
|
106
|
+
def post(path, options={}, &block)
|
107
|
+
options[:constraints] = guard.merge(options[:constraints] || {})
|
108
|
+
options[:constraints].merge!(:request_method => "POST")
|
109
|
+
define_route(path, options, &block)
|
110
|
+
end
|
111
|
+
|
112
|
+
def put(path, options={}, &block)
|
113
|
+
options[:constraints] = guard.merge(options[:constraints] || {})
|
114
|
+
options[:constraints].merge!(:request_method => "PUT")
|
115
|
+
define_route(path, options, &block)
|
116
|
+
end
|
117
|
+
|
118
|
+
def delete(path, options={}, &block)
|
119
|
+
options[:constraints] = guard.merge(options[:constraints] || {})
|
120
|
+
options[:constraints].merge!(:request_method => "DELETE")
|
121
|
+
define_route(path, options, &block)
|
122
|
+
end
|
123
|
+
|
124
|
+
# Group Route
|
125
|
+
def group(url, app=nil, &block)
|
126
|
+
scope = self.scope
|
127
|
+
app = Class.new(self.superclass) do
|
128
|
+
self.scope = scope
|
129
|
+
class_eval(&block)
|
130
|
+
end
|
131
|
+
routes[url] = app
|
132
|
+
end
|
133
|
+
|
134
|
+
# Register Extension
|
135
|
+
def register(*extensions)
|
136
|
+
extensions.each do |ext|
|
137
|
+
extend ext
|
138
|
+
if ext.check(self)
|
139
|
+
ext.respond_to?(:registered)
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
# Set Guard
|
145
|
+
def set_guard(args, &block)
|
146
|
+
current = self.guard.dup
|
147
|
+
self.guard = args
|
148
|
+
instance_eval(&block)
|
149
|
+
self.guard = current
|
150
|
+
end
|
151
|
+
|
152
|
+
# System Helper
|
153
|
+
def helpers(*args, &block)
|
154
|
+
if block_given?
|
155
|
+
args << Module.new(&block)
|
156
|
+
end
|
157
|
+
args.each do |m|
|
158
|
+
scope.send :include, m
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
# Import
|
163
|
+
def import(cls, *args, &block)
|
164
|
+
impt << [cls, args, block]
|
165
|
+
end
|
166
|
+
|
167
|
+
# Before Proccess
|
168
|
+
def before(&block)
|
169
|
+
before_actions << Proc.new(&block)
|
170
|
+
end
|
171
|
+
|
172
|
+
# After Proccess
|
173
|
+
def after(&block)
|
174
|
+
after_actions << Proc.new(&block)
|
175
|
+
end
|
176
|
+
|
177
|
+
protected
|
178
|
+
# Define Framework Route
|
179
|
+
def define_route(path, options, &block)
|
180
|
+
self.rd << [path, options, Proc.new(&block)]
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|
data/lib/generic/help.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
require 'action_dispatch/routing'
|
3
|
+
require 'action_dispatch/journey'
|
4
|
+
|
5
|
+
module Genericer
|
6
|
+
class DJRoutes < ActionDispatch::Journey::Routes
|
7
|
+
def initialize
|
8
|
+
super
|
9
|
+
end
|
10
|
+
end
|
11
|
+
class DJRouter < ActionDispatch::Journey::Router
|
12
|
+
def initialize(routes, params)
|
13
|
+
super
|
14
|
+
end
|
15
|
+
end
|
16
|
+
class DJJourner < ActionDispatch::Journey::Path::Pattern
|
17
|
+
def initialize(path)
|
18
|
+
super
|
19
|
+
end
|
20
|
+
end
|
21
|
+
module General
|
22
|
+
def self.included(base)
|
23
|
+
base.class_eval do
|
24
|
+
def self.general(name, value)
|
25
|
+
@g ||= []
|
26
|
+
@g << name
|
27
|
+
self.class.send(:attr_accessor, name)
|
28
|
+
self.send("#{name}=", value)
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.inherited(sc)
|
32
|
+
@g.each do |a|
|
33
|
+
sc.general(a, self.send(a).deep_dup)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
module Generic
|
42
|
+
class Routing
|
43
|
+
attr_reader :scope, :jr, :before_actions, :after_actions, :fail
|
44
|
+
|
45
|
+
def initialize(options)
|
46
|
+
@scope = options[:scope]
|
47
|
+
@before_actions = options[:before_actions]
|
48
|
+
@after_actions = options[:after_actions]
|
49
|
+
@fail = options[:fail]
|
50
|
+
ready(options[:rd])
|
51
|
+
end
|
52
|
+
|
53
|
+
def call(env)
|
54
|
+
response = jr.call(env)
|
55
|
+
if response[0] == 404 and fail
|
56
|
+
fail.call(env)
|
57
|
+
else
|
58
|
+
response
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def ready(rd)
|
63
|
+
routes = Genericer::DJRoutes.new
|
64
|
+
@jr = Genericer::DJRouter.new(routes, {})
|
65
|
+
rd.each do |path, options, handler|
|
66
|
+
pat = Genericer::DJJourner.new(path)
|
67
|
+
guard = options.fetch(:guard, {})
|
68
|
+
defaults = options.fetch(:defaults, {})
|
69
|
+
@jr.routes.add_route(Generic::InsEval.hand(scope, handler, before_actions, after_actions),
|
70
|
+
pat,
|
71
|
+
guard,
|
72
|
+
defaults)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'forwardable'
|
2
|
+
require 'rack/contrib/cookies'
|
3
|
+
|
4
|
+
module Generic
|
5
|
+
class Target
|
6
|
+
extend Forwardable
|
7
|
+
|
8
|
+
attr_reader :request, :response
|
9
|
+
def_delegators :request, :session, :params
|
10
|
+
def_delegators :response, :headers
|
11
|
+
|
12
|
+
def initialize(p)
|
13
|
+
@request = Request.new(p)
|
14
|
+
@response = Response.new [], 200, {'Content-Type' => 'text/html'}
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def system_redirect(uri, status=302)
|
20
|
+
exit(status, {'Location' => uri})
|
21
|
+
end
|
22
|
+
|
23
|
+
def status(code)
|
24
|
+
response.status = code
|
25
|
+
end
|
26
|
+
|
27
|
+
def exit(status, headers={}, body='')
|
28
|
+
@response = Response.new(body, status, headers)
|
29
|
+
cookies.finish!(response) if @cookies
|
30
|
+
throw :halt, response
|
31
|
+
end
|
32
|
+
|
33
|
+
def system_cookies
|
34
|
+
@cookies ||= Rack::Cookies::CookieJar.new(request.cookies)
|
35
|
+
end
|
36
|
+
|
37
|
+
[:goto, :go_to].each do |keyword|
|
38
|
+
alias_method keyword, :system_redirect
|
39
|
+
end
|
40
|
+
|
41
|
+
public
|
42
|
+
def apply(&handler)
|
43
|
+
data = instance_eval(&handler)
|
44
|
+
data.respond_to?(:each) ? response.body = data : response.write(data)
|
45
|
+
cookies.finish!(response) if @cookies
|
46
|
+
response
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: generic
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Write your name
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-08-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: Write a longer description. Optional.
|
42
|
+
email:
|
43
|
+
- Write your email address
|
44
|
+
executables:
|
45
|
+
- generic
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- README.md
|
50
|
+
- Gemfile
|
51
|
+
- Rakefile
|
52
|
+
- lib/generic/boot.rb
|
53
|
+
- lib/generic/generic.rb
|
54
|
+
- lib/generic/help.rb
|
55
|
+
- lib/generic/routing.rb
|
56
|
+
- lib/generic/target.rb
|
57
|
+
- lib/generic/version.rb
|
58
|
+
- lib/generic.rb
|
59
|
+
- bin/generic
|
60
|
+
homepage: ''
|
61
|
+
licenses:
|
62
|
+
- MIT
|
63
|
+
metadata: {}
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - '>='
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
requirements: []
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 2.0.14
|
81
|
+
signing_key:
|
82
|
+
specification_version: 4
|
83
|
+
summary: Write a short summary. Required.
|
84
|
+
test_files: []
|