lab42_core 0.0.7 → 0.1.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: 6c1c6148c550240fac9cbd4b175be6f44986fd3e
4
- data.tar.gz: 630f25d0a92570a4c1c23c46f963520ac7e3f5b1
3
+ metadata.gz: 78544f6924efe2fc7b4f8e3db0503886225e4352
4
+ data.tar.gz: cb657a1d204799beca463d4ef393ad45ed4c7973
5
5
  SHA512:
6
- metadata.gz: c971f1782da35b2c340b275e0d195b99e7506ed9a85fa240110e847b1a9bcc63f4a081e97514ceb6a3f39e711ffb171447eb4f6d1baa3b94eaf9a411091ecf82
7
- data.tar.gz: 242a5a8b5c2f6866b0be0a62698b39a887309a71ce82781bba43e3001c2318ae2ac060408d8162b88d6e38353d8f321ce8a5a26ca01cf57e4acec6044c882908
6
+ metadata.gz: 1b53638447c31d3a6fbc64cb096a88b4b1d704d7967c0eabc908fdc693ee10dba232a6a96e4b1f21a9d7fdcc28bd10503badf7e68a61a0b107b296595d5e0a72
7
+ data.tar.gz: f93db262bbf0f249f27eaec041d47f7d806271166b569b24a111ec1fa4b85e4994ae8fabf7f878c1ea1833969c2b5987bdce5c7b40f77ec1d71cf7896887f60f
data/README.md CHANGED
@@ -1,76 +1,98 @@
1
1
  # lab42\_core
2
2
 
3
- Ruby Core Module Extensions (in the spirit of lab419/core)
3
+ Simple Ruby Core Module Extensions (for more see lab42\_more)
4
+
5
+ ## Array
6
+
7
+ Can be used after `require 'lab42/core'` or `require 'lab42/core/array'`
8
+
9
+ ### flatten\_once
10
+
11
+ ```ruby
12
+ [].flatten_once.assert.empty?
13
+
14
+ [[2, {a: 3}, [4]], {a: 5}].flatten_once.assert ==
15
+ [2, {a: 3}, [4], {a: 5}]
16
+ ```
17
+
18
+ For details see the corresponding [QED demo](https://github.com/RobertDober/lab42_core/blob/master/demo/array.md).
4
19
 
5
20
  ## Dir
6
21
 
22
+ Can be used after `require 'lab42/core'` or `require 'lab42/core/dir'`
23
+
7
24
  ```ruby
8
25
  Dir.files "**/*" do | partial_path, full_path |
9
26
  end
10
27
  ```
11
28
 
29
+ For details see the corresponding [QED demo](https://github.com/RobertDober/lab42_core/blob/master/demo/dir.md).
30
+
12
31
  ## Enumerable
13
32
 
33
+ ### grep2
34
+
14
35
  ```ruby
15
36
  enum.grep2 expr # ===>
16
37
  enum.partition{ |ele| expr === ele }
17
38
  ```
18
39
 
19
- ## Hash
40
+ ### to\_proc
41
+
42
+ And also `Enumerable#to\_proc` as e.g.
20
43
 
21
44
  ```ruby
22
- {a: 42, b: 43}.only :a, :c # ===> {a: 42}
45
+ counter = (1..3).to_proc
46
+ counter.().assert == 1
47
+ counter.().assert == 2
48
+ counter.().assert == 3
49
+ StopIteration.assert.raised? do
50
+ counter.()
51
+ end
23
52
  ```
24
53
 
25
- ## Fn
26
-
27
- Must be required specifically!
28
54
 
29
- ### Access to methods
55
+ For details see the corresponding [QED demo](https://github.com/RobertDober/lab42_core/blob/master/demo/enumerable.md).
30
56
 
31
- ```ruby
32
- require 'lab42/core/fn'
57
+ ## File
33
58
 
34
- # Get method of an object
35
- printer = Kernel.fn.puts
36
- printer.( 42 )
37
- 2.times(&printer)
59
+ `expand\_local\_path` to get rid of the `__FILE__` inside `expand\_path`.
38
60
 
39
- # It also extends some enumerable methods for easier execution
40
- %w{hello world}.each printer
41
- ```
61
+ For details see the corresponding [QED demo](https://github.com/RobertDober/lab42_core/blob/master/demo/file.md).
42
62
 
43
- Thusly `fn` gives us access to methods of objects, but what about instance methods?
44
-
45
- ### Access to instance methods
46
-
47
- Is realized via `fm`.
63
+ ## Hash
48
64
 
49
65
  ```ruby
50
- require 'lab42/core/fn'
51
-
52
- (1..100).reduce Fixnum.fm.+ # ---> 5050
66
+ {a: 42, b: 43}.only :a, :c # ===> {a: 42}
53
67
  ```
54
68
 
55
- ### Objects as constant procs
69
+ For details see the corresponding [QED demo](https://github.com/RobertDober/lab42_core/blob/master/demo/hash.md).
70
+
71
+ ## Fn
56
72
 
57
- While `Object#self` will return the obvious, so will by extension `object.fn.self`.
73
+ Has been moved into gem [lab42\_more](https://github.com/RobertDober/lab42_more)
58
74
 
59
- This can be very useful, e.g. in this nice way to create an empty array ;)
75
+ ## OpenObject
76
+
77
+ Immutable Open Objects
60
78
 
61
79
  ```ruby
62
- (1..100).filter(false.fn.self)
80
+ x = OpenObject.new a: 42
81
+ x.a.assert == 42
82
+ x[:a].assert == 42
63
83
  ```
64
84
 
65
- If you hesitate to use this all, have a look into Kernel#const_lambda
85
+ Immutability
86
+
66
87
 
67
- ### Partial Application
88
+ All _modifications_ just return a new instance.
68
89
 
69
90
  ```ruby
70
- f = Array.fm.push :next
71
- [[],[1]].map( f ) # ---> [[:next], [1, :next]]
91
+ x = OpenObject.new a: 42, b: 43
72
92
 
73
- a=[]
74
- f = a.fn.push :first
75
- f.(1) # a ---> [:first, 1]
93
+ y = x.update a: 44
94
+ y.to_hash.assert == {a: 44, b: 43}
95
+ x.a.assert == 42
76
96
  ```
97
+
98
+ For details see the corresponding [QED demo](https://github.com/RobertDober/lab42_core/blob/master/demo/open_object.md).
@@ -0,0 +1,12 @@
1
+ class Array
2
+ def flatten_once
3
+ inject [] do | r, a |
4
+ case a
5
+ when Array
6
+ [*r] + [*a]
7
+ else
8
+ [*r] + [a]
9
+ end
10
+ end
11
+ end
12
+ end # class Array
@@ -1,13 +1,24 @@
1
1
  class << Dir
2
2
  def files glob_para, &blk
3
- glob( glob_para ).map do |f|
4
- full = File.join pwd, f
5
- next if File.directory? full
6
- [f, full]
7
- end
8
- .compact
9
- .tap do | result |
10
- result.each(&blk) if blk
3
+ __files__ glob_para, &blk
4
+ end
5
+
6
+ private
7
+
8
+ def __files__ glob_para, &blk
9
+ here = pwd
10
+ if blk
11
+ glob( glob_para ).each do | f |
12
+ next if File.directory? f
13
+ blk.( [ f, File.expand_path(File.join( here, f )) ] )
11
14
  end
15
+ else
16
+ Enumerator.new do |y|
17
+ glob( glob_para ).each do | f |
18
+ next if File.directory? f
19
+ y.yield [ f, File.expand_path(File.join( here, f )) ]
20
+ end
21
+ end
22
+ end
12
23
  end
13
24
  end
@@ -2,4 +2,37 @@ module Enumerable
2
2
  def grep2 expr
3
3
  partition{ |ele| expr === ele }
4
4
  end
5
+
6
+
7
+ def to_proc
8
+ this = self
9
+ yielder = to_enum.lazy
10
+ -> do
11
+ yielder.next
12
+ end
13
+ .extend( Module.new do
14
+ define_method :reset! do
15
+ yielder = this.to_enum.lazy
16
+ end
17
+ end)
18
+ end
19
+ end
20
+
21
+ class Enumerator::Lazy
22
+ def grep2 expr
23
+ [ select{ |x| expr === x }, reject{ |x| expr === x } ]
24
+ end
25
+
26
+ def to_proc
27
+ this = self
28
+ yielder = to_enum.lazy
29
+ -> do
30
+ yielder.next
31
+ end
32
+ .extend( Module.new do
33
+ define_method :reset! do
34
+ yielder = this.to_enum.lazy
35
+ end
36
+ end)
37
+ end
5
38
  end
@@ -0,0 +1,8 @@
1
+ class << File
2
+ def expand_local_path *args, &blk
3
+ raise ArgumentError, 'need a block to determine source location' unless blk
4
+ values = args + Array( blk.() )
5
+ expand_path File.join( '..', values.compact), blk.source_location.first
6
+
7
+ end
8
+ end # class << File
@@ -0,0 +1,52 @@
1
+ require 'forwarder'
2
+ # require 'lab42/core/open_object/transformations'
3
+
4
+ class OpenObject
5
+ include Enumerable
6
+
7
+ # include Lab42::Core::OpenObject::Transformations
8
+
9
+ extend Forwarder
10
+ forward_all :[], :keys, :length, :size, :values, to: :@data
11
+
12
+ def == other
13
+ self.class === other && to_hash == other.to_hash
14
+ end
15
+ def each &blk
16
+ @data.each do | k, v |
17
+ blk.( self.class.new k => v )
18
+ end
19
+ end
20
+
21
+ def to_hash
22
+ @data.clone
23
+ end
24
+ def update **params
25
+ new_params = @data.merge params
26
+ self.class.new( **new_params )
27
+ end
28
+ alias_method :merge, :update
29
+
30
+
31
+ private
32
+ def initialize( **params )
33
+ @data = params
34
+ params.each do |k, v|
35
+ class << self; self end.module_eval do
36
+ define_method(k){v}
37
+ end
38
+ end
39
+ end
40
+
41
+ class << self
42
+ def inherited *args, **kwds, &blk
43
+ raise RuntimeError, "I prefer delegation to inheritance, if you do not, MP me"
44
+ end
45
+ end
46
+ end # class OpenObject
47
+
48
+ class Hash
49
+ def to_open_object
50
+ OpenObject.new **self
51
+ end
52
+ end
@@ -1,5 +1,5 @@
1
1
  module Lab42
2
2
  module Core
3
- VERSION = "0.0.7"
3
+ VERSION = "0.1.0"
4
4
  end # module Core
5
5
  end # module Lab42
data/lib/lab42/core.rb CHANGED
@@ -1,6 +1,6 @@
1
- require 'lab42/core/dir'
2
- require 'lab42/core/enumerable'
3
- require 'lab42/core/hash'
4
- require 'lab42/core/kernel'
5
- require 'lab42/core/object'
6
- require 'lab42/core/module'
1
+ require_relative './core/array'
2
+ require_relative './core/dir'
3
+ require_relative './core/enumerable'
4
+ require_relative './core/file'
5
+ require_relative './core/hash'
6
+ require_relative './core/open_object'
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.0.7
4
+ version: 0.1.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-02-15 00:00:00.000000000 Z
11
+ date: 2014-09-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry
@@ -16,29 +16,86 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.9.12
19
+ version: '0.10'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 0.9.12
26
+ version: '0.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry-nav
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.2'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.2'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rspec
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
45
  - - "~>"
32
46
  - !ruby/object:Gem::Version
33
- version: 2.13.0
47
+ version: '3.1'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: qed
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.9'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.9'
69
+ - !ruby/object:Gem::Dependency
70
+ name: ae
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.8'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.8'
83
+ - !ruby/object:Gem::Dependency
84
+ name: lab42_tmux2
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.0'
34
90
  type: :development
35
91
  prerelease: false
36
92
  version_requirements: !ruby/object:Gem::Requirement
37
93
  requirements:
38
94
  - - "~>"
39
95
  - !ruby/object:Gem::Version
40
- version: 2.13.0
41
- description: Hash, Dir and more extensions
96
+ version: '0.0'
97
+ description: Extending Array, Hash, File, Enumerable with convenience methods, conceptual
98
+ changes have been moved into lab42_more
42
99
  email: robert.dober@gmail.com
43
100
  executables: []
44
101
  extensions: []
@@ -47,18 +104,12 @@ files:
47
104
  - LICENSE
48
105
  - README.md
49
106
  - lib/lab42/core.rb
107
+ - lib/lab42/core/array.rb
50
108
  - lib/lab42/core/dir.rb
51
109
  - lib/lab42/core/enumerable.rb
52
- - lib/lab42/core/fn.rb
53
- - lib/lab42/core/fn/array.rb
54
- - lib/lab42/core/fn/basic_object.rb
55
- - lib/lab42/core/fn/enumerable.rb
56
- - lib/lab42/core/fn/enumerator/lazy.rb
57
- - lib/lab42/core/fn/iterator_reimpl.rb
110
+ - lib/lab42/core/file.rb
58
111
  - lib/lab42/core/hash.rb
59
- - lib/lab42/core/kernel.rb
60
- - lib/lab42/core/module.rb
61
- - lib/lab42/core/object.rb
112
+ - lib/lab42/core/open_object.rb
62
113
  - lib/lab42/core/version.rb
63
114
  homepage: https://github.com/RobertDober/lab42_core
64
115
  licenses:
@@ -80,8 +131,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
80
131
  version: '0'
81
132
  requirements: []
82
133
  rubyforge_project:
83
- rubygems_version: 2.2.0
134
+ rubygems_version: 2.2.2
84
135
  signing_key:
85
136
  specification_version: 4
86
- summary: What I am missing in Ruby
137
+ summary: Simple Ruby Core Module Extensions (for more see lab42_more)
87
138
  test_files: []
@@ -1,5 +0,0 @@
1
- require 'lab42/core/fn/iterator_reimpl'
2
-
3
- class Array
4
- include Lab42::Core::IteratorReimpl
5
- end
@@ -1,35 +0,0 @@
1
- module Lab42
2
- module Core
3
- class Fn < BasicObject
4
- def self;
5
- ->(*args,&blk){ @source } # This is *not* a recursive call
6
- end
7
- private
8
- def initialize source
9
- @source = source
10
- end
11
-
12
- def method_missing *args, &blk
13
- return @source.method args.first if args.size == 1 && blk.nil?
14
- s = @source
15
- -> *a, &b do
16
- s.method( args.first ).(*(args[1..-1]+a), &(blk||b))
17
- end
18
- end
19
- end # class Fn
20
-
21
- class Fm < BasicObject
22
- private
23
- def initialize source
24
- @source = source
25
- end
26
-
27
- def method_missing *args, &blk
28
- m = @source.instance_method args.first
29
- -> receiver, *a, &b do
30
- m.bind( receiver ).(*(args[1..-1]+a), &(blk||b))
31
- end
32
- end
33
- end # class Fn
34
- end # module Core
35
- end # module Lab42
@@ -1,5 +0,0 @@
1
- require 'lab42/core/fn/iterator_reimpl'
2
-
3
- module Enumerable
4
- include Lab42::Core::IteratorReimpl
5
- end
@@ -1,7 +0,0 @@
1
- require 'lab42/core/fn/iterator_reimpl'
2
-
3
- class Enumerator
4
- class Lazy
5
- include Lab42::Core::IteratorReimpl
6
- end # class Lazy
7
- end # class Enumerator
@@ -1,47 +0,0 @@
1
- module Lab42
2
- module Core
3
- module IteratorReimpl
4
-
5
-
6
- def self.decompose_args args, block
7
- raise ArgumentError, 'too many arguments' if args.size + ( block ? 1 : 0 ) > 2
8
- return [args,block] if block
9
- b, a = args.partition{|x| behavior? x}
10
- behave = b.pop
11
- a = b if a.empty?
12
- raise ArgumentError, "No behavior specified" unless behave
13
- return [a,behave]
14
- end
15
-
16
- def self.included into
17
- _self = self
18
- into.module_eval do
19
- alias_method :__lab42_core_iterator_map__, :map
20
- def map behavior=nil, &blk
21
- __lab42_core_iterator_map__(&(behavior||blk))
22
- end
23
- alias_method :__lab42_core_iterator_select__, :select
24
- def select behavior=nil, &blk
25
- __lab42_core_iterator_select__(&(behavior||blk))
26
- end
27
- alias_method :filter, :select
28
-
29
- alias_method :__lab42_core_iterator_inject__, :inject
30
- define_method :inject do |*args, &blk|
31
- args, behavior = _self.decompose_args args, blk
32
- __lab42_core_iterator_inject__( *args, &behavior )
33
- end
34
- alias_method :reduce, :inject
35
-
36
- def filter *args, &blk
37
- end
38
- end
39
-
40
- private
41
- def self.behavior? x
42
- Symbol === x || Proc === x || Method === x
43
- end
44
- end
45
- end
46
- end # module Core
47
- end # module Lab42
data/lib/lab42/core/fn.rb DELETED
@@ -1,21 +0,0 @@
1
- require 'lab42/core/fn/basic_object'
2
- require 'lab42/core/fn/array'
3
- require 'lab42/core/fn/enumerable'
4
- require 'lab42/core/fn/enumerator/lazy'
5
-
6
- class Object
7
- def fn
8
- # There can only be one (per instance)
9
- @__lab42_core_fn__ ||= Lab42::Core::Fn.new self
10
- # But caching in frozen objects is not an option
11
- rescue
12
- Lab42::Core::Fn.new self
13
- end
14
-
15
- def fm
16
- @__lab42_core_fm__ ||= Lab42::Core::Fm.new self
17
- # But caching in frozen objects is not an option
18
- rescue
19
- Lab42::Core::Fn.new self
20
- end
21
- end # class Object
@@ -1,12 +0,0 @@
1
- module Kernel
2
-
3
- def const_lambda a_const
4
- -> *a, &b do
5
- a_const
6
- end
7
- end
8
-
9
- def sendmsg *args
10
- ->(*a){ a.first.send(*(args + a.drop( 1 ))) }
11
- end
12
- end # module Kernel
@@ -1,37 +0,0 @@
1
- class ::Module
2
-
3
- def memoize name, by: ->(n){n}
4
- im = instance_method name
5
- cache = {}
6
- mcache = {}
7
- # Replace method by the memoized version
8
- #
9
- remove_method name
10
- define_method name do |*args, &blk|
11
- cache[object_id] ||= {}
12
- mcache[object_id] ||= im.bind self
13
- key = by.(*args,&blk)
14
-
15
- return cache[object_id][key] if cache[object_id].has_key? key
16
- rval = mcache[object_id].(*args, &blk)
17
- cache[object_id][key] = rval
18
- end
19
- wm = instance_method name
20
- # Create a one time wrapper for cache initialization
21
- remove_method name
22
- wrapper = ->(*args,&blk) do
23
- cache[object_id] = {}
24
- class << self; self end.module_eval do
25
- define_method name, wm
26
- end
27
- result = wm.bind(self).(*args, &blk)
28
- # reinstall the wrapper
29
- class << self; self end.module_eval do
30
- define_method( name, &wrapper )
31
- end
32
- result
33
- end
34
- define_method( name, &wrapper )
35
- end
36
-
37
- end
@@ -1,3 +0,0 @@
1
- class Object
2
- def self; self end
3
- end