rubidux 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /debug.rb
11
+ /*.gem
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rubidux.gemspec
4
+ gemspec
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
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
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,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -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,10 @@
1
+ module Rubidux
2
+ class Reducer
3
+ def self.combine(**reducers)
4
+ -> (state, action) {
5
+ state = {} unless state
6
+ Hash[reducers.map { |key, reducer| [key, reducer.(state[key], action)] }]
7
+ }
8
+ end
9
+ end
10
+ 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
@@ -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
@@ -0,0 +1,3 @@
1
+ module Rubidux
2
+ VERSION = "0.0.6"
3
+ end
data/lib/rubidux.rb ADDED
@@ -0,0 +1,8 @@
1
+ require "rubidux/version"
2
+ require "rubidux/store"
3
+ require "rubidux/reducer"
4
+ require "rubidux/middleware"
5
+ require "rubidux/util"
6
+
7
+ module Rubidux
8
+ end
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: