shared 0.4.2 → 0.4.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.
Files changed (4) hide show
  1. data/README +35 -6
  2. data/ann-shared-0.4.2.txt +183 -0
  3. data/lib/shared.rb +5 -1
  4. metadata +2 -1
data/README CHANGED
@@ -12,6 +12,11 @@ URIS
12
12
  http://codeforpeople.com/lib/ruby/
13
13
 
14
14
  HISTORY
15
+ 0.4.3
16
+ - added version info
17
+ - move methods from Object to Kernel and made them private (thx Stefan
18
+ Rusterholz)
19
+
15
20
  0.4.2
16
21
  initial version
17
22
 
@@ -44,8 +49,12 @@ SAMPLES
44
49
 
45
50
  ~ > ruby samples/a.rb
46
51
 
47
- "Foo"
48
- "Bar"
52
+ samples/a.rb:4:in `require': ./lib/shared.rb:2: no .<digit> floating literal anymore; put 0 before dot (SyntaxError)
53
+ Shared::VERSION = 0.4.3 unless defined?(Shared::VERSION)
54
+ ^
55
+ ./lib/shared.rb:2: syntax error, unexpected tINTEGER
56
+ Shared::VERSION = 0.4.3 unless defined?(Shared::VERSION)
57
+ ^ from samples/a.rb:4
49
58
 
50
59
 
51
60
  <========< samples/b.rb >========>
@@ -73,7 +82,12 @@ SAMPLES
73
82
 
74
83
  ~ > ruby samples/b.rb
75
84
 
76
- 42
85
+ samples/b.rb:5:in `require': ./lib/shared.rb:2: no .<digit> floating literal anymore; put 0 before dot (SyntaxError)
86
+ Shared::VERSION = 0.4.3 unless defined?(Shared::VERSION)
87
+ ^
88
+ ./lib/shared.rb:2: syntax error, unexpected tINTEGER
89
+ Shared::VERSION = 0.4.3 unless defined?(Shared::VERSION)
90
+ ^ from samples/b.rb:5
77
91
 
78
92
 
79
93
  <========< samples/c.rb >========>
@@ -108,7 +122,12 @@ SAMPLES
108
122
 
109
123
  ~ > ruby samples/c.rb
110
124
 
111
- "a".."c"
125
+ samples/c.rb:5:in `require': ./lib/shared.rb:2: no .<digit> floating literal anymore; put 0 before dot (SyntaxError)
126
+ Shared::VERSION = 0.4.3 unless defined?(Shared::VERSION)
127
+ ^
128
+ ./lib/shared.rb:2: syntax error, unexpected tINTEGER
129
+ Shared::VERSION = 0.4.3 unless defined?(Shared::VERSION)
130
+ ^ from samples/c.rb:5
112
131
 
113
132
 
114
133
  <========< samples/d.rb >========>
@@ -139,7 +158,12 @@ SAMPLES
139
158
 
140
159
  ~ > ruby samples/d.rb
141
160
 
142
- "<blink> zaphod </blink>"
161
+ samples/d.rb:5:in `require': ./lib/shared.rb:2: no .<digit> floating literal anymore; put 0 before dot (SyntaxError)
162
+ Shared::VERSION = 0.4.3 unless defined?(Shared::VERSION)
163
+ ^
164
+ ./lib/shared.rb:2: syntax error, unexpected tINTEGER
165
+ Shared::VERSION = 0.4.3 unless defined?(Shared::VERSION)
166
+ ^ from samples/d.rb:5
143
167
 
144
168
 
145
169
  <========< samples/e.rb >========>
@@ -179,5 +203,10 @@ SAMPLES
179
203
 
180
204
  ~ > ruby samples/e.rb
181
205
 
182
- 42
206
+ samples/e.rb:5:in `require': ./lib/shared.rb:2: no .<digit> floating literal anymore; put 0 before dot (SyntaxError)
207
+ Shared::VERSION = 0.4.3 unless defined?(Shared::VERSION)
208
+ ^
209
+ ./lib/shared.rb:2: syntax error, unexpected tINTEGER
210
+ Shared::VERSION = 0.4.3 unless defined?(Shared::VERSION)
211
+ ^ from samples/e.rb:5
183
212
 
@@ -0,0 +1,183 @@
1
+ NAME
2
+ shared.rb
3
+
4
+ DESCRIPTION
5
+ super simple super power sharing of instance and class level code
6
+
7
+ INSTALL
8
+ gem install shared
9
+
10
+ URIS
11
+ http://rubyforge.org/projects/codeforpeople
12
+ http://codeforpeople.com/lib/ruby/
13
+
14
+ HISTORY
15
+ 0.4.2
16
+ initial version
17
+
18
+ SAMPLES
19
+
20
+ <========< samples/a.rb >========>
21
+
22
+ ~ > cat samples/a.rb
23
+
24
+ # shared.rb is a very simple and very powerful method of sharing code between
25
+ # classes.
26
+ #
27
+ require 'shared'
28
+
29
+ shared(:code) do
30
+ def classname() self.class.name end
31
+ end
32
+
33
+ class Foo
34
+ include shared(:code)
35
+ end
36
+
37
+ class Bar
38
+ include shared(:code)
39
+ end
40
+
41
+ p Foo.new.classname #=> "Foo"
42
+
43
+ p Bar.new.classname #=> "Bar"
44
+
45
+ ~ > ruby samples/a.rb
46
+
47
+ "Foo"
48
+ "Bar"
49
+
50
+
51
+ <========< samples/b.rb >========>
52
+
53
+ ~ > cat samples/b.rb
54
+
55
+ # shared.rb allows natural declaration of both instance and class-level
56
+ # methods - it's more than simple module inclusion
57
+ #
58
+
59
+ require 'shared'
60
+
61
+ shared('methods') do
62
+ def self.class_method() 40 end
63
+
64
+ def instance_method() 2 end
65
+ end
66
+
67
+ class C
68
+ include shared('methods')
69
+ end
70
+
71
+ p(C.class_method + C.new.instance_method) #=> 42
72
+
73
+
74
+ ~ > ruby samples/b.rb
75
+
76
+ 42
77
+
78
+
79
+ <========< samples/c.rb >========>
80
+
81
+ ~ > cat samples/c.rb
82
+
83
+ # shared.rb works equally well with individual objects in addition to the
84
+ # normal class level usage
85
+ #
86
+
87
+ require 'shared'
88
+
89
+ shared(:meta_tools) do
90
+ def singleton_class &block
91
+ singleton_class =
92
+ class << self
93
+ self
94
+ end
95
+ block ? singleton_class.module_eval(&block) : singleton_class
96
+ end
97
+ end
98
+
99
+ a = %w( a b c )
100
+
101
+ a.extend shared(:meta_tools)
102
+
103
+ a.singleton_class do
104
+ def to_range() first .. last end
105
+ end
106
+
107
+ p a.to_range #=> "a".."c"
108
+
109
+ ~ > ruby samples/c.rb
110
+
111
+ "a".."c"
112
+
113
+
114
+ <========< samples/d.rb >========>
115
+
116
+ ~ > cat samples/d.rb
117
+
118
+ # an example use-case for shared.rb is sharing code bewteen classes that
119
+ # require both class level and instance level behaviour to be shared
120
+ #
121
+
122
+ require 'shared'
123
+
124
+ shared(:acts_as_named) do
125
+ validates_precence_of :name
126
+
127
+ def to_html() "<blink> #{ name } </blink>" end
128
+ end
129
+
130
+ class Model
131
+ def Model.validates_precence_of(*a) end
132
+
133
+ def name() 'zaphod' end
134
+
135
+ include shared(:acts_as_named)
136
+ end
137
+
138
+ p Model.new.to_html #=> "<blink> zaphod </blink>"
139
+
140
+ ~ > ruby samples/d.rb
141
+
142
+ "<blink> zaphod </blink>"
143
+
144
+
145
+ <========< samples/e.rb >========>
146
+
147
+ ~ > cat samples/e.rb
148
+
149
+ # it's important to note that the shared code is injected into the reciever
150
+ # fresh on each call and, in that way, the effect is rather macro like
151
+ #
152
+
153
+ require 'shared'
154
+
155
+ share(:instance_tracker) do
156
+ const_set :Instances, []
157
+
158
+ def self.instances() const_get :Instances end
159
+
160
+ def self.new *a, &b
161
+ obj = super
162
+ ensure
163
+ instances << obj
164
+ end
165
+ end
166
+
167
+ class Foo
168
+ include shared(:instance_tracker)
169
+ end
170
+
171
+ class Bar
172
+ include shared(:instance_tracker)
173
+ end
174
+
175
+ 2.times{ Foo.new }
176
+ 40.times{ Bar.new }
177
+
178
+ p(Foo.instances.size + Bar.instances.size)
179
+
180
+ ~ > ruby samples/e.rb
181
+
182
+ 42
183
+
@@ -1,4 +1,7 @@
1
1
  module Shared
2
+ Shared::VERSION = 0.4.3 unless defined?(Shared::VERSION)
3
+ def version() Shared::VERSION end
4
+
2
5
  Code = {}
3
6
 
4
7
  def shared name, &block
@@ -48,7 +51,8 @@ module Shared
48
51
  extend self
49
52
  end
50
53
 
51
- class Object
54
+ module Kernel
55
+ private
52
56
  def share *a, &b
53
57
  Shared.share *a, &b
54
58
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shared
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ara T. Howard
@@ -31,6 +31,7 @@ extensions: []
31
31
  extra_rdoc_files: []
32
32
 
33
33
  files:
34
+ - ann-shared-0.4.2.txt
34
35
  - gemspec.rb
35
36
  - gen_readme.rb
36
37
  - install.rb