cldwalker-core 0.0.0 → 0.1.0
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.
- data/README.markdown +4 -78
- data/Rakefile +48 -40
- data/TODO.txt +7 -0
- data/lib/core.rb +1 -67
- data/lib/core/array.rb +1 -4
- data/lib/core/io.rb +0 -13
- metadata +6 -8
- data/VERSION.yml +0 -4
- data/lib/core/irb.rb +0 -29
data/README.markdown
CHANGED
|
@@ -1,80 +1,6 @@
|
|
|
1
1
|
Description
|
|
2
|
-
|
|
2
|
+
==========
|
|
3
3
|
|
|
4
|
-
My extensions to core
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
So if you're not feeling shy, monkeypatch away:
|
|
8
|
-
|
|
9
|
-
irb>> require 'core'
|
|
10
|
-
true
|
|
11
|
-
irb>> Core.adds_to Array
|
|
12
|
-
Array
|
|
13
|
-
irb>> Core.ancestors
|
|
14
|
-
=> [Array, Core::Array, Enumerable, Object, PP::ObjectMixin, Kernel]
|
|
15
|
-
|
|
16
|
-
And if you're not feeling your monkey-fu:
|
|
17
|
-
|
|
18
|
-
irb>> class Base; class Array < Array; end; end
|
|
19
|
-
=> nil
|
|
20
|
-
irb>> Core.add_to Base::Array, :with=>Core::Array
|
|
21
|
-
=>Base::Array
|
|
22
|
-
irb>> Base::Array.ancestors
|
|
23
|
-
=>[Base::Array,Array, Core::Array, Enumerable, Object, PP::ObjectMixin, Kernel]
|
|
24
|
-
|
|
25
|
-
So what happens when it's four o'clock in the morning and you monkeypatch the wrong way?:
|
|
26
|
-
|
|
27
|
-
irb>> module Core::Array; def is_a?(*args); puts "dunno, i'm sleepy!"; end; end
|
|
28
|
-
nil
|
|
29
|
-
irb>> Core.adds_to Array
|
|
30
|
-
Couldn't include Core::Array into Array because the following methods conflict:
|
|
31
|
-
map
|
|
32
|
-
|
|
33
|
-
Phew, that was a close one. But wait, I really do think I know what I'm doing:
|
|
34
|
-
|
|
35
|
-
irb>> Core.adds_to Array, :force=>true
|
|
36
|
-
Array
|
|
37
|
-
irb>> [1,2].is_a?(Array)
|
|
38
|
-
dunno, i'm sleepy!
|
|
39
|
-
=>nil
|
|
40
|
-
|
|
41
|
-
Hopefully you'll use the force option more wisely.
|
|
42
|
-
|
|
43
|
-
Your Core
|
|
44
|
-
=========
|
|
45
|
-
If you'd like to wrap your own core extensions in say the original namespace MyCore:
|
|
46
|
-
|
|
47
|
-
MyCore.send :include, Core::Loader
|
|
48
|
-
|
|
49
|
-
You'll then be able to extend classes as in the examples above, replacing Core with MyCore.
|
|
50
|
-
To take advantage of the auto-requiring done by Core::Loader, place your extensions
|
|
51
|
-
in a directory mycore/ and make sure your $LOAD\_PATH contains the directory mycore.
|
|
52
|
-
In other words, `require 'mycore/array'` should be valid.
|
|
53
|
-
|
|
54
|
-
To wrap up your methods for MyCore, see my extensions or use this template:
|
|
55
|
-
|
|
56
|
-
#in mycore/array.rb
|
|
57
|
-
module MyCore
|
|
58
|
-
#extensions for Array's
|
|
59
|
-
module Array
|
|
60
|
-
def blah
|
|
61
|
-
end
|
|
62
|
-
#....
|
|
63
|
-
end
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
Limitations
|
|
68
|
-
===========
|
|
69
|
-
|
|
70
|
-
Checks for method name collisions currently use *instance\_methods and *methods of a class.
|
|
71
|
-
Patches for more thorough checks are welcome.
|
|
72
|
-
|
|
73
|
-
Todo
|
|
74
|
-
====
|
|
75
|
-
|
|
76
|
-
* Support extending class methods of the extended class.
|
|
77
|
-
* Import/Upgrade my old tests for my extension classes.
|
|
78
|
-
* Would be nice to:
|
|
79
|
-
** Provide aliasing for methods to avoid method name clashes.
|
|
80
|
-
** Make it easier to share/install core extensions made by others.
|
|
4
|
+
My extensions to core ruby classes, similar to the facets gem.
|
|
5
|
+
Some of these are probably only useful shortcuts to common things I do
|
|
6
|
+
with core ruby classes.
|
data/Rakefile
CHANGED
|
@@ -1,49 +1,57 @@
|
|
|
1
|
-
require '
|
|
2
|
-
require 'rake/
|
|
3
|
-
require '
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
1
|
+
require 'rubygems'
|
|
2
|
+
require 'rake/gempackagetask'
|
|
3
|
+
require 'rubygems/specification'
|
|
4
|
+
require 'date'
|
|
5
|
+
require 'spec/rake/spectask'
|
|
6
|
+
|
|
7
|
+
GEM = "core"
|
|
8
|
+
GEM_VERSION = "0.1.0"
|
|
9
|
+
AUTHOR = "Gabriel Horner"
|
|
10
|
+
EMAIL = "gabriel.horner@gmail.com"
|
|
11
|
+
HOMEPAGE = "http://github.com/cldwalker/core"
|
|
12
|
+
SUMMARY = "My extensions to core ruby classes, similar to the facets gem."
|
|
13
|
+
|
|
14
|
+
spec = Gem::Specification.new do |s|
|
|
15
|
+
s.name = GEM
|
|
16
|
+
s.version = GEM_VERSION
|
|
17
|
+
s.platform = Gem::Platform::RUBY
|
|
18
|
+
s.has_rdoc = true
|
|
19
|
+
s.extra_rdoc_files = ["README.markdown", "LICENSE.txt", 'TODO.txt']
|
|
20
|
+
s.summary = SUMMARY
|
|
21
|
+
s.description = s.summary
|
|
22
|
+
s.author = AUTHOR
|
|
23
|
+
s.email = EMAIL
|
|
24
|
+
s.homepage = HOMEPAGE
|
|
25
|
+
|
|
26
|
+
# Uncomment this to add a dependency
|
|
27
|
+
# s.add_dependency "foo"
|
|
28
|
+
|
|
29
|
+
s.require_path = 'lib'
|
|
30
|
+
s.autorequire = GEM
|
|
31
|
+
s.files = %w(LICENSE.txt README.markdown Rakefile TODO.txt) + Dir.glob("{lib}/**/*")
|
|
15
32
|
end
|
|
16
33
|
|
|
17
|
-
|
|
18
|
-
require 'jeweler'
|
|
19
|
-
Jeweler::Tasks.new do |s|
|
|
20
|
-
s.name = "core"
|
|
21
|
-
s.description = "My extensions to core ruby classes, similar to the facets gem."
|
|
22
|
-
s.summary = s.description
|
|
23
|
-
s.email = "gabriel.horner@gmail.com"
|
|
24
|
-
s.homepage = "http://github.com/cldwalker/core"
|
|
25
|
-
s.authors = ["Gabriel Horner"]
|
|
26
|
-
s.files = FileList["VERSION.yml", "Rakefile", "README.markdown", "LICENSE.txt", "{bin,lib}/**/*"]
|
|
27
|
-
s.has_rdoc = true
|
|
28
|
-
s.extra_rdoc_files = ["README.markdown", "LICENSE.txt", "TODO.txt"]
|
|
29
|
-
end
|
|
34
|
+
task :default => :spec
|
|
30
35
|
|
|
31
|
-
|
|
32
|
-
|
|
36
|
+
desc "Run specs"
|
|
37
|
+
Spec::Rake::SpecTask.new do |t|
|
|
38
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
|
39
|
+
t.spec_opts = %w(-fs --color)
|
|
33
40
|
end
|
|
34
41
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
t.verbose = false
|
|
42
|
+
|
|
43
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
|
44
|
+
pkg.gem_spec = spec
|
|
39
45
|
end
|
|
40
46
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
rdoc.options << '--line-numbers' << '--inline-source'
|
|
45
|
-
rdoc.rdoc_files.include('README*')
|
|
46
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
|
47
|
+
desc "install the gem locally"
|
|
48
|
+
task :install => [:package] do
|
|
49
|
+
sh %{sudo gem install pkg/#{GEM}-#{GEM_VERSION}}
|
|
47
50
|
end
|
|
48
51
|
|
|
49
|
-
|
|
52
|
+
desc "create a gemspec file"
|
|
53
|
+
task :make_spec do
|
|
54
|
+
File.open("#{GEM}.gemspec", "w") do |file|
|
|
55
|
+
file.puts spec.to_ruby
|
|
56
|
+
end
|
|
57
|
+
end
|
data/TODO.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
* Import/Upgrade my old tests.
|
|
2
|
+
* Move all core extensions to modules so that they can be optionally extended by an object
|
|
3
|
+
or included by a non-core class.
|
|
4
|
+
* Would be nice to:
|
|
5
|
+
** Provide aliasing for methods to avoid method name clashes.
|
|
6
|
+
** Provide a sake-like way for easily sharing/installing your methods with others.
|
|
7
|
+
** Extend this for non-core classes ie some Rails classes.
|
data/lib/core.rb
CHANGED
|
@@ -1,69 +1,3 @@
|
|
|
1
|
-
$:.unshift(File.dirname(__FILE__)) unless
|
|
2
|
-
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
|
3
1
|
module Core
|
|
4
|
-
|
|
5
|
-
def self.included(base)
|
|
6
|
-
base.extend(self)
|
|
7
|
-
end
|
|
8
|
-
|
|
9
|
-
def adds_to(klass, options = {})
|
|
10
|
-
unless (extension_klass = options[:with] || get_extension_class(klass))
|
|
11
|
-
puts "No #{self} extension class found"
|
|
12
|
-
return false
|
|
13
|
-
end
|
|
14
|
-
conflicts = check_for_conflicts(klass, extension_klass)
|
|
15
|
-
if conflicts.empty? || options[:force]
|
|
16
|
-
klass.send :include, extension_klass
|
|
17
|
-
else
|
|
18
|
-
puts "Couldn't include #{extension_klass} into #{klass} because the following methods conflict:"
|
|
19
|
-
puts conflicts.sort.join(", ")
|
|
20
|
-
false
|
|
21
|
-
end
|
|
22
|
-
end
|
|
23
|
-
alias_method :add_to, :adds_to
|
|
24
|
-
|
|
25
|
-
def detect_extension_class(klass)
|
|
26
|
-
extension_klass = self.const_get(klass.to_s) rescue nil
|
|
27
|
-
extension_klass = nil if extension_klass == klass
|
|
28
|
-
extension_klass
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
def get_extension_class(klass)
|
|
32
|
-
unless (extension_klass = detect_extension_class(klass))
|
|
33
|
-
#try again but first by requiring possible file
|
|
34
|
-
begin; require("#{self.to_s.gsub('::','/').downcase}/#{klass.to_s.downcase}"); rescue(LoadError); end
|
|
35
|
-
extension_klass = detect_extension_class(klass)
|
|
36
|
-
end
|
|
37
|
-
extension_klass
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
def check_for_conflicts(klass, extension_klass)
|
|
41
|
-
if false #TODO: extension_klass.to_s =~ /ClassMethods/
|
|
42
|
-
else
|
|
43
|
-
all_instance_methods(klass) & all_instance_methods(extension_klass)
|
|
44
|
-
end
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
def all_instance_methods(klass)
|
|
48
|
-
klass.public_instance_methods + klass.private_instance_methods + klass.protected_instance_methods
|
|
49
|
-
end
|
|
50
|
-
end
|
|
2
|
+
VERSION = "0.1.0"
|
|
51
3
|
end
|
|
52
|
-
|
|
53
|
-
Core.send :include, Core::Loader
|
|
54
|
-
|
|
55
|
-
__END__
|
|
56
|
-
|
|
57
|
-
#extend Array with Core::Array's ClassMethods + InstanceMethods
|
|
58
|
-
Core.adds_to Array
|
|
59
|
-
|
|
60
|
-
#extend MyArray from file
|
|
61
|
-
class My::Array
|
|
62
|
-
Core.adds_to self
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
#from console
|
|
66
|
-
Core.adds_to My::Array
|
|
67
|
-
|
|
68
|
-
#extend Array only with ClassMethods
|
|
69
|
-
Core.adds_to Array, :from=>Core::Array::ClassMethods
|
data/lib/core/array.rb
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
module Array
|
|
1
|
+
class Array
|
|
3
2
|
# Allows you to specify ranges of elements and individual elements with one string, array starts at 1
|
|
4
3
|
# Example: choose first and fourth through eighth elements: '1,4-8'
|
|
5
4
|
def multislice(range,splitter=',',offset=nil)
|
|
6
|
-
#td: fix swallowing of empty lines
|
|
7
5
|
result = []
|
|
8
6
|
for r in range.split(splitter)
|
|
9
7
|
if r =~ /-/
|
|
@@ -102,4 +100,3 @@ module Core
|
|
|
102
100
|
! include_any?(arr)
|
|
103
101
|
end
|
|
104
102
|
end
|
|
105
|
-
end
|
data/lib/core/io.rb
CHANGED
|
@@ -5,17 +5,4 @@ class IO
|
|
|
5
5
|
i = f.index(string) || 100000
|
|
6
6
|
f.slice(0,i)
|
|
7
7
|
end
|
|
8
|
-
|
|
9
|
-
#from output_catcher gem
|
|
10
|
-
def self.capture_stdout(&block)
|
|
11
|
-
original_stdout = $stdout
|
|
12
|
-
$stdout = fake = StringIO.new
|
|
13
|
-
begin
|
|
14
|
-
yield
|
|
15
|
-
ensure
|
|
16
|
-
$stdout = original_stdout
|
|
17
|
-
end
|
|
18
|
-
fake.string
|
|
19
|
-
end
|
|
20
|
-
|
|
21
8
|
end
|
metadata
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: cldwalker-core
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Gabriel Horner
|
|
8
|
-
autorequire:
|
|
8
|
+
autorequire: core
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
11
|
|
|
12
|
-
date:
|
|
12
|
+
date: 2008-12-10 00:00:00 -08:00
|
|
13
13
|
default_executable:
|
|
14
14
|
dependencies: []
|
|
15
15
|
|
|
@@ -24,10 +24,10 @@ extra_rdoc_files:
|
|
|
24
24
|
- LICENSE.txt
|
|
25
25
|
- TODO.txt
|
|
26
26
|
files:
|
|
27
|
-
- VERSION.yml
|
|
28
|
-
- Rakefile
|
|
29
|
-
- README.markdown
|
|
30
27
|
- LICENSE.txt
|
|
28
|
+
- README.markdown
|
|
29
|
+
- Rakefile
|
|
30
|
+
- TODO.txt
|
|
31
31
|
- lib/core
|
|
32
32
|
- lib/core/array.rb
|
|
33
33
|
- lib/core/class.rb
|
|
@@ -35,11 +35,9 @@ files:
|
|
|
35
35
|
- lib/core/file.rb
|
|
36
36
|
- lib/core/hash.rb
|
|
37
37
|
- lib/core/io.rb
|
|
38
|
-
- lib/core/irb.rb
|
|
39
38
|
- lib/core/object.rb
|
|
40
39
|
- lib/core/string.rb
|
|
41
40
|
- lib/core.rb
|
|
42
|
-
- TODO.txt
|
|
43
41
|
has_rdoc: true
|
|
44
42
|
homepage: http://github.com/cldwalker/core
|
|
45
43
|
post_install_message:
|
data/VERSION.yml
DELETED
data/lib/core/irb.rb
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
#from http://errtheblog.com/posts/9-drop-to-irb
|
|
2
|
-
# call with IRB.start_session(Kernel.binding) in script
|
|
3
|
-
require 'irb'
|
|
4
|
-
|
|
5
|
-
module IRB
|
|
6
|
-
def self.start_session(binding)
|
|
7
|
-
IRB.setup(nil)
|
|
8
|
-
|
|
9
|
-
workspace = WorkSpace.new(binding)
|
|
10
|
-
|
|
11
|
-
if @CONF[:SCRIPT]
|
|
12
|
-
irb = Irb.new(workspace, @CONF[:SCRIPT])
|
|
13
|
-
else
|
|
14
|
-
irb = Irb.new(workspace)
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
@CONF[:IRB_RC].call(irb.context) if @CONF[:IRB_RC]
|
|
18
|
-
@CONF[:MAIN_CONTEXT] = irb.context
|
|
19
|
-
|
|
20
|
-
trap("SIGINT") do
|
|
21
|
-
irb.signal_handle
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
catch(:IRB_EXIT) do
|
|
25
|
-
irb.eval_input
|
|
26
|
-
end
|
|
27
|
-
end
|
|
28
|
-
end
|
|
29
|
-
|