ruta 1.0.2 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +42 -5
- data/lib/ruta/context.rb +1 -1
- data/lib/ruta/handler.rb +16 -2
- data/lib/ruta/history.rb +2 -1
- data/lib/ruta/route.rb +2 -1
- data/lib/ruta/router.rb +8 -4
- data/lib/ruta/version.rb +1 -1
- data/lib/ruta.rb +27 -4
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c031c5e2a865625d021d0f25c9bfb080bcb5355b
|
4
|
+
data.tar.gz: fa54e3be8f2f33388d2b21bb15f636872860b34d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 31a11420d5e281cc6a9d067625a4038d1e847067e7ad722157281fc6b45574e8e8a267879d36642da0c8ebb530412f130f7146395ef844f3e6731894b89f95b1
|
7
|
+
data.tar.gz: 453a1427861d14731546a0a86ec41173fc335f636bb4d3d5791e608eec0bd049c8f2f2198c7a72582b205dea763e4d50440b9081e718b689bc66af5054ef5189
|
data/README.md
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
# Ruta
|
2
2
|
|
3
|
+
##TODO
|
4
|
+
### Patch
|
5
|
+
|
6
|
+
|
7
|
+
### Minor
|
8
|
+
|
9
|
+
### Major
|
10
|
+
|
3
11
|
## Installation
|
4
12
|
|
5
13
|
Add this line to your application's Gemfile:
|
@@ -18,6 +26,24 @@ Or install it yourself as:
|
|
18
26
|
|
19
27
|
## Usage
|
20
28
|
|
29
|
+
##config settings
|
30
|
+
|
31
|
+
To configure Ruta's settings you can use a config block
|
32
|
+
|
33
|
+
Here's a config block with Ruta's default settings
|
34
|
+
```ruby
|
35
|
+
Ruta.configure do |config|
|
36
|
+
config.context_prefix = false
|
37
|
+
end
|
38
|
+
```
|
39
|
+
|
40
|
+
The following settings have the following effects:
|
41
|
+
1. `context_prefix`
|
42
|
+
This setting turns on context prefixing, whenever you navigate to a route
|
43
|
+
the context it's located in is pre-fixed to the front of it, navigating to
|
44
|
+
a context with this turned on will set the url to being just the context name
|
45
|
+
|
46
|
+
|
21
47
|
Making use of this router is pretty simple.
|
22
48
|
|
23
49
|
###Contexts
|
@@ -76,15 +102,23 @@ end
|
|
76
102
|
Next you should define some handlers, not every component needs a handler but every component has one
|
77
103
|
needs to match names with a corresponding handler.
|
78
104
|
|
79
|
-
you use the `handle`
|
105
|
+
you use the `handle` macro to define a handler and you pass into it the name of the element you want this
|
80
106
|
handler to drive.
|
81
107
|
|
108
|
+
You can also however render a sub-context instead of a component, just use:
|
109
|
+
|
110
|
+
```ruby
|
111
|
+
handle :component do |params,url|
|
112
|
+
mount :sub_context
|
113
|
+
end
|
114
|
+
```
|
115
|
+
|
82
116
|
The handler has two variables passed to it:
|
83
117
|
|
84
118
|
1. `params` a hash containing the named params of the route
|
85
119
|
2. `url` a string containing the url of the route being passed to the handler
|
86
120
|
|
87
|
-
The handler must return a renderable component.
|
121
|
+
The handler must return a renderable component or mount a sub-context.
|
88
122
|
|
89
123
|
```ruby
|
90
124
|
Ruta::Handlers.define_for :main do
|
@@ -106,6 +140,9 @@ Ruta::Handlers.define_for :sign_up do
|
|
106
140
|
end
|
107
141
|
```
|
108
142
|
|
143
|
+
You can use `default` inside of a handler to render the component in the default state
|
144
|
+
as it is defined in the context.
|
145
|
+
|
109
146
|
###Routes
|
110
147
|
|
111
148
|
The last thing to define before your app is ready is the routes that will activate the correct handlers to
|
@@ -138,9 +175,9 @@ end
|
|
138
175
|
|
139
176
|
###Renderer
|
140
177
|
|
141
|
-
The
|
178
|
+
The penultimate step is to tell the router how to render your components.
|
142
179
|
|
143
|
-
You do this using `Ruta::Context.handle_render` and then
|
180
|
+
You do this using `Ruta::Context.handle_render` and then define the render macro.
|
144
181
|
Passed into the macro is the `component` you wish to render and the `element_id` of the components mounting
|
145
182
|
point.
|
146
183
|
|
@@ -163,7 +200,7 @@ All you have to do is this
|
|
163
200
|
```ruby
|
164
201
|
Ruta.navigate_to_ref(:info_view,:i_switch,value)
|
165
202
|
```
|
166
|
-
The first arg is the context the route you wish to navigate to is located in, the second is the
|
203
|
+
The first arg is the context of the route you wish to navigate to is located in, the second is the
|
167
204
|
reference of the route, lastly any params you wish to place into the route go next, they are placed into the route as they come.
|
168
205
|
|
169
206
|
You would most probably use it in a 'click' callback in response to the user clicking on somthing
|
data/lib/ruta/context.rb
CHANGED
data/lib/ruta/handler.rb
CHANGED
@@ -16,13 +16,12 @@ module Ruta
|
|
16
16
|
# @see #Handlers#define_for
|
17
17
|
def initialize context,block
|
18
18
|
@context = context
|
19
|
-
instance_exec
|
19
|
+
instance_exec(&block)
|
20
20
|
end
|
21
21
|
|
22
22
|
# wipe the matching element and render a context
|
23
23
|
#
|
24
24
|
# @param [Symbol] context context to be mounted to matching element of handler
|
25
|
-
# @return [Proc] returns a proc to be executed later
|
26
25
|
def mount context
|
27
26
|
handler_name = @handler_name
|
28
27
|
proc {
|
@@ -31,6 +30,21 @@ module Ruta
|
|
31
30
|
}
|
32
31
|
end
|
33
32
|
|
33
|
+
# Render the default content for this component as it is defined in the context.
|
34
|
+
#
|
35
|
+
def default
|
36
|
+
handler_name = @handler_name
|
37
|
+
proc {
|
38
|
+
comp = @context.elements[handler_name][:content]
|
39
|
+
if comp.kind_of?(Proc)
|
40
|
+
comp.call
|
41
|
+
else
|
42
|
+
Context.wipe handler_name
|
43
|
+
Context.render comp, handler_name
|
44
|
+
end
|
45
|
+
}
|
46
|
+
end
|
47
|
+
|
34
48
|
class << self
|
35
49
|
# define handlers for a context
|
36
50
|
#
|
data/lib/ruta/history.rb
CHANGED
@@ -9,7 +9,8 @@ module Ruta
|
|
9
9
|
# @param [String] url to be added to history
|
10
10
|
# @param [Hash] data to be added to history
|
11
11
|
# @param [String] title to be added to history, defaults to ""
|
12
|
-
def push(url,data,title="")
|
12
|
+
def push(context,url,data,title="")
|
13
|
+
url = Ruta.config.context_prefix ? "/#{context}#{url}" : url
|
13
14
|
`history.pushState(#{data.to_n}, #{title}, #{url})`
|
14
15
|
end
|
15
16
|
|
data/lib/ruta/route.rb
CHANGED
@@ -31,7 +31,7 @@ module Ruta
|
|
31
31
|
|
32
32
|
if flags[:to]
|
33
33
|
@type = :handlers
|
34
|
-
@handlers = [flags.delete
|
34
|
+
@handlers = [flags.delete(:to)].flatten
|
35
35
|
elsif flags[:context]
|
36
36
|
@type = :context
|
37
37
|
@handlers = flags.delete :context
|
@@ -129,6 +129,7 @@ module Ruta
|
|
129
129
|
when :context
|
130
130
|
Context.wipe
|
131
131
|
Context.render handlers
|
132
|
+
History.push(@context_ref.ref,"",[],@context_ref.ref)
|
132
133
|
end
|
133
134
|
end
|
134
135
|
|
data/lib/ruta/router.rb
CHANGED
@@ -15,7 +15,7 @@ module Ruta
|
|
15
15
|
def initialize block
|
16
16
|
@current_context = []
|
17
17
|
Context.define(:no_context)
|
18
|
-
instance_exec
|
18
|
+
instance_exec(&block)
|
19
19
|
end
|
20
20
|
|
21
21
|
# set which Context to map the following routes to
|
@@ -40,7 +40,7 @@ module Ruta
|
|
40
40
|
# @param [Symbol] reference to context
|
41
41
|
def root_to reference
|
42
42
|
Router.set_root_to reference
|
43
|
-
context = Context.collection[
|
43
|
+
context = Context.collection[reference]
|
44
44
|
context.routes[:root]= Route.new('/', context,{ context: reference})
|
45
45
|
end
|
46
46
|
|
@@ -79,8 +79,6 @@ module Ruta
|
|
79
79
|
# root_to :main
|
80
80
|
# end
|
81
81
|
# @note please be aware that placing contexts within other contexts doesn't actully do anything.
|
82
|
-
# however it is planed to be able to mount sub contexts to a route and re-direct as neccasary
|
83
|
-
# think mounting engines or sub apps in RoR or Padrino
|
84
82
|
# @yield Use this block to define any routes
|
85
83
|
def define &block
|
86
84
|
new block
|
@@ -91,6 +89,12 @@ module Ruta
|
|
91
89
|
end
|
92
90
|
|
93
91
|
def find_and_execute(path)
|
92
|
+
path =
|
93
|
+
if Ruta.config.context_prefix
|
94
|
+
( path == '/' || path == "") ? path : (path[/(?:\/.*?)(\/.*)/];$1)
|
95
|
+
else
|
96
|
+
path
|
97
|
+
end
|
94
98
|
res = find(path)
|
95
99
|
if res
|
96
100
|
navigate_to res
|
data/lib/ruta/version.rb
CHANGED
data/lib/ruta.rb
CHANGED
@@ -14,10 +14,29 @@ if RUBY_ENGINE == 'opal'
|
|
14
14
|
|
15
15
|
|
16
16
|
module Ruta
|
17
|
-
class
|
17
|
+
class Config
|
18
|
+
attr_accessor :context_prefix
|
19
|
+
def initialize &block
|
20
|
+
@context_prefix = false
|
21
|
+
block_given?
|
22
|
+
instance_exec(self,&block) if block_given?
|
23
|
+
end
|
18
24
|
|
25
|
+
def configure &block
|
26
|
+
instance_exec(&block)
|
27
|
+
end
|
28
|
+
end
|
19
29
|
|
30
|
+
class << self
|
31
|
+
attr_reader :config
|
20
32
|
|
33
|
+
def configure &block
|
34
|
+
if self.config
|
35
|
+
@config.configure(&block)
|
36
|
+
else
|
37
|
+
@config = Config.new(&block)
|
38
|
+
end
|
39
|
+
end
|
21
40
|
# used to retrieve a stored url
|
22
41
|
#
|
23
42
|
# @param [Symbol] context of the stored url, if this is nil it defaults to the current context
|
@@ -32,10 +51,9 @@ if RUBY_ENGINE == 'opal'
|
|
32
51
|
# @param [Symbol] context that route is mounted to
|
33
52
|
# @param [Symbol] ref to a route that you wish to navigate to
|
34
53
|
# @param [Array<String,Number,Boolean>] *params 0 or more params to replace params in the paramterized route
|
35
|
-
# @return [Proc] A proc that can be used as a callback block for an event
|
36
54
|
def navigate_to_ref context, ref,*params
|
37
55
|
route = Router.route_for(context,ref,params)
|
38
|
-
History.push(route[:path],route[:params],route[:title])
|
56
|
+
History.push(context,route[:path],route[:params],route[:title])
|
39
57
|
Router.navigate_to(route)
|
40
58
|
end
|
41
59
|
|
@@ -44,7 +62,12 @@ if RUBY_ENGINE == 'opal'
|
|
44
62
|
# $document.ready do
|
45
63
|
# Ruta.start_app
|
46
64
|
# end
|
47
|
-
def start_app
|
65
|
+
def start_app &block
|
66
|
+
if block_given?
|
67
|
+
configure(&block)
|
68
|
+
else
|
69
|
+
configure unless self.config
|
70
|
+
end
|
48
71
|
Context.render(Router.current_context)
|
49
72
|
Router.find_and_execute(History.current :path)
|
50
73
|
History.listen_for_pop
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruta
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Martin Becker
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2016-
|
12
|
+
date: 2016-02-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: opal
|