kinda-core 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +1 -4
- data/Rakefile +20 -0
- data/lib/core.rb +3 -1
- data/lib/core/accessor.rb +20 -16
- data/lib/core/forwardable.rb +3 -4
- data/lib/core/functor.rb +2 -2
- data/lib/core/hash.rb +3 -3
- data/lib/core/module.rb +25 -13
- data/lib/core/object.rb +7 -3
- data/lib/core/proc.rb +3 -3
- data/lib/core/this.rb +1 -1
- metadata +5 -3
data/README.rdoc
CHANGED
@@ -1,6 +1,3 @@
|
|
1
|
-
Author:: Manuel Vila (mailto:mvila@3base.com)
|
2
|
-
License:: Don't know yet
|
3
|
-
|
4
1
|
= Core
|
5
2
|
|
6
3
|
Basic helpers used by other kinda projects.
|
@@ -11,4 +8,4 @@ Basic helpers used by other kinda projects.
|
|
11
8
|
|
12
9
|
== Usage
|
13
10
|
|
14
|
-
Still very alpha
|
11
|
+
Still very alpha, documentation will come later.
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require "rake/testtask"
|
2
|
+
|
3
|
+
begin
|
4
|
+
require "hanna/rdoctask"
|
5
|
+
rescue LoadError
|
6
|
+
require "rake/rdoctask"
|
7
|
+
end
|
8
|
+
|
9
|
+
task :default => [:test]
|
10
|
+
|
11
|
+
Rake::TestTask.new do |t|
|
12
|
+
t.test_files = Dir["test/*_test.rb"]
|
13
|
+
end
|
14
|
+
|
15
|
+
Rake::RDocTask.new do |t|
|
16
|
+
t.title = "Core Documentation"
|
17
|
+
t.main = "README.rdoc"
|
18
|
+
t.rdoc_files.include("README.rdoc", "lib/**/*.rb")
|
19
|
+
t.rdoc_dir = "doc"
|
20
|
+
end
|
data/lib/core.rb
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), 'core', 'module')
|
2
|
+
require File.join(File.dirname(__FILE__), 'core', 'object')
|
2
3
|
ruby_files = Dir.glob(File.join(File.dirname(__FILE__), 'core', '*.rb'))
|
3
4
|
ruby_files.each { |file| require file }
|
4
5
|
|
5
|
-
module Kinda
|
6
|
+
module Kinda #:nodoc:
|
7
|
+
# Basic helpers used by other kinda projects
|
6
8
|
module Core
|
7
9
|
include Accessor
|
8
10
|
include Forwardable
|
data/lib/core/accessor.rb
CHANGED
@@ -1,14 +1,8 @@
|
|
1
|
-
module Kinda
|
2
|
-
module Core
|
1
|
+
module Kinda #:nodoc:
|
2
|
+
module Core #:nodoc:
|
3
|
+
# Improves the Ruby buit-in accessor methods
|
3
4
|
module Accessor
|
4
|
-
|
5
|
-
# attr_reader(:name, :age, :size)
|
6
|
-
# attr_reader(:name => :@name) is the default behaviour but you can use:
|
7
|
-
# attr_reader (:name => @the_name) to use a different instance variable
|
8
|
-
# attr_reader(:name => :first_name) or even an another attribute.
|
9
|
-
# But more interesting, you can delegate to another object:
|
10
|
-
# attr_reader(:name => [:father, :name])
|
11
|
-
# attr_reader(:name => [:mother, :husband, :name])
|
5
|
+
ClassMethods = inheritable_extend do
|
12
6
|
def __attr_reader__(*attributes, with_filter, &block)
|
13
7
|
attributes.each do |attribute|
|
14
8
|
if attribute.is_a?(Hash)
|
@@ -17,7 +11,7 @@ module Kinda
|
|
17
11
|
source = "@#{attribute}"
|
18
12
|
end
|
19
13
|
proc = if with_filter || !block
|
20
|
-
|
14
|
+
Proc.new do |*args|
|
21
15
|
return send("#{attribute}=", *args) unless args.empty?
|
22
16
|
value = if source[0] == '@'
|
23
17
|
instance_variable_get(source)
|
@@ -31,10 +25,19 @@ module Kinda
|
|
31
25
|
else
|
32
26
|
block
|
33
27
|
end
|
34
|
-
|
28
|
+
send(:define_method, attribute, &proc)
|
35
29
|
end
|
36
30
|
end
|
31
|
+
|
32
|
+
private :__attr_reader__
|
37
33
|
|
34
|
+
# attr_reader(:name, :age, :size)
|
35
|
+
# attr_reader(:name => :@name) is the default behaviour but you can use:
|
36
|
+
# attr_reader (:name => @the_name) to use a different instance variable
|
37
|
+
# attr_reader(:name => :first_name) or even an another attribute.
|
38
|
+
# More interesting, you can delegate to another object:
|
39
|
+
# attr_reader(:name => [:father, :name])
|
40
|
+
# attr_reader(:name => [:mother, :husband, :name])
|
38
41
|
def attr_reader(*attributes, &block)
|
39
42
|
__attr_reader__(*attributes, false, &block)
|
40
43
|
end
|
@@ -51,7 +54,7 @@ module Kinda
|
|
51
54
|
target = "@#{attribute}"
|
52
55
|
end
|
53
56
|
proc = if with_filter || !block
|
54
|
-
|
57
|
+
Proc.new do |value|
|
55
58
|
value = instance_exec(value, &block) if block
|
56
59
|
if target[0] == '@'
|
57
60
|
instance_variable_set(target, value)
|
@@ -64,9 +67,11 @@ module Kinda
|
|
64
67
|
else
|
65
68
|
block
|
66
69
|
end
|
67
|
-
|
70
|
+
send(:define_method, "#{attribute}=", &proc)
|
68
71
|
end
|
69
72
|
end
|
73
|
+
|
74
|
+
private :__attr_writer__
|
70
75
|
|
71
76
|
def attr_writer(*attributes, &block)
|
72
77
|
__attr_writer__(*attributes, false, &block)
|
@@ -103,8 +108,7 @@ module Kinda
|
|
103
108
|
end
|
104
109
|
end
|
105
110
|
|
106
|
-
|
107
|
-
inheritable_extension ClassMethods
|
111
|
+
delegate_to_class ClassMethods.public_instance_methods
|
108
112
|
end
|
109
113
|
end
|
110
114
|
end
|
data/lib/core/forwardable.rb
CHANGED
@@ -3,7 +3,7 @@ require 'forwardable'
|
|
3
3
|
module Kinda
|
4
4
|
module Core
|
5
5
|
module Forwardable
|
6
|
-
|
6
|
+
ClassMethods = inheritable_extend do
|
7
7
|
include ::Forwardable
|
8
8
|
|
9
9
|
# Add a nice way to specify a different method for the accessor:
|
@@ -19,7 +19,7 @@ module Kinda
|
|
19
19
|
end
|
20
20
|
end
|
21
21
|
end
|
22
|
-
|
22
|
+
|
23
23
|
alias_method :delegate, :instance_delegate
|
24
24
|
|
25
25
|
# Create both reader and writer delegate
|
@@ -79,8 +79,7 @@ module Kinda
|
|
79
79
|
end
|
80
80
|
end
|
81
81
|
|
82
|
-
|
83
|
-
inheritable_extension ClassMethods
|
82
|
+
delegate_to_class ClassMethods.public_instance_methods
|
84
83
|
end
|
85
84
|
end
|
86
85
|
end
|
data/lib/core/functor.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
# Thomas Sawyer's Functor Class slightly modified for Ruby 1.9
|
2
|
-
# http://facets.rubyforge.org/apidoc/api/core/classes/Functor.html
|
3
1
|
module Kinda
|
4
2
|
module Core
|
3
|
+
# Thomas Sawyer's Functor Class slightly modified for Ruby 1.9
|
4
|
+
# http://facets.rubyforge.org/apidoc/api/core/classes/Functor.html
|
5
5
|
class Functor < BasicObject
|
6
6
|
def initialize(&proc)
|
7
7
|
@proc = proc
|
data/lib/core/hash.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module Kinda
|
2
2
|
module Core
|
3
|
-
module
|
3
|
+
module HashExtension
|
4
4
|
def delete_value(value)
|
5
5
|
delete_if { |k, v| v == value }
|
6
6
|
end
|
@@ -8,6 +8,6 @@ module Kinda
|
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
|
-
class Hash
|
12
|
-
include Kinda::Core::
|
11
|
+
class Hash #:nodoc:
|
12
|
+
include Kinda::Core::HashExtension
|
13
13
|
end
|
data/lib/core/module.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
module Kinda
|
2
2
|
module Core
|
3
|
-
module
|
3
|
+
module ModuleExtension
|
4
4
|
def self.included(klass)
|
5
|
-
klass.alias_method_chain :append_features, :
|
5
|
+
klass.alias_method_chain :append_features, :inheritable_extensions
|
6
6
|
end
|
7
7
|
|
8
8
|
def alias_method_chain(target, feature)
|
@@ -37,22 +37,34 @@ module Kinda
|
|
37
37
|
singleton_class.ancestors.take_while { |mod| !mod.is_a?(Class) }
|
38
38
|
end
|
39
39
|
|
40
|
-
def
|
41
|
-
|
40
|
+
def inheritable_extend(*mods, &block)
|
41
|
+
if block
|
42
|
+
mod = Module.new
|
43
|
+
mod.extend extensions
|
44
|
+
mod.module_eval(&block)
|
45
|
+
mods << mod
|
46
|
+
end
|
47
|
+
extend *mods
|
48
|
+
(@inheritable_extensions ||= []).unshift(*mods.reverse).uniq!
|
49
|
+
mod
|
42
50
|
end
|
43
51
|
|
44
|
-
def
|
45
|
-
|
52
|
+
def append_features_with_inheritable_extensions(target)
|
53
|
+
append_features_without_inheritable_extensions(target)
|
46
54
|
if @inheritable_extensions
|
47
55
|
target.extend *@inheritable_extensions
|
48
|
-
target.
|
56
|
+
target.inheritable_extend *@inheritable_extensions.reverse
|
49
57
|
end
|
50
58
|
end
|
51
59
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
60
|
+
# delegate_to_class :singleton_method_added => :method_added
|
61
|
+
# delegate_to_class [:attr_reader, :attr_writer]
|
62
|
+
# delegate_to_class ClassMethods.instance_methods
|
63
|
+
def delegate_to_class(*sources)
|
64
|
+
sources, target = sources.first.first if sources.first.is_a?(Hash)
|
65
|
+
[*sources].flatten.each do |source|
|
66
|
+
define_method(source) do |*args, &block|
|
67
|
+
singleton_class_send(target || source, *args, &block)
|
56
68
|
end
|
57
69
|
end
|
58
70
|
end
|
@@ -75,7 +87,7 @@ module Kinda
|
|
75
87
|
end
|
76
88
|
end
|
77
89
|
|
78
|
-
class Module
|
79
|
-
include Kinda::Core::
|
90
|
+
class Module #:nodoc:
|
91
|
+
include Kinda::Core::ModuleExtension
|
80
92
|
end
|
81
93
|
|
data/lib/core/object.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module Kinda
|
2
2
|
module Core
|
3
|
-
module
|
3
|
+
module ObjectExtension
|
4
4
|
def self.included(klass)
|
5
5
|
klass.alias_method_chain :extend, :flexible_arguments
|
6
6
|
end
|
@@ -17,6 +17,10 @@ module Kinda
|
|
17
17
|
singleton_class.class_eval(&block)
|
18
18
|
end
|
19
19
|
|
20
|
+
def singleton_class_send(method_name, *args, &block)
|
21
|
+
singleton_class_eval { send(method_name, *args, &block) }
|
22
|
+
end
|
23
|
+
|
20
24
|
def to_class
|
21
25
|
kind_of?(Module) ? self : singleton_class
|
22
26
|
end
|
@@ -58,6 +62,6 @@ module Kinda
|
|
58
62
|
end
|
59
63
|
end
|
60
64
|
|
61
|
-
class Object
|
62
|
-
include Kinda::Core::
|
65
|
+
class Object #:nodoc:
|
66
|
+
include Kinda::Core::ObjectExtension
|
63
67
|
end
|
data/lib/core/proc.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module Kinda
|
2
2
|
module Core
|
3
|
-
module
|
3
|
+
module ProcExtension
|
4
4
|
def call_with_this(this, *args, &block)
|
5
5
|
binding_self = binding.eval('self')
|
6
6
|
binding_self.push_this(this)
|
@@ -12,6 +12,6 @@ module Kinda
|
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
|
-
class Proc
|
16
|
-
include Kinda::Core::
|
15
|
+
class Proc #:nodoc:
|
16
|
+
include Kinda::Core::ProcExtension
|
17
17
|
end
|
data/lib/core/this.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kinda-core
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Manuel Vila
|
@@ -22,7 +22,9 @@ extensions: []
|
|
22
22
|
extra_rdoc_files:
|
23
23
|
- README.rdoc
|
24
24
|
files:
|
25
|
+
- Rakefile
|
25
26
|
- lib/core.rb
|
27
|
+
- lib/kinda-core.rb
|
26
28
|
- lib/core/accessor.rb
|
27
29
|
- lib/core/forwardable.rb
|
28
30
|
- lib/core/functor.rb
|
@@ -32,10 +34,10 @@ files:
|
|
32
34
|
- lib/core/object.rb
|
33
35
|
- lib/core/proc.rb
|
34
36
|
- lib/core/this.rb
|
35
|
-
- lib/kinda-core.rb
|
36
37
|
- README.rdoc
|
37
38
|
has_rdoc: true
|
38
39
|
homepage: http://github.com/kinda/core
|
40
|
+
licenses:
|
39
41
|
post_install_message:
|
40
42
|
rdoc_options: []
|
41
43
|
|
@@ -56,7 +58,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
56
58
|
requirements: []
|
57
59
|
|
58
60
|
rubyforge_project: kinda-core
|
59
|
-
rubygems_version: 1.
|
61
|
+
rubygems_version: 1.3.5
|
60
62
|
signing_key:
|
61
63
|
specification_version: 2
|
62
64
|
summary: Basic helpers used by other kinda projects
|