redux 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/.travis.yml +20 -0
- data/CHANGELOG.md +6 -0
- data/Gemfile +5 -0
- data/MIT-LICENSE.txt +20 -0
- data/README.md +37 -0
- data/Rakefile +30 -0
- data/lib/redux.rb +12 -0
- data/lib/redux/store.rb +23 -0
- data/lib/redux/version.rb +4 -0
- data/redux.gemspec +21 -0
- data/spec/redux_spec.rb +59 -0
- metadata +58 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 66fc20bcbea5c41bf52d66d27e937a091214b384
|
4
|
+
data.tar.gz: dce5f96521e412ec2c8c263c8030527a9ae0cbba
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fa75d7b37183c6d34b232effd09b24e25caef6014ca11ce3426dd1b81d083209faa1761fabe1e2920c58261fa4724dc9f705766d91437c00b864e057b4727184
|
7
|
+
data.tar.gz: 8603465ba6c4c041f8e78c8defecc12223880f9467a0197e4151d82b00abfe458dd296b6638f06f1d7c9e69ece2763b723ed0e95cd0ce048ce36c3fdf367dd9b
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
sudo: false
|
2
|
+
language: ruby
|
3
|
+
|
4
|
+
script: bundle exec ruby spec/redux_spec.rb
|
5
|
+
|
6
|
+
rvm:
|
7
|
+
- 2.3.0
|
8
|
+
- 2.2
|
9
|
+
- 2.1
|
10
|
+
- 2.0
|
11
|
+
- ruby-head
|
12
|
+
- rbx-2
|
13
|
+
- jruby-head
|
14
|
+
- jruby-9000
|
15
|
+
|
16
|
+
cache:
|
17
|
+
- bundler
|
18
|
+
|
19
|
+
# matrix:
|
20
|
+
# fast_finish: true
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/MIT-LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2016 Jan Lelis, mail@janlelis.de
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# redux.rb [![[version]](https://badge.fury.io/rb/redux.svg)](http://badge.fury.io/rb/redux) [![[travis]](https://travis-ci.org/janlelis/redux.rb.png)](https://travis-ci.org/janlelis/redux.rb)
|
2
|
+
|
3
|
+
A [Redux](https://github.com/rackt/redux) implementation in Ruby.
|
4
|
+
|
5
|
+
|
6
|
+
## Setup
|
7
|
+
|
8
|
+
Add to your `Gemfile`:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'redux'
|
12
|
+
```
|
13
|
+
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
reducer = ->(state = 0, action){
|
19
|
+
case action['type']
|
20
|
+
when 'INCREMENT'
|
21
|
+
state + 1
|
22
|
+
when 'DECREMENT'
|
23
|
+
state - 1
|
24
|
+
else
|
25
|
+
state
|
26
|
+
end
|
27
|
+
}
|
28
|
+
|
29
|
+
store = Redux::Store.new(0, &reducer)
|
30
|
+
store.dispatch "type" => "INCREMENT"
|
31
|
+
store.state # => 1
|
32
|
+
```
|
33
|
+
|
34
|
+
|
35
|
+
## MIT License
|
36
|
+
|
37
|
+
Copyright (C) 2016 Jan Lelis <http://janlelis.com>. Released under the MIT license.
|
data/Rakefile
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# # #
|
2
|
+
# Get gemspec info
|
3
|
+
|
4
|
+
gemspec_file = Dir['*.gemspec'].first
|
5
|
+
gemspec = eval File.read(gemspec_file), binding, gemspec_file
|
6
|
+
info = "#{gemspec.name} | #{gemspec.version} | " \
|
7
|
+
"#{gemspec.runtime_dependencies.size} dependencies | " \
|
8
|
+
"#{gemspec.files.size} files"
|
9
|
+
|
10
|
+
|
11
|
+
# # #
|
12
|
+
# Gem build and install task
|
13
|
+
|
14
|
+
desc info
|
15
|
+
task :gem do
|
16
|
+
puts info + "\n\n"
|
17
|
+
print " "; sh "gem build #{gemspec_file}"
|
18
|
+
FileUtils.mkdir_p 'pkg'
|
19
|
+
FileUtils.mv "#{gemspec.name}-#{gemspec.version}.gem", 'pkg'
|
20
|
+
puts; sh %{gem install --no-document pkg/#{gemspec.name}-#{gemspec.version}.gem}
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
# # #
|
25
|
+
# Start an IRB session with the gem loaded
|
26
|
+
|
27
|
+
desc "#{gemspec.name} | IRB"
|
28
|
+
task :irb do
|
29
|
+
sh "irb -I ./lib -r #{gemspec.name.gsub '-','/'}"
|
30
|
+
end
|
data/lib/redux.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require_relative "redux/version"
|
2
|
+
require_relative "redux/store"
|
3
|
+
|
4
|
+
module Redux
|
5
|
+
def self.combine_reducers(reducers)
|
6
|
+
->(state = {}, action){
|
7
|
+
reducers.reduce({}){ |next_state, (key, reducer)|
|
8
|
+
next_state[key] = reducer.call(state[key], action)
|
9
|
+
}
|
10
|
+
}
|
11
|
+
end
|
12
|
+
end
|
data/lib/redux/store.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
module Redux
|
2
|
+
class Store
|
3
|
+
attr_reader :state
|
4
|
+
|
5
|
+
def initialize(initial_state = nil, &reducer)
|
6
|
+
@state = initial_state
|
7
|
+
@reducer = reducer || ->(*){}
|
8
|
+
@listeners = []
|
9
|
+
dispatch({})
|
10
|
+
end
|
11
|
+
|
12
|
+
def dispatch(action)
|
13
|
+
@state = @reducer.call(@state, action)
|
14
|
+
@listeners.each{ |listener| listener.call() }
|
15
|
+
end
|
16
|
+
|
17
|
+
def subscribe(&listener)
|
18
|
+
@listeners << listener
|
19
|
+
->{ @listeners.delete(listener) }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
data/redux.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require File.dirname(__FILE__) + "/lib/redux/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |gem|
|
6
|
+
gem.name = "redux"
|
7
|
+
gem.version = Redux::VERSION
|
8
|
+
gem.summary = "redux.rb"
|
9
|
+
gem.description = "Tiny Redux implementation in Ruby."
|
10
|
+
gem.authors = ["Jan Lelis"]
|
11
|
+
gem.email = ["mail@janlelis.de"]
|
12
|
+
gem.homepage = "https://github.com/janlelis/redux.rb"
|
13
|
+
gem.license = "MIT"
|
14
|
+
|
15
|
+
gem.files = Dir["{**/}{.*,*}"].select{ |path| File.file?(path) && path !~ /^pkg/ }
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.required_ruby_version = "~> 2.0"
|
21
|
+
end
|
data/spec/redux_spec.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
require_relative "../lib/redux"
|
2
|
+
require "minitest/autorun"
|
3
|
+
|
4
|
+
describe Redux do
|
5
|
+
describe Redux::Store do
|
6
|
+
it "has nil as initial state if none given" do
|
7
|
+
store = Redux::Store.new
|
8
|
+
assert_equal nil, store.state
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "example: counter reducer" do
|
12
|
+
reducer = ->(state = 0, action){
|
13
|
+
case action['type']
|
14
|
+
when 'INCREMENT'
|
15
|
+
state + 1
|
16
|
+
when 'DECREMENT'
|
17
|
+
state - 1
|
18
|
+
else
|
19
|
+
state
|
20
|
+
end
|
21
|
+
}
|
22
|
+
|
23
|
+
it "uses passed initial state" do
|
24
|
+
store = Redux::Store.new(0, &reducer)
|
25
|
+
assert_equal 0, store.state
|
26
|
+
end
|
27
|
+
|
28
|
+
it "can be incremented" do
|
29
|
+
store = Redux::Store.new(0, &reducer)
|
30
|
+
store.dispatch "type" => "INCREMENT"
|
31
|
+
assert_equal 1, store.state
|
32
|
+
end
|
33
|
+
|
34
|
+
it "can be decremented" do
|
35
|
+
store = Redux::Store.new(0, &reducer)
|
36
|
+
store.dispatch "type" => "DECREMENT"
|
37
|
+
assert_equal -1, store.state
|
38
|
+
end
|
39
|
+
|
40
|
+
it "can be subscribed to state changes" do
|
41
|
+
callback_called = false
|
42
|
+
store = Redux::Store.new(0, &reducer)
|
43
|
+
store.subscribe { callback_called = true }
|
44
|
+
store.dispatch "type" => "DECREMENT"
|
45
|
+
assert_equal true, callback_called
|
46
|
+
end
|
47
|
+
|
48
|
+
it "can be unsubscribed from state changes" do
|
49
|
+
callback_called = false
|
50
|
+
store = Redux::Store.new(0, &reducer)
|
51
|
+
unsubscribe = store.subscribe { callback_called = true }
|
52
|
+
unsubscribe.call
|
53
|
+
store.dispatch "type" => "DECREMENT"
|
54
|
+
assert_equal false, callback_called
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: redux
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jan Lelis
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-02-01 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Tiny Redux implementation in Ruby.
|
14
|
+
email:
|
15
|
+
- mail@janlelis.de
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".gitignore"
|
21
|
+
- ".travis.yml"
|
22
|
+
- CHANGELOG.md
|
23
|
+
- Gemfile
|
24
|
+
- MIT-LICENSE.txt
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- lib/redux.rb
|
28
|
+
- lib/redux/store.rb
|
29
|
+
- lib/redux/version.rb
|
30
|
+
- redux.gemspec
|
31
|
+
- spec/redux_spec.rb
|
32
|
+
homepage: https://github.com/janlelis/redux.rb
|
33
|
+
licenses:
|
34
|
+
- MIT
|
35
|
+
metadata: {}
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - "~>"
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '2.0'
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
requirements: []
|
51
|
+
rubyforge_project:
|
52
|
+
rubygems_version: 2.5.1
|
53
|
+
signing_key:
|
54
|
+
specification_version: 4
|
55
|
+
summary: redux.rb
|
56
|
+
test_files:
|
57
|
+
- spec/redux_spec.rb
|
58
|
+
has_rdoc:
|