rudux 0.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3366b5bb893b073eca9ac76ae800fc12f0ddc6c4
4
+ data.tar.gz: bffb63faac917180249526e66a2dcea835657fb0
5
+ SHA512:
6
+ metadata.gz: c43df47a8a3546499bcf34dd38447cc5e4ef5c80bdd715a732f76dec276350f38252c834d66f2977c7205b9f881c78be4687233182c18e0a6ecbe2192012d023
7
+ data.tar.gz: 7d4cfa68536740a613e6d9cb043ef4741d18619248e19eb033595ea40d8c68e5d9f45c2e488450cb606d673f0526708a0ded5b949998536407487dc082c4f517
@@ -0,0 +1,15 @@
1
+ module Rudux
2
+ class Action
3
+
4
+ def to_sym
5
+ @@sym_cache ||= {}
6
+ @@sym_cache[self.class.name] ||= self.class.name.
7
+ split('::').last.
8
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
9
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
10
+ tr("-", "_").
11
+ downcase.to_sym
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module Rudux
2
+ class Combined
3
+ def initialize hash
4
+ @hash = hash
5
+ end
6
+
7
+ def reduce state, action
8
+ @hash.keys.map do |key|
9
+ reducer = @hash[key]
10
+ reduced = reducer.reduce(state.send(key), action)
11
+ Hash[key, reduced]
12
+ end.reduce({}, &:merge!)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,43 @@
1
+ require 'securerandom'
2
+
3
+ module Rudux
4
+ class Entity
5
+ attr_reader :id
6
+
7
+ def initialize hash={}
8
+ unless hash.has_key? :id
9
+ @id = SecureRandom.uuid.freeze
10
+ end
11
+ hash.each do |key, value|
12
+ value.freeze
13
+ instance_variable_set("@#{key}", value)
14
+ end
15
+ end
16
+
17
+ def to_hash
18
+ Hash[instance_variables.map{ |key|
19
+ [key[1..-1].to_sym, instance_variable_get(key)]
20
+ }]
21
+ end
22
+
23
+ def copy hash={}
24
+ if hash.empty?
25
+ self
26
+ else
27
+ self.class.new to_hash.merge! hash
28
+ end
29
+ end
30
+
31
+ def mutate hash={}
32
+ if hash.empty?
33
+ self
34
+ else
35
+ hash.keys.each do |key|
36
+ instance_variable_set("@#{key}", hash[key])
37
+ end
38
+ self
39
+ end
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,50 @@
1
+ require 'rudux/combined'
2
+
3
+ module Rudux
4
+ class MutatingReducer
5
+
6
+ def self.reduce states, action
7
+ if states.is_a? Array
8
+ reduce_array_of_states(states, action)
9
+ elsif states.is_a? Hash
10
+ reduce_hash_of_states(states, action)
11
+ else
12
+ reduce_single_state(states, action)
13
+ end
14
+ end
15
+
16
+ def self.reduce_array_of_states states, action
17
+ states.map do |state|
18
+ reduce_single_state(state, action)
19
+ end
20
+ end
21
+
22
+ def self.reduce_hash_of_states hash, action
23
+ hash.map do |state|
24
+ result = reduce_single_state(state[1], action)
25
+ Hash[result.id, result]
26
+ end.reduce(&:merge!)
27
+ end
28
+
29
+ def self.reduce_single_state state, action
30
+ base = self.base.reduce(state, action)
31
+ if self.respond_to? action.to_sym
32
+ this = self.send(action.to_sym, state, action)
33
+ else
34
+ this = {}
35
+ end
36
+ merged = base.merge!(this)
37
+ if merged.empty?
38
+ state
39
+ else
40
+ # TODO: Everything but this line is essentially a copy of Rudux::Reducer
41
+ state.mutate(merged)
42
+ end
43
+ end
44
+
45
+ def self.base
46
+ Rudux::Combined.new({})
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,49 @@
1
+ require 'rudux/combined'
2
+
3
+ module Rudux
4
+ class Reducer
5
+
6
+ def self.reduce states, action
7
+ if states.is_a? Array
8
+ reduce_array_of_states(states, action)
9
+ elsif states.is_a? Hash
10
+ reduce_hash_of_states(states, action)
11
+ else
12
+ reduce_single_state(states, action)
13
+ end
14
+ end
15
+
16
+ def self.reduce_array_of_states states, action
17
+ states.map do |state|
18
+ reduce_single_state(state, action)
19
+ end
20
+ end
21
+
22
+ def self.reduce_hash_of_states hash, action
23
+ hash.map do |state|
24
+ result = reduce_single_state(state[1], action)
25
+ Hash[result.id, result]
26
+ end.reduce(&:merge!)
27
+ end
28
+
29
+ def self.reduce_single_state state, action
30
+ base = self.base.reduce(state, action)
31
+ if self.respond_to? action.to_sym
32
+ this = self.send(action.to_sym, state, action)
33
+ else
34
+ this = {}
35
+ end
36
+ merged = base.merge!(this)
37
+ if merged.empty?
38
+ state
39
+ else
40
+ state.copy(merged)
41
+ end
42
+ end
43
+
44
+ def self.base
45
+ Rudux::Combined.new({})
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1,19 @@
1
+ module Rudux
2
+ class Store
3
+ attr_reader :state
4
+
5
+ def initialize reducer, state
6
+ @reducer = reducer
7
+ @state = state
8
+ end
9
+
10
+ def dispatch action
11
+ @state = @reducer.reduce(@state, action)
12
+ end
13
+
14
+ def select selector
15
+ selector.select(@state)
16
+ end
17
+
18
+ end
19
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rudux
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Christopher Okhravi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-11-10 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Redux like state management in OO Ruby
14
+ email:
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/rudux/action.rb
20
+ - lib/rudux/combined.rb
21
+ - lib/rudux/entity.rb
22
+ - lib/rudux/mutating_reducer.rb
23
+ - lib/rudux/reducer.rb
24
+ - lib/rudux/store.rb
25
+ homepage: https://github.com/chrokh/rudux
26
+ licenses:
27
+ - MIT
28
+ metadata: {}
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 2.5.1
46
+ signing_key:
47
+ specification_version: 4
48
+ summary: Redux like state management in OO Ruby
49
+ test_files: []