rubidux 0.0.6
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 +7 -0
- data/.gitignore +11 -0
- data/Gemfile +4 -0
- data/README.md +102 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/rubidux/middleware.rb +25 -0
- data/lib/rubidux/reducer.rb +10 -0
- data/lib/rubidux/store.rb +39 -0
- data/lib/rubidux/util.rb +13 -0
- data/lib/rubidux/version.rb +3 -0
- data/lib/rubidux.rb +8 -0
- data/rubidux.gemspec +25 -0
- metadata +85 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0e432f9f11fa6897a1592213b787e7106337846c
|
4
|
+
data.tar.gz: cfa0fd74d6ffcbde25d78a014ed4c83e8dc40bd5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9c85da202226cefd4414816810289e07723d0df1a4313519ec4c9d1281395cb781116a77fadbfb7e0f2b3a9f0728cca9bc901cf22d1cef293990029d2a93fd34
|
7
|
+
data.tar.gz: 1761320479b451f29cab65e9c4a44eb6c60fff15906458dc124349996bf387587b785fab819f13c724ad4572a7bb5f511740bfbe04040266680d54b82412427e
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
# Rubidux
|
2
|
+
|
3
|
+
Rubidux is a tiny Ruby framework inspired by [Redux](https://github.com/reactjs/redux)
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'rubidux'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install rubidux
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### Rubidux::Reducer
|
24
|
+
Reducers are functions that take state and action as arguments and return a new state. So you can have a reducer like this:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
r1 = -> (state, action) {
|
28
|
+
state = { a: 0, b: 0 } unless state
|
29
|
+
|
30
|
+
case action[:type]
|
31
|
+
when 'a_plus_one'
|
32
|
+
{ a: state[:a]+1, b: state[:b] }
|
33
|
+
when 'b_plus_one'
|
34
|
+
{ a: state[:a], b: state[:b]+1 }
|
35
|
+
else
|
36
|
+
state
|
37
|
+
end
|
38
|
+
}
|
39
|
+
|
40
|
+
r2 = -> (state, action) {
|
41
|
+
state = { c: 0, d: 0 } unless state
|
42
|
+
|
43
|
+
case action[:type]
|
44
|
+
when 'c_plus_one'
|
45
|
+
{ c: state[:c]+1, d: state[:d] }
|
46
|
+
when 'd_plus_one'
|
47
|
+
{ c: state[:c], d: state[:d]+1 }
|
48
|
+
else
|
49
|
+
state
|
50
|
+
end
|
51
|
+
}
|
52
|
+
```
|
53
|
+
|
54
|
+
And you can combine reducers via `conbine`:
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
Rubidux::Reducer.combine(r1: r1, r2: r2)
|
58
|
+
```
|
59
|
+
|
60
|
+
### Rubidux::Middleware
|
61
|
+
You can make a middleware via `init`:
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
m1 = Rubidux::Middleware.init { |action, **middleware_api|
|
65
|
+
puts "#{action} in middleware 1"
|
66
|
+
}
|
67
|
+
|
68
|
+
m2 = Rubidux::Middleware.init { |action, **middleware_api|
|
69
|
+
puts "#{middleware_api[:get_state].()} in middleware 2"
|
70
|
+
}
|
71
|
+
```
|
72
|
+
|
73
|
+
And you can apply these middlewares via `apply`:
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
enhancer = Rubidux::Middleware.apply m1, m2
|
77
|
+
```
|
78
|
+
|
79
|
+
### Rubidux::Store
|
80
|
+
|
81
|
+
You can initialize a new store instance:
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
store = Rubidux::Store.new rc3, {}, enhancer
|
85
|
+
```
|
86
|
+
|
87
|
+
And subscribe a listener via `subscribe`:
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
unsubscribe_function = store.subscribe.(->{puts 'foo'})
|
91
|
+
unsubscribe_function.() # => This will unsubscribe listener
|
92
|
+
```
|
93
|
+
|
94
|
+
Most importantly, you can dispatch an action via `dispatch`:
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
store.dispatch.({type: 'a_plus_one'})
|
98
|
+
```
|
99
|
+
|
100
|
+
## Contributing
|
101
|
+
|
102
|
+
Bug reports and pull requests are welcome on GitHub at [here](https://github.com/davidjuin0519/rubidux).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "rubidux"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
module Rubidux
|
2
|
+
class Middleware
|
3
|
+
def self.init(&block)
|
4
|
+
-> (**middleware_api) {
|
5
|
+
-> (_next) {
|
6
|
+
-> (action) {
|
7
|
+
block.call(action, **middleware_api)
|
8
|
+
_next.(action)
|
9
|
+
}
|
10
|
+
}
|
11
|
+
}
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.apply(*middlewares)
|
15
|
+
-> (get_state, dispatch) {
|
16
|
+
middleware_api = {
|
17
|
+
get_state: get_state,
|
18
|
+
dispatch: -> (action) { dispatch.(action) }
|
19
|
+
}
|
20
|
+
chain = middlewares.map { |middleware| middleware.(middleware_api) }
|
21
|
+
new_dispatch = Rebidux::Util.compose(*chain).(dispatch)
|
22
|
+
}
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Rubidux
|
2
|
+
class Store
|
3
|
+
attr_accessor :state
|
4
|
+
attr_accessor :reducer
|
5
|
+
attr_accessor :listeners
|
6
|
+
attr_accessor :dispatch
|
7
|
+
attr_accessor :subscribe
|
8
|
+
|
9
|
+
def initialize(reducer, preload_state, enhancer = nil)
|
10
|
+
raise ArgumentError.new("Expect reducer to be a Proc.") unless reducer.is_a? Proc
|
11
|
+
@state = preload_state || {}
|
12
|
+
@listeners = []
|
13
|
+
@reducer = reducer
|
14
|
+
@dispatch = _dispatch
|
15
|
+
@subscribe = _subscribe
|
16
|
+
|
17
|
+
@dispatch = enhancer.(-> { @state }, @dispatch) if enhancer
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def _dispatch
|
23
|
+
-> (action) {
|
24
|
+
raise ArgumentError.new("Expect action to have key 'type'.") unless action[:type]
|
25
|
+
@state = @reducer.(@state, action)
|
26
|
+
@listeners.each(&:call)
|
27
|
+
action
|
28
|
+
}
|
29
|
+
end
|
30
|
+
|
31
|
+
def _subscribe
|
32
|
+
-> (listener) {
|
33
|
+
raise ArgumentError.new("Expect listener to be a Proc.") unless listener.is_a? Proc
|
34
|
+
@listeners.push listener
|
35
|
+
-> { @listeners.delete listener }
|
36
|
+
}
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/lib/rubidux/util.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
module Rebidux
|
2
|
+
class Util
|
3
|
+
def self.compose(*funcs)
|
4
|
+
if funcs.size == 1
|
5
|
+
funcs[0]
|
6
|
+
else
|
7
|
+
last = funcs[funcs.size-1]
|
8
|
+
rest = funcs[0..funcs.size-2].reverse
|
9
|
+
-> (*args) { rest.reduce(last.(*args)) { |composed, f| f.(composed) } }
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/lib/rubidux.rb
ADDED
data/rubidux.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rubidux/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "rubidux"
|
8
|
+
spec.version = Rubidux::VERSION
|
9
|
+
spec.authors = ["Juin Chiu"]
|
10
|
+
spec.email = ["davidjuin0519@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = "A tiny ruby framework inspired by Redux"
|
13
|
+
spec.description = "A tiny ruby framework inspired by Redux"
|
14
|
+
spec.homepage = "https://github.com/davidjuin0519/rubidux"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rubidux
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.6
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Juin Chiu
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-10-09 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.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
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: A tiny ruby framework inspired by Redux
|
42
|
+
email:
|
43
|
+
- davidjuin0519@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- README.md
|
51
|
+
- Rakefile
|
52
|
+
- bin/console
|
53
|
+
- bin/setup
|
54
|
+
- lib/rubidux.rb
|
55
|
+
- lib/rubidux/middleware.rb
|
56
|
+
- lib/rubidux/reducer.rb
|
57
|
+
- lib/rubidux/store.rb
|
58
|
+
- lib/rubidux/util.rb
|
59
|
+
- lib/rubidux/version.rb
|
60
|
+
- rubidux.gemspec
|
61
|
+
homepage: https://github.com/davidjuin0519/rubidux
|
62
|
+
licenses: []
|
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.4.5.1
|
81
|
+
signing_key:
|
82
|
+
specification_version: 4
|
83
|
+
summary: A tiny ruby framework inspired by Redux
|
84
|
+
test_files: []
|
85
|
+
has_rdoc:
|