grand_central 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +91 -4
- data/lib/grand_central/version.rb +1 -1
- data/lib/grand_central.rb +4 -1
- 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: 6c0e9a7148ac9f9b9e5b20cc9274a586b9afe8e0
|
4
|
+
data.tar.gz: a7086f1e94374a7a9d80e8392387fcabde4fc414
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4a76db401fa74f3ea0d82f0ec85f191407098b8c8317c74fb5d08f6b91805d0e40f7140daec007e41d58a6cfd5b689d7fbadb99a82b645e843dd7cde791f91c1
|
7
|
+
data.tar.gz: dc22db5f648f0c447a60840bc397907c71000f384f9b18e5ba7d387168bd09169a66dee9782058866bb53d10b306ac52f523a35a2a896490536695dada069fe1
|
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# GrandCentral
|
2
2
|
|
3
|
-
|
3
|
+
GrandCentral is a state-management and action-dispatching library for Opal apps. It was created with [Clearwater](https://github.com/clearwater-rb/clearwater) apps in mind, but there's no reason you couldn't use it with other types of Opal apps.
|
4
4
|
|
5
|
-
|
5
|
+
GrandCentral is based on ideas similar to [Redux](http://rackt.github.io/redux/). You have a central store that holds all your state. This state is updated via a reducer block when you dispatch actions to the store.
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
@@ -22,7 +22,94 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
## Usage
|
24
24
|
|
25
|
-
|
25
|
+
First, you'll need a store. You'll need to seed it with initial state and give it a reducer function:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
require 'grand_central'
|
29
|
+
|
30
|
+
store = GrandCentral::Store.new(a: 1, b: 2) do |state, action|
|
31
|
+
case action
|
32
|
+
when :a
|
33
|
+
# Notice we aren't updating the state in-place. We are returning a new
|
34
|
+
# value for it by passing a new value for the :a key
|
35
|
+
state.merge a: state[:a] + 1
|
36
|
+
when :b
|
37
|
+
state.merge b: state[:b] + 1
|
38
|
+
else # Always return the given state if you aren't updating it.
|
39
|
+
state
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
store.dispatch :a
|
44
|
+
store.dispatch :b
|
45
|
+
store.dispatch "You can dispatch anything you want, really"
|
46
|
+
```
|
47
|
+
|
48
|
+
### Actions
|
49
|
+
|
50
|
+
The actions you dispatch to the store can be anything. We used symbols in the above example, but GrandCentral also provides a class called `GrandCentral::Action` to help you set up your actions:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
module Actions
|
54
|
+
include GrandCentral
|
55
|
+
|
56
|
+
# The :todo becomes a method on the action, similar to a Struct
|
57
|
+
AddTodo = Action.with_attributes(:todo)
|
58
|
+
DeleteTodo = Action.with_attributes(:todo)
|
59
|
+
ToggleTodo = Action.with_attributes(:todo) do
|
60
|
+
# We don't want to toggle the Todo in place. We want a new instance of it.
|
61
|
+
def toggled_todo
|
62
|
+
Todo.new(
|
63
|
+
id: todo.id,
|
64
|
+
name: todo.name,
|
65
|
+
complete: !todo.complete?
|
66
|
+
)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
```
|
71
|
+
|
72
|
+
Then your reducer can use these actions to update the state more easily:
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
store = GrandCentral::Store.new(todos: []) do |state, action|
|
76
|
+
case action
|
77
|
+
when Actions::AddTodo
|
78
|
+
state.merge todos: state[:todos] + [action.todo]
|
79
|
+
when Actions::DeleteTodo
|
80
|
+
state.merge todos: state[:todos] - [action.todo]
|
81
|
+
when Actions::ToggleTodo
|
82
|
+
state.merge todos: state[:todos].map { |todo|
|
83
|
+
# We want to replace the todo in the array
|
84
|
+
if todo.id == action.todo.id
|
85
|
+
action.todo
|
86
|
+
else
|
87
|
+
todo
|
88
|
+
end
|
89
|
+
}
|
90
|
+
else
|
91
|
+
state
|
92
|
+
end
|
93
|
+
end
|
94
|
+
```
|
95
|
+
|
96
|
+
### Performing actions on dispatch
|
97
|
+
|
98
|
+
You may want your application to do something in response to a dispatch. For example, in a Clearwater app, you might want to re-render the application when the store's state has changed:
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
store = GrandCentral::Store.new(todos: []) do |state, action|
|
102
|
+
# ...
|
103
|
+
end
|
104
|
+
|
105
|
+
app = Clearwater::Application.new(component: Layout.new)
|
106
|
+
|
107
|
+
store.on_dispatch do |old_state, new_state|
|
108
|
+
app.render unless old_state.equal?(new_state)
|
109
|
+
end
|
110
|
+
```
|
111
|
+
|
112
|
+
Notice the `unless old_state.equal?(new_state)` clause. This is one of the reasons we recommend you update state by returning a new value instead of mutating it in-place. It allows you to do cache invalidation in O(1) time.
|
26
113
|
|
27
114
|
## Development
|
28
115
|
|
@@ -32,5 +119,5 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
32
119
|
|
33
120
|
## Contributing
|
34
121
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
122
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/clearwater-rb/grand_central. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
36
123
|
|
data/lib/grand_central.rb
CHANGED
@@ -1,7 +1,10 @@
|
|
1
1
|
require "grand_central/version"
|
2
2
|
require "grand_central/store"
|
3
|
+
require "grand_central/action"
|
3
4
|
|
4
5
|
module GrandCentral
|
5
6
|
end
|
6
7
|
|
7
|
-
|
8
|
+
if RUBY_ENGINE != 'opal'
|
9
|
+
Opal.append_path File.expand_path('..', __FILE__)
|
10
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grand_central
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jamie Gaskins
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-09-
|
11
|
+
date: 2015-09-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: opal
|