lab42_core 0.3.2 → 0.3.3

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: 9f7dde044401dfbe2f779d84728f458dd3c49370
4
- data.tar.gz: 14507f78a4d194afb9b243f83b0bf1913dce1e3f
3
+ metadata.gz: 5cd12ff6e489be963c104371e4e2af457aba6074
4
+ data.tar.gz: d0c1dd7b78302cc8326c092d05331d0fdb9d6498
5
5
  SHA512:
6
- metadata.gz: 015ae1a78dc4bd449030db2be0bb75535038688f3785d5e1ae33ed7893d795263219ea097f30e5e0e15a269f17fd639e1f327ab70a430ddfc5d0d6dbda1a8f56
7
- data.tar.gz: e2bb5e9166067edd5ea90009f4568788650ca1ce46a60515ea79b5e4c870fd2e10bb08229df1ef75e7a307e74e9e6d209e16e4edcb900c4cac361bb1c4b15332
6
+ metadata.gz: cc889bb8bb613314b00811dce275fb55516f40a862339ec96353a20d0ff75d26329b44c1996c70fc8fae895ee644687c79971d11e790fa81c2e4d24d689a4ae8
7
+ data.tar.gz: 3188d4c7607973f5b4c2d5d4fba1d00c86548249ba1134af74555b922e69fc6cfa802ec932ba5323a9970bfad0e73adc7eb7d5c26624ea54e5518604ae4bfab3
data/README.md CHANGED
@@ -10,7 +10,9 @@ Simple Ruby Core Module Extensions (for more see lab42\_more)
10
10
 
11
11
  ## Programming Paradigms
12
12
 
13
- ### Fn - Functional Access To Methods
13
+ ### Functional
14
+
15
+ #### Fn/Fm - Functional Access To Methods
14
16
 
15
17
  Can be used after `require 'lab42/core/fn'` **only**.
16
18
 
@@ -18,7 +20,7 @@ Might be moved into gem [lab42\_more](https://github.com/RobertDober/lab42_more)
18
20
 
19
21
  API will remain the same, require will change to `require 'lab42_more/fn'`
20
22
 
21
- ### fn like function
23
+ ##### fn like function
22
24
 
23
25
  ```ruby
24
26
  Dir.files [APP_ROOT, 'spec', 'support', '**', '*.rb'], Kernel.fn.require
@@ -26,7 +28,7 @@ API will remain the same, require will change to `require 'lab42_more/fn'`
26
28
  Dir.files( %w{.. assets ** *.txt} ).sort_by &File.fn.mtime
27
29
  ```
28
30
 
29
- #### fm like function/method
31
+ ##### fm like function/method
30
32
 
31
33
  ```ruby
32
34
  %w{ alpha beta gamma delta }.sort_by &String.fm.size
@@ -38,6 +40,20 @@ upon call, once it has been transformed by `#to_proc`
38
40
 
39
41
  For details see the corresponding [QED demo](https://github.com/RobertDober/lab42_core/blob/master/demo/fn.md).
40
42
 
43
+ ## Behave
44
+
45
+ We exposed methods as _functions_ in the previous chapter. However we are subject to sematic confusion
46
+ with terms like _methods_, _procedures_, _lambdas_ and _functions_.
47
+
48
+ Is it not time to abstract a little bit?
49
+
50
+ Enter **Behave**, which is everything that behaves, right?
51
+
52
+ Let us see how that behaves.
53
+
54
+
55
+ For details see the corresponding [QED demo](https://github.com/RobertDober/lab42_core/blob/master/demo/behave.md).
56
+
41
57
  #### Memoization and Lazy Attributes
42
58
 
43
59
  ##### Memoization
@@ -0,0 +1,5 @@
1
+ require_relative 'behavior'
2
+
3
+ def B *a, &b
4
+ Lab42::Meta::Behavior *a, &b
5
+ end
File without changes
@@ -2,8 +2,6 @@
2
2
 
3
3
  require_relative '../core'
4
4
  require_relative 'fn'
5
+ require_relative 'b'
5
6
 
6
- def B *a, &b
7
- Lab42::Meta::Behavior *a, &b
8
- end
9
7
  Dir.glob File.expand_local_path{ %w{console_tools ** *.rb} }, &Kernel.fn.require
@@ -1,4 +1,6 @@
1
+ require_relative 'meta'
1
2
  class Hash
3
+
2
4
  def only *args
3
5
  args.inject Hash.new do | r, k |
4
6
  if has_key? k
@@ -9,6 +11,16 @@ class Hash
9
11
  end
10
12
  end
11
13
 
14
+ def reject_values *behavior, &beh
15
+ beh = Lab42::Meta::Behavior *behavior, &beh
16
+ reject{ |_, v| beh.(v) }
17
+ end
18
+
19
+ def select_values *behavior, &beh
20
+ beh = Lab42::Meta::Behavior *behavior, &beh
21
+ select{ |_, v| beh.(v) }
22
+ end
23
+
12
24
  def with_present key, default: nil
13
25
  if has_key? key
14
26
  yield fetch(key), self
@@ -17,6 +29,16 @@ class Hash
17
29
  end
18
30
  end
19
31
 
32
+ def without *keys
33
+ inject Hash.new do | r, (k,v) |
34
+ if keys.include? k
35
+ r
36
+ else
37
+ r.merge k => v
38
+ end
39
+ end
40
+ end
41
+
20
42
  def fetch! key, *defaults, &defblk
21
43
  default_present = !(defaults.empty? && defblk.nil?)
22
44
  return fetch key unless default_present
@@ -1,6 +1,18 @@
1
1
  require_relative 'hash'
2
+
3
+ module Lab42::Unmemoizer
4
+ def unmemoize_memo method_name, *args
5
+ ivar_name = "@__#{method_name}__"
6
+ return unless instance_variable_defined? ivar_name
7
+ return instance_variable_set ivar_name, {} if args.empty?
8
+ instance_variable_get( ivar_name ).delete args
9
+ end
10
+ end
11
+
2
12
  class Module
3
13
 
14
+ include Lab42::Unmemoizer
15
+
4
16
  def lazy_attr sym, &blk
5
17
  raise ArgumentError, 'missing initialization block' unless blk
6
18
  define_method sym do
@@ -11,6 +23,7 @@ class Module
11
23
  end
12
24
 
13
25
  def memoize sym
26
+ include Lab42::Unmemoizer
14
27
  orig_method = instance_method sym
15
28
  define_method sym do |*args|
16
29
  ivar_name = "@__#{sym}__"
@@ -1,5 +1,5 @@
1
1
  module Lab42
2
2
  module Core
3
- VERSION = "0.3.2"
3
+ VERSION = "0.3.3"
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.3.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Dober
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-30 00:00:00.000000000 Z
11
+ date: 2015-05-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: forwarder2
@@ -119,6 +119,8 @@ files:
119
119
  - README.md
120
120
  - lib/lab42/core.rb
121
121
  - lib/lab42/core/array.rb
122
+ - lib/lab42/core/b.rb
123
+ - lib/lab42/core/behave.rb
122
124
  - lib/lab42/core/behavior.rb
123
125
  - lib/lab42/core/behavior/proxy.rb
124
126
  - lib/lab42/core/console_tools.rb