lab42_core 0.5.0 → 0.5.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 +4 -4
- data/README.md +3 -0
- data/lib/lab42/core.rb +13 -10
- data/lib/lab42/core/behavior/send_behavior.rb +0 -8
- data/lib/lab42/core/console_tools/func.rb +0 -11
- data/lib/lab42/core/dir.rb +0 -2
- data/lib/lab42/core/enumerable.rb +9 -0
- data/lib/lab42/core/hash.rb +1 -0
- data/lib/lab42/core/state_machine.rb +49 -0
- data/lib/lab42/core/state_machine/class_methods.rb +29 -0
- data/lib/lab42/core/state_machine/match.rb +3 -0
- data/lib/lab42/core/state_machine/tools.rb +31 -0
- data/lib/lab42/core/state_machine/transition.rb +44 -0
- data/lib/lab42/core/version.rb +1 -1
- metadata +9 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 416a1aa0325efa09f4ead0d7bb91b80532cbcdf2
|
4
|
+
data.tar.gz: 33802025f6ffa984026b3c686748bad3af4bdac3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a666044f58c6ecc7b77ab56e75591c671697df76196041c6017aeb4d97cc262cd93b83760062a52aca1c960f27921c9e7ee9b558bbf9934596f7ef1bf595a573
|
7
|
+
data.tar.gz: f04cc077dec9d194a319a860b11caff5fe3d049a43c94bbcd8f317a6b38d74f421fe929b2da3f79835e8e5d81d9d63d9dc1873d45fb66ffdbea9fa178052112f
|
data/README.md
CHANGED
@@ -3,11 +3,14 @@
|
|
3
3
|
|
4
4
|
[](https://travis-ci.org/RobertDober/lab42_core)
|
5
5
|
[](https://codeclimate.com/github/RobertDober/lab42_core)
|
6
|
+
[](https://codeclimate.com/github/RobertDober/lab42_core)
|
6
7
|
[](https://codeclimate.com/github/RobertDober/lab42_core)
|
7
8
|
[](http://badge.fury.io/rb/lab42_core)
|
8
9
|
|
9
10
|
Simple Ruby Core Module Extensions (for more see lab42\_more)
|
10
11
|
|
12
|
+
** Attention v0.6.0 pushes functional behavior to [lab42_function](https://github/RobertDober/lab42_function)**
|
13
|
+
|
11
14
|
## Programming Paradigms
|
12
15
|
|
13
16
|
### Functional
|
data/lib/lab42/core.rb
CHANGED
@@ -1,11 +1,14 @@
|
|
1
1
|
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
require_relative './core/
|
6
|
-
require_relative './core/
|
7
|
-
require_relative './core/
|
8
|
-
require_relative './core/
|
9
|
-
require_relative './core/
|
10
|
-
require_relative './core/
|
11
|
-
require_relative './core/
|
2
|
+
# requires are inside for full support of compact class/module definition syntax
|
3
|
+
module Lab42
|
4
|
+
module Core
|
5
|
+
require_relative './core/kernel'
|
6
|
+
require_relative './core/array'
|
7
|
+
require_relative './core/dir'
|
8
|
+
require_relative './core/enumerable'
|
9
|
+
require_relative './core/file'
|
10
|
+
require_relative './core/hash'
|
11
|
+
require_relative './core/open_object'
|
12
|
+
require_relative './core/memoization'
|
13
|
+
end
|
14
|
+
end
|
@@ -16,14 +16,6 @@ module Lab42
|
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
|
-
# Rebinding:
|
20
|
-
# We still apply our LIFO policy! So much for the proverbial
|
21
|
-
# "The early bird catches the worm"? Anyway, what would I do
|
22
|
-
# with a worm??? (Mescal maybe?)
|
23
|
-
def _ *a, &b
|
24
|
-
self.class.new method, *(a + args), &(b||block)
|
25
|
-
end
|
26
|
-
|
27
19
|
private
|
28
20
|
def initialize method, *args, &block
|
29
21
|
@method = method
|
@@ -7,19 +7,8 @@ def _realize_args_ para, args
|
|
7
7
|
args.map{ |arg| Proc === arg ? arg.(para) :arg }
|
8
8
|
end
|
9
9
|
|
10
|
-
def _transform_for_f_ args
|
11
|
-
args.map do | arg |
|
12
|
-
case arg
|
13
|
-
when Array
|
14
|
-
f *arg
|
15
|
-
else
|
16
|
-
arg
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
10
|
|
21
11
|
def f *args
|
22
|
-
args = _transform_for_f_ args
|
23
12
|
# At this level we need to see if there is a Lab42::Receiver
|
24
13
|
if Symbol === args.first
|
25
14
|
-> r do
|
data/lib/lab42/core/dir.rb
CHANGED
data/lib/lab42/core/hash.rb
CHANGED
@@ -8,6 +8,7 @@ class Hash
|
|
8
8
|
self[ key ] = defblk ? defblk.() : defaults.first
|
9
9
|
end
|
10
10
|
end
|
11
|
+
alias_method :fetch_or_set, :fetch!
|
11
12
|
|
12
13
|
def replace_rec *keys, limit: nil, limits: nil, &valblock
|
13
14
|
raise ArgumentError, "must not pass in limit: and limits: keyword parameters" if limit && limits
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module Lab42::Core::StateMachine
|
2
|
+
require_relative 'state_machine/class_methods'
|
3
|
+
require_relative 'state_machine/transition'
|
4
|
+
require_relative 'state_machine/tools'
|
5
|
+
T = Lab42::Core::StateMachine::Tools
|
6
|
+
|
7
|
+
attr_reader :current_state, :object
|
8
|
+
|
9
|
+
def self.included into
|
10
|
+
into.extend ClassMethods
|
11
|
+
end
|
12
|
+
|
13
|
+
def run input
|
14
|
+
|
15
|
+
input.each_with_index do |line, idx|
|
16
|
+
match = __handlers__[current_state].find_with_value{ |t, h| t.match line, idx, h }
|
17
|
+
next unless match
|
18
|
+
__run_handler__ match
|
19
|
+
@current_state = match.state
|
20
|
+
end
|
21
|
+
__after_last__
|
22
|
+
|
23
|
+
object
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def initialize start_state, initial_value
|
29
|
+
@current_state = start_state
|
30
|
+
@object = initial_value
|
31
|
+
end
|
32
|
+
|
33
|
+
def __after_last__
|
34
|
+
handler = __handlers__[T.end_state_id].first
|
35
|
+
instance_exec(&handler) if handler
|
36
|
+
end
|
37
|
+
|
38
|
+
def __run_handler__ match
|
39
|
+
case match.handler
|
40
|
+
when Proc
|
41
|
+
instance_exec(match, &match.handler)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def __handlers__
|
46
|
+
@__handlers__ ||= self.class.send :__handlers__
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Lab42::Core::StateMachine::ClassMethods
|
2
|
+
|
3
|
+
require_relative 'transition'
|
4
|
+
require_relative 'tools'
|
5
|
+
T = Lab42::Core::StateMachine::Tools
|
6
|
+
|
7
|
+
def add_transition from_state, trigger_expr, *handler, to: from_state, &handler_block
|
8
|
+
handler = T.define_handler(handler, handler_block)
|
9
|
+
trigger = Lab42::Core::StateMachine::Transition
|
10
|
+
.new(to, trigger_expr)
|
11
|
+
__register__ from_state, [trigger, handler]
|
12
|
+
end
|
13
|
+
|
14
|
+
def after_last_input *handler, &handler_block
|
15
|
+
handler = T.define_handler(handler, handler_block)
|
16
|
+
__register__ T.end_state_id, handler
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def __register__ state, actions
|
23
|
+
__handlers__[state] << actions
|
24
|
+
end
|
25
|
+
|
26
|
+
def __handlers__
|
27
|
+
@__handlers__ ||= Hash.new{ |h, k| h[k] = [] }
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Lab42::Core::StateMachine::Tools extend self
|
2
|
+
|
3
|
+
|
4
|
+
def end_state_id
|
5
|
+
@__end_state_id__ ||= "END_#{SecureRandom.hex}"
|
6
|
+
end
|
7
|
+
|
8
|
+
def define_handler handler, handler_block
|
9
|
+
raise ArgumentError, "cannot provide handler and block" if
|
10
|
+
handler_block && !handler.empty?
|
11
|
+
raise ArgumentError, "must not provide more than one handler" if
|
12
|
+
handler.size > 1
|
13
|
+
|
14
|
+
make_handler( handler.first || handler_block )
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def make_handler handler
|
21
|
+
case handler
|
22
|
+
when Symbol
|
23
|
+
-> *args do
|
24
|
+
send(handler, *args)
|
25
|
+
end
|
26
|
+
when Proc
|
27
|
+
handler
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require_relative '../fn'
|
2
|
+
|
3
|
+
class Lab42::Core::StateMachine::Transition
|
4
|
+
|
5
|
+
require_relative 'match'
|
6
|
+
|
7
|
+
attr_reader :next_state, :matcher, :matcher_arity
|
8
|
+
def initialize next_state, matcher
|
9
|
+
@next_state = next_state
|
10
|
+
@matcher = make_matcher matcher
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
def match line, idx, handler
|
15
|
+
if matcher_arity == 1
|
16
|
+
matcher.(line)
|
17
|
+
else
|
18
|
+
matcher.(line, idx)
|
19
|
+
end.tap do | result |
|
20
|
+
return Lab42::Core::StateMachine::Match.new(
|
21
|
+
data: result, state: next_state, transition: self, line: line, index: idx, handler: handler
|
22
|
+
) if result
|
23
|
+
return nil
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def make_matcher matcher
|
30
|
+
case matcher
|
31
|
+
when Regexp
|
32
|
+
@matcher_arity = 1
|
33
|
+
matcher.fn.match
|
34
|
+
when Proc, Method, Lab42::Behavior
|
35
|
+
@matcher_arity = matcher.arity
|
36
|
+
matcher
|
37
|
+
when TrueClass
|
38
|
+
->(*){true}
|
39
|
+
else
|
40
|
+
raise ArgumentError, "cannot make a behavoir from #{matcher}"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
data/lib/lab42/core/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lab42_core
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robert Dober
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-12-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: forwarder2
|
@@ -143,6 +143,11 @@ files:
|
|
143
143
|
- lib/lab42/core/meta/hash.rb
|
144
144
|
- lib/lab42/core/old_ruby2.rb
|
145
145
|
- lib/lab42/core/open_object.rb
|
146
|
+
- lib/lab42/core/state_machine.rb
|
147
|
+
- lib/lab42/core/state_machine/class_methods.rb
|
148
|
+
- lib/lab42/core/state_machine/match.rb
|
149
|
+
- lib/lab42/core/state_machine/tools.rb
|
150
|
+
- lib/lab42/core/state_machine/transition.rb
|
146
151
|
- lib/lab42/core/version.rb
|
147
152
|
- lib/lab42/file.rb
|
148
153
|
- lib/lab42/lazy.rb
|
@@ -158,7 +163,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
158
163
|
requirements:
|
159
164
|
- - ">="
|
160
165
|
- !ruby/object:Gem::Version
|
161
|
-
version: 2.
|
166
|
+
version: 2.3.1
|
162
167
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
163
168
|
requirements:
|
164
169
|
- - ">="
|
@@ -166,7 +171,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
166
171
|
version: '0'
|
167
172
|
requirements: []
|
168
173
|
rubyforge_project:
|
169
|
-
rubygems_version: 2.6.
|
174
|
+
rubygems_version: 2.6.14
|
170
175
|
signing_key:
|
171
176
|
specification_version: 4
|
172
177
|
summary: Simple Ruby Core Module Extensions (for more see lab42_more)
|