grand_central 0.1.0 → 0.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8abd6b12cae93650613a9f466eb975de3a9eb5aa
4
- data.tar.gz: e2dc7159021c2338fe9cfdb5a98ec488a2653f57
3
+ metadata.gz: 6c0e9a7148ac9f9b9e5b20cc9274a586b9afe8e0
4
+ data.tar.gz: a7086f1e94374a7a9d80e8392387fcabde4fc414
5
5
  SHA512:
6
- metadata.gz: baa7b094d514c66e172b7a3c00ab39da2d77d328c528099d7ee176319fdf3f837f392ec66d2698c6cf1ab906e789470d444d2773e2d9b9ecaf077f4e59a704fd
7
- data.tar.gz: 99ef1e7ac694c4a171af3c46bbf6cce16322df55e69f3e0daa123000ee5716d01b15d392af5c9d2d1429413cad3bfff48b02c6689ba781c6c1790773b43ff34e
6
+ metadata.gz: 4a76db401fa74f3ea0d82f0ec85f191407098b8c8317c74fb5d08f6b91805d0e40f7140daec007e41d58a6cfd5b689d7fbadb99a82b645e843dd7cde791f91c1
7
+ data.tar.gz: dc22db5f648f0c447a60840bc397907c71000f384f9b18e5ba7d387168bd09169a66dee9782058866bb53d10b306ac52f523a35a2a896490536695dada069fe1
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # GrandCentral
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/grand_central`. To experiment with that code, run `bin/console` for an interactive prompt.
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
- TODO: Delete this and the text above, and describe your gem
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
- TODO: Write usage instructions here
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/[USERNAME]/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.
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
 
@@ -1,3 +1,3 @@
1
1
  module GrandCentral
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
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
- Opal.append_path File.expand_path('..', __FILE__)
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.0
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-05 00:00:00.000000000 Z
11
+ date: 2015-09-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: opal