lab42_core 0.3.2 → 0.3.3
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 +19 -3
- data/lib/lab42/core/b.rb +5 -0
- data/lib/lab42/core/behave.rb +0 -0
- data/lib/lab42/core/console_tools.rb +1 -3
- data/lib/lab42/core/hash.rb +22 -0
- data/lib/lab42/core/memoization.rb +13 -0
- data/lib/lab42/core/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5cd12ff6e489be963c104371e4e2af457aba6074
|
4
|
+
data.tar.gz: d0c1dd7b78302cc8326c092d05331d0fdb9d6498
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
###
|
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
|
-
|
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
|
-
|
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
|
data/lib/lab42/core/b.rb
ADDED
File without changes
|
data/lib/lab42/core/hash.rb
CHANGED
@@ -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}__"
|
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.3.
|
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-
|
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
|