lab42_core 0.1.2 → 0.2.0

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: cf5aec7a9918cd936728fb06721e663d530c901e
4
- data.tar.gz: 08d0c079fd94050faea45f17e8d155d1d788e3b9
3
+ metadata.gz: 16682f25db174dc49c05db95880ad4dbdffb08a6
4
+ data.tar.gz: 9303958cffdf2f045054a1f3860de6f509213b43
5
5
  SHA512:
6
- metadata.gz: 8ceabf66233750d990fed83eb335f7ac27b3646e01be7d45612df31b1b057cb219353eeb0f70d371c8161c291329467f086a1cfecf239eecddccb58282718f2b
7
- data.tar.gz: 3d1596f2c2323bc904f80db7c249cc8e8b079ae6839ff9748c8f3b6879e12dc17a5e7a532bc4f90324c96dee7200299a6200a695ddd248383c19566fdb6bdec6
6
+ metadata.gz: 4cdfb75fd68d03b62a023b6064c5e63d52ea69392e514df65eaaaa8a2ee2f90a4c18479b7db795013ec9c5d0b7d83c6cefdeab117561e2cf89f2105b5bdf6fe7
7
+ data.tar.gz: b814d0a775141a045f84b810d7cc37d8cf5159c39e73eb42886a7b74cf6aaf9adcff8b8816302d791ca0b794071ac8b13feebc2dd6cc488c3ed5653de1d8c82a
data/README.md CHANGED
@@ -35,6 +35,13 @@ Can be used after `require 'lab42/core'` or `require 'lab42/core/dir'`
35
35
  end
36
36
  ```
37
37
 
38
+ If only the relative or absolute pathes are needed there are the two variations avaiable:
39
+
40
+ ```ruby
41
+ Dir.abs_files ...
42
+ Dir.rel_files ...
43
+ ```
44
+
38
45
  For details see the corresponding [QED demo](https://github.com/RobertDober/lab42_core/blob/master/demo/dir.md).
39
46
 
40
47
  ## Enumerable
@@ -92,7 +99,35 @@ For details see the corresponding [QED demo](https://github.com/RobertDober/lab4
92
99
 
93
100
  ## Fn
94
101
 
95
- Has been moved into gem [lab42\_more](https://github.com/RobertDober/lab42_more)
102
+ Can be used after `require 'lab42/core/array'` **only**.
103
+
104
+ Might be moved into gem [lab42\_more](https://github.com/RobertDober/lab42_more) in the future .
105
+
106
+ API will remain the same, require will change to `require 'lab42_more/fn'`
107
+
108
+ ### fn like function
109
+
110
+ ```ruby
111
+ Dir.files [APP_ROOT, 'spec', 'support', '**', '*.rb'], Kernel.fn.require
112
+
113
+ Dir.files( %w{.. assets ** *.txt} ).sort_by &File.fn.mtime
114
+ ```
115
+
116
+ ### fm like function/method
117
+
118
+ ```ruby
119
+ %w{ alpha beta gamma delta }.sort_by &String.fm.size
120
+ ```
121
+
122
+ **N.B.** This only works because the object behind the scenes of `Class#fm` knows how to bind
123
+ upon call, once it has been transformed by `#to_proc`
124
+
125
+
126
+ For details see the corresponding [QED demo](https://github.com/RobertDober/lab42_core/blob/master/demo/fn.md).
127
+
128
+ ## Object
129
+
130
+ Backport of `#itself` for versions < 2.2
96
131
 
97
132
  ## OpenObject
98
133
 
@@ -0,0 +1,24 @@
1
+ require_relative '../behavior'
2
+ require_relative '../unbound_behavior'
3
+ module Lab42
4
+ class Behavior
5
+ # Hey man really, that's all??? All my responsibility is to intercept
6
+ # missing methods and sending them off to that Lab42::Behavior bloke,
7
+ # having him have all the fun!!!! You cannott be serious by any known
8
+ # defintion of that term, or, can you .....
9
+ class Proxy
10
+ attr_reader :receiver, :fm
11
+ private
12
+ def initialize receiver, fm: false
13
+ @receiver = receiver
14
+ @fm = fm
15
+ end
16
+
17
+ def method_missing *args, &blk
18
+ fm ?
19
+ Lab42::UnboundBehavior.new( receiver, receiver.instance_method( args.first ), *args.drop(1), &blk ) :
20
+ Lab42::Behavior.new( receiver, receiver.method( args.first ), *args.drop(1), &blk )
21
+ end
22
+ end # class Proxy
23
+ end # class Behavior
24
+ end # module Lab42
@@ -0,0 +1,38 @@
1
+ require 'forwarder'
2
+
3
+ module Lab42
4
+ # Thank youn my dear Proxy, for sending me all the information
5
+ # I, the clever guy, who is having all the fun, will make best
6
+ # use of it by being an extremly, yes I said *extremly* clever
7
+ # callable object representing the methods or instance methods
8
+ # represented by the calls to `Object#fn` or `Module#fm`.
9
+ # Did I tell you yet? It is sooooo out not be functional...
10
+ class Behavior
11
+ attr_reader :args, :block, :method, :receiver
12
+
13
+
14
+ def call *a, &b
15
+ method
16
+ .( *(a + args), &(b||block) )
17
+ end
18
+ alias_method :[], :call
19
+
20
+ def to_proc
21
+ -> *a, &b do
22
+ method
23
+ .( *(a + args), &(b||block) )
24
+ end
25
+ end
26
+
27
+
28
+ private
29
+ def initialize receiver, method, *args, &block
30
+ @receiver = receiver
31
+ @method = method
32
+ @args = args
33
+ @block = block
34
+ end
35
+
36
+
37
+ end # class Behavior
38
+ end # module Lab42
@@ -1,24 +1,55 @@
1
+ require_relative 'meta'
1
2
  class << Dir
2
- def files glob_para, &blk
3
- __files__ glob_para, &blk
3
+ def abs_files glob_para, *rst, &blk
4
+ glob_para = File.join( *glob_para ) if Array === glob_para
5
+ blk = Lab42::Meta::Behavior *rst, &blk
6
+
7
+ __files__ :last, glob_para, &blk
8
+ end
9
+
10
+ def files glob_para, *rst, &blk
11
+ glob_para = File.join( *glob_para ) if Array === glob_para
12
+ blk = Lab42::Meta::Behavior *rst, &blk
13
+
14
+ __files__ :both, glob_para, &blk
15
+ end
16
+
17
+ def rel_files glob_para, *rst, &blk
18
+ glob_para = File.join( *glob_para ) if Array === glob_para
19
+ blk = Lab42::Meta::Behavior *rst, &blk
20
+
21
+ __files__ :first, glob_para, &blk
4
22
  end
5
23
 
6
24
  private
7
25
 
8
- def __files__ glob_para, &blk
26
+ def __files__ selector, glob_para, &blk
9
27
  here = pwd
10
28
  if blk
11
- glob( glob_para ).each do | f |
29
+ glob( glob_para ).map do | f |
12
30
  next if File.directory? f
13
- blk.( [ f, File.expand_path(File.join( here, f )) ] )
14
- end
31
+ blk.( __select__( selector, f ){ File.expand_path File.join( here, f ) } )
32
+ end.compact
15
33
  else
16
34
  Enumerator.new do |y|
17
35
  glob( glob_para ).each do | f |
18
36
  next if File.directory? f
19
- y.yield [ f, File.expand_path(File.join( here, f )) ]
37
+ y.yield __select__( selector, f ){ File.expand_path File.join( here, f ) }
20
38
  end
21
39
  end
22
40
  end
23
41
  end
42
+
43
+ def __select__ selector, frst_param, &last_param
44
+ case selector
45
+ when :first
46
+ frst_param
47
+ when :last
48
+ last_param.()
49
+ when :both
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
+ end
54
+ end
24
55
  end
@@ -0,0 +1,13 @@
1
+ require_relative 'behavior/proxy'
2
+
3
+ class Object
4
+ def fn
5
+ Lab42::Behavior::Proxy.new self, fm: false
6
+ end
7
+ end # class Object
8
+
9
+ class Module
10
+ def fm
11
+ Lab42::Behavior::Proxy.new self, fm: true
12
+ end
13
+ end
@@ -0,0 +1,27 @@
1
+ module Lab42
2
+ module Meta extend self
3
+ def Behavior *args, &blk
4
+ return blk if blk
5
+ determine_behavior_from args
6
+ end
7
+
8
+ private
9
+ def determine_behavior_from args
10
+ return make_callable args.last if args.last && args.last.respond_to?( :call )
11
+ return make_message_sender args if Symbol === args.first
12
+ end
13
+
14
+ def make_callable callable
15
+ return callable if Proc === callable
16
+ -> *a, &b do
17
+ callable.(*a, &b)
18
+ end
19
+ end
20
+
21
+ def make_message_sender args
22
+ -> (rcv, *late_args) do
23
+ rcv.send( args.first, *( late_args + args.drop(1) ) )
24
+ end
25
+ end
26
+ end # module Meta
27
+ end # module Lab42
@@ -0,0 +1,3 @@
1
+ class Object
2
+ def itself; self end
3
+ end
@@ -0,0 +1,44 @@
1
+ require_relative 'behavior'
2
+
3
+ module Lab42
4
+ # Thank youn my dear Proxy, for sending me all the information
5
+ # I, the clever guy, who is having all the fun, will make best
6
+ # use of it by being an extremly, yes I said *extremly* clever
7
+ # callable object representing the methods or instance methods
8
+ # represented by the calls to `Object#fn` or `Module#fm`.
9
+ # Did I tell you yet? It is sooooo out not be functional...
10
+ class UnboundBehavior
11
+ attr_reader :args, :block, :method
12
+
13
+ def call *a, &b
14
+ available_args = a + args
15
+ m = method.bind available_args.first
16
+ m.( *available_args.drop(1), &(b||block) )
17
+ end
18
+
19
+ def to_proc
20
+ -> *a, &b do
21
+ available_args = a + args
22
+ method.bind( available_args.first )
23
+ .( *available_args.drop(1), &(b||block) )
24
+ end
25
+ end
26
+
27
+ # Rebinding:
28
+ # We still apply our LIFO policy! So much for the proverbial
29
+ # "The early bird catches the worm"? Anyway, what would I do
30
+ # with a worm??? (Mescal maybe?)
31
+ def _ *a, &b
32
+ self.class.new @klass, method, *(a + args), &(b||block)
33
+ end
34
+
35
+ private
36
+ def initialize klass, method, *args, &block
37
+ @klass = klass
38
+ @method = method
39
+ @args = args
40
+ @block = block
41
+ end
42
+
43
+ end # class Behavior
44
+ end # module Lab42
@@ -1,5 +1,5 @@
1
1
  module Lab42
2
2
  module Core
3
- VERSION = "0.1.2"
3
+ VERSION = "0.2.0"
4
4
  end # module Core
5
5
  end # module Lab42
data/lib/lab42/core.rb CHANGED
@@ -1,3 +1,6 @@
1
+
2
+ require_relative './core/old_ruby2' if RUBY_VERSION < '2.2'
3
+
1
4
  require_relative './core/array'
2
5
  require_relative './core/dir'
3
6
  require_relative './core/enumerable'
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.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Dober
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-30 00:00:00.000000000 Z
11
+ date: 2015-04-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: forwarder2
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '3.1'
61
+ version: '3.2'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '3.1'
68
+ version: '3.2'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: qed
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -119,11 +119,17 @@ files:
119
119
  - README.md
120
120
  - lib/lab42/core.rb
121
121
  - lib/lab42/core/array.rb
122
+ - lib/lab42/core/behavior.rb
123
+ - lib/lab42/core/behavior/proxy.rb
122
124
  - lib/lab42/core/dir.rb
123
125
  - lib/lab42/core/enumerable.rb
124
126
  - lib/lab42/core/file.rb
127
+ - lib/lab42/core/fn.rb
125
128
  - lib/lab42/core/hash.rb
129
+ - lib/lab42/core/meta.rb
130
+ - lib/lab42/core/old_ruby2.rb
126
131
  - lib/lab42/core/open_object.rb
132
+ - lib/lab42/core/unbound_behavior.rb
127
133
  - lib/lab42/core/version.rb
128
134
  - lib/lab42/file.rb
129
135
  - lib/lab42/lazy.rb
@@ -147,7 +153,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
147
153
  version: '0'
148
154
  requirements: []
149
155
  rubyforge_project:
150
- rubygems_version: 2.4.3
156
+ rubygems_version: 2.4.6
151
157
  signing_key:
152
158
  specification_version: 4
153
159
  summary: Simple Ruby Core Module Extensions (for more see lab42_more)