lab42_core 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 51b3e6327e2be8479393ed3e879afe81cae225a8
4
- data.tar.gz: b8094fb38f9d1052a33cbfd47b82e2ee9c8ea983
3
+ metadata.gz: 416a1aa0325efa09f4ead0d7bb91b80532cbcdf2
4
+ data.tar.gz: 33802025f6ffa984026b3c686748bad3af4bdac3
5
5
  SHA512:
6
- metadata.gz: 72921a0406b217ce1566e57bfb5305aff13767570167a134d75bdcb7372738ef3f259f9fd15a2ac31407dd4584fc27f78e3a0cc378b8ffc39512e5fad1b6a68a
7
- data.tar.gz: 2d9087aefbec7753e89145429350ac3de226ff561941b6685e19dc83a394611e583284b15c82a495d4ec5d9301b8dae467851feda4df820a622ba3696c23e8d4
6
+ metadata.gz: a666044f58c6ecc7b77ab56e75591c671697df76196041c6017aeb4d97cc262cd93b83760062a52aca1c960f27921c9e7ee9b558bbf9934596f7ef1bf595a573
7
+ data.tar.gz: f04cc077dec9d194a319a860b11caff5fe3d049a43c94bbcd8f317a6b38d74f421fe929b2da3f79835e8e5d81d9d63d9dc1873d45fb66ffdbea9fa178052112f
data/README.md CHANGED
@@ -3,11 +3,14 @@
3
3
 
4
4
  [![Build Status](https://travis-ci.org/RobertDober/lab42_core.svg?branch=master)](https://travis-ci.org/RobertDober/lab42_core)
5
5
  [![Code Climate](https://codeclimate.com/github/RobertDober/lab42_core/badges/gpa.svg)](https://codeclimate.com/github/RobertDober/lab42_core)
6
+ [![Issue Count](https://codeclimate.com/github/RobertDober/lab42_core/badges/issue_count.svg)](https://codeclimate.com/github/RobertDober/lab42_core)
6
7
  [![Test Coverage](https://codeclimate.com/github/RobertDober/lab42_core/badges/coverage.svg)](https://codeclimate.com/github/RobertDober/lab42_core)
7
8
  [![Gem Version](https://badge.fury.io/rb/lab42_core.svg)](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
@@ -1,11 +1,14 @@
1
1
 
2
- require_relative './core/old_ruby2' if RUBY_VERSION < '2.2'
3
-
4
- require_relative './core/kernel'
5
- require_relative './core/array'
6
- require_relative './core/dir'
7
- require_relative './core/enumerable'
8
- require_relative './core/file'
9
- require_relative './core/hash'
10
- require_relative './core/open_object'
11
- require_relative './core/memoization'
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
@@ -48,8 +48,6 @@ class << Dir
48
48
  last_param.()
49
49
  when :both
50
50
  [ frst_param, last_param.() ]
51
- else
52
- raise ArgumentError, "Lazy selection must be called with :first, :last or :both, but was with #{selector.inspect}"
53
51
  end
54
52
  end
55
53
  end
@@ -1,4 +1,13 @@
1
1
  module Enumerable
2
+
3
+ def find_with_value default_value=nil, &blk
4
+ each do | val |
5
+ val_prime = blk.(val)
6
+ return val_prime if val_prime
7
+ end
8
+ default_value
9
+ end
10
+
2
11
  def grep2 expr
3
12
  partition{ |ele| expr === ele }
4
13
  end
@@ -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,3 @@
1
+ require 'ostruct'
2
+ class Lab42::Core::StateMachine::Match < OpenStruct
3
+ 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
@@ -1,5 +1,5 @@
1
1
  module Lab42
2
2
  module Core
3
- VERSION = "0.5.0"
3
+ VERSION = "0.5.1"
4
4
  end # module Core
5
5
  end # module Lab42
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.0
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-02-22 00:00:00.000000000 Z
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.4.0
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.8
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)