shared 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -2,195 +2,36 @@ NAME
2
2
  shared.rb
3
3
 
4
4
  DESCRIPTION
5
- super simple super power sharing of instance and class level code
5
+ shared.rb provides a super easy way to share code between classes or modules
6
+ in a simple way. shared code can be at the class and/or instance level and
7
+ users deferred evaluation so this is more powerful that the normal ruby
8
+ module inclusion facilities on which it is based.
6
9
 
7
- INSTALL
8
- gem install shared
10
+ SYNOPSIS
11
+ require 'shared'
9
12
 
10
- URIS
11
- http://rubyforge.org/projects/codeforpeople
12
- http://codeforpeople.com/lib/ruby/
13
+ Shared 'methods' do
14
+ class << self
15
+ attr :classname
16
+ end
13
17
 
14
- HISTORY
15
- 1.0.0
16
- - add Share/Shared methods
17
- - move from @@vars to closure based impl
18
+ @classname = name.downcase
18
19
 
19
- 0.4.5
20
- - add Shared.for as 'shared' alias to rails' style const missing can
21
- trigger lib load for a call. usage: Shared.for(:name){ ... }
22
-
23
- 0.4.3
24
- - added version info
25
- - move methods from Object to Kernel and made them private (thx Stefan
26
- Rusterholz)
20
+ def objectname
21
+ self.class.classname + "(#{ object_id })"
22
+ end
23
+ end
27
24
 
28
- 0.4.2
29
- initial version
25
+ class C
26
+ include Shared('methods')
27
+ end
30
28
 
31
- SAMPLES
29
+ class B
30
+ include Shared('methods')
31
+ end
32
32
 
33
- <========< samples/a.rb >========>
34
-
35
- ~ > cat samples/a.rb
36
-
37
- # shared.rb is a very simple and very powerful method of sharing code between
38
- # classes.
39
- #
40
- require 'shared'
41
-
42
- shared(:code) do
43
- def classname() self.class.name end
44
- end
45
-
46
- class Foo
47
- include shared(:code)
48
- end
49
-
50
- class Bar
51
- include shared(:code)
52
- end
53
-
54
- p Foo.new.classname #=> "Foo"
55
-
56
- p Bar.new.classname #=> "Bar"
57
-
58
- ~ > ruby samples/a.rb
59
-
60
- "Foo"
61
- "Bar"
62
-
63
-
64
- <========< samples/b.rb >========>
65
-
66
- ~ > cat samples/b.rb
67
-
68
- # shared.rb allows natural declaration of both instance and class-level
69
- # methods - it's more than simple module inclusion
70
- #
71
-
72
- require 'shared'
73
-
74
- shared('methods') do
75
- def self.class_method() 40 end
76
-
77
- def instance_method() 2 end
78
- end
79
-
80
- class C
81
- include shared('methods')
82
- end
83
-
84
- p(C.class_method + C.new.instance_method) #=> 42
85
-
86
-
87
- ~ > ruby samples/b.rb
88
-
89
- 42
90
-
91
-
92
- <========< samples/c.rb >========>
93
-
94
- ~ > cat samples/c.rb
95
-
96
- # shared.rb works equally well with individual objects in addition to the
97
- # normal class level usage
98
- #
99
-
100
- require 'shared'
101
-
102
- shared(:meta_tools) do
103
- def singleton_class &block
104
- singleton_class =
105
- class << self
106
- self
107
- end
108
- block ? singleton_class.module_eval(&block) : singleton_class
109
- end
110
- end
111
-
112
- a = %w( a b c )
113
-
114
- a.extend shared(:meta_tools)
115
-
116
- a.singleton_class do
117
- def to_range() first .. last end
118
- end
119
-
120
- p a.to_range #=> "a".."c"
121
-
122
- ~ > ruby samples/c.rb
123
-
124
- "a".."c"
125
-
126
-
127
- <========< samples/d.rb >========>
128
-
129
- ~ > cat samples/d.rb
130
-
131
- # an example use-case for shared.rb is sharing code bewteen classes that
132
- # require both class level and instance level behaviour to be shared
133
- #
134
-
135
- require 'shared'
136
-
137
- shared(:acts_as_named) do
138
- validates_precence_of :name
139
-
140
- def to_html() "<blink> #{ name } </blink>" end
141
- end
142
-
143
- class Model
144
- def Model.validates_precence_of(*a) end
145
-
146
- def name() 'zaphod' end
147
-
148
- include shared(:acts_as_named)
149
- end
150
-
151
- p Model.new.to_html #=> "<blink> zaphod </blink>"
152
-
153
- ~ > ruby samples/d.rb
154
-
155
- "<blink> zaphod </blink>"
156
-
157
-
158
- <========< samples/e.rb >========>
159
-
160
- ~ > cat samples/e.rb
161
-
162
- # it's important to note that the shared code is injected into the reciever
163
- # fresh on each call and, in that way, the effect is rather macro like
164
- #
165
-
166
- require 'shared'
167
-
168
- share(:instance_tracker) do
169
- const_set :Instances, []
170
-
171
- def self.instances() const_get :Instances end
172
-
173
- def self.new *a, &b
174
- obj = super
175
- ensure
176
- instances << obj
177
- end
178
- end
179
-
180
- class Foo
181
- include shared(:instance_tracker)
182
- end
183
-
184
- class Bar
185
- include shared(:instance_tracker)
186
- end
187
-
188
- 2.times{ Foo.new }
189
- 40.times{ Bar.new }
190
-
191
- p(Foo.instances.size + Bar.instances.size)
192
-
193
- ~ > ruby samples/e.rb
194
-
195
- 42
33
+ p C.classname #=> 'c'
34
+ p C.new.objectname #=> 'c(1234)'
196
35
 
36
+ p B.classname #=> 'b'
37
+ p B.new.objectname #=> 'b(4567)'
@@ -0,0 +1,228 @@
1
+
2
+ This.rubyforge_project = 'codeforpeople'
3
+ This.author = "Ara T. Howard"
4
+ This.email = "ara.t.howard@gmail.com"
5
+ This.homepage = "http://github.com/ahoward/#{ This.lib }/tree/master"
6
+
7
+
8
+ task :default do
9
+ puts(Rake::Task.tasks.map{|task| task.name} - ['default'])
10
+ end
11
+
12
+
13
+ task :gemspec do
14
+ ignore_extensions = 'git', 'svn', 'tmp', /sw./, 'bak', 'gem'
15
+ ignore_directories = 'pkg'
16
+ ignore_files = 'test/log'
17
+
18
+ shiteless =
19
+ lambda do |list|
20
+ list.delete_if do |entry|
21
+ next unless test(?e, entry)
22
+ extension = File.basename(entry).split(%r/[.]/).last
23
+ ignore_extensions.any?{|ext| ext === extension}
24
+ end
25
+ list.delete_if do |entry|
26
+ next unless test(?d, entry)
27
+ dirname = File.expand_path(entry)
28
+ ignore_directories.any?{|dir| File.expand_path(dir) == dirname}
29
+ end
30
+ list.delete_if do |entry|
31
+ next unless test(?f, entry)
32
+ filename = File.expand_path(entry)
33
+ ignore_files.any?{|file| File.expand_path(file) == filename}
34
+ end
35
+ end
36
+
37
+ lib = This.lib
38
+ version = This.version
39
+ files = shiteless[Dir::glob("**/**")]
40
+ executables = shiteless[Dir::glob("bin/*")].map{|exe| File.basename(exe)}
41
+ has_rdoc = true #File.exist?('doc')
42
+ test_files = "test/#{ lib }.rb" if File.file?("test/#{ lib }.rb")
43
+
44
+ extensions = This.extensions
45
+ if extensions.nil?
46
+ %w( Makefile configure extconf.rb ).each do |ext|
47
+ extensions << ext if File.exists?(ext)
48
+ end
49
+ end
50
+ extensions = [extensions].flatten.compact
51
+
52
+ template =
53
+ if test(?e, 'gemspec.erb')
54
+ Template{ IO.read('gemspec.erb') }
55
+ else
56
+ Template {
57
+ <<-__
58
+ ## #{ lib }.gemspec
59
+ #
60
+
61
+ Gem::Specification::new do |spec|
62
+ spec.name = #{ lib.inspect }
63
+ spec.version = #{ version.inspect }
64
+ spec.platform = Gem::Platform::RUBY
65
+ spec.summary = #{ lib.inspect }
66
+
67
+ spec.files = #{ files.inspect }
68
+ spec.executables = #{ executables.inspect }
69
+
70
+ spec.require_path = "lib"
71
+
72
+ spec.has_rdoc = #{ has_rdoc.inspect }
73
+ spec.test_files = #{ test_files.inspect }
74
+ #spec.add_dependency 'lib', '>= version'
75
+ #spec.add_dependency 'fattr'
76
+
77
+ spec.extensions.push(*#{ extensions.inspect })
78
+
79
+ spec.rubyforge_project = #{ This.rubyforge_project.inspect }
80
+ spec.author = #{ This.author.inspect }
81
+ spec.email = #{ This.email.inspect }
82
+ spec.homepage = #{ This.homepage.inspect }
83
+ end
84
+ __
85
+ }
86
+ end
87
+
88
+ open("#{ lib }.gemspec", "w"){|fd| fd.puts template}
89
+ This.gemspec = "#{ lib }.gemspec"
90
+ end
91
+
92
+ task :gem => [:clean, :gemspec] do
93
+ Fu.mkdir_p This.pkgdir
94
+ before = Dir['*.gem']
95
+ cmd = "gem build #{ This.gemspec }"
96
+ `#{ cmd }`
97
+ after = Dir['*.gem']
98
+ gem = ((after - before).first || after.first) or abort('no gem!')
99
+ Fu.mv gem, This.pkgdir
100
+ This.gem = File.basename(gem)
101
+ end
102
+
103
+ task :readme do
104
+ samples = ''
105
+ prompt = '~ > '
106
+ lib = This.lib
107
+ version = This.version
108
+
109
+ Dir['sample*/*'].sort.each do |sample|
110
+ samples << "\n" << " <========< #{ sample } >========>" << "\n\n"
111
+
112
+ cmd = "cat #{ sample }"
113
+ samples << Util.indent(prompt + cmd, 2) << "\n\n"
114
+ samples << Util.indent(`#{ cmd }`, 4) << "\n"
115
+
116
+ cmd = "ruby #{ sample }"
117
+ samples << Util.indent(prompt + cmd, 2) << "\n\n"
118
+
119
+ cmd = "ruby -e'STDOUT.sync=true; exec %(ruby -Ilib #{ sample })'"
120
+ samples << Util.indent(`#{ cmd } 2>&1`, 4) << "\n"
121
+ end
122
+
123
+ template =
124
+ if test(?e, 'readme.erb')
125
+ Template{ IO.read('readme.erb') }
126
+ else
127
+ Template {
128
+ <<-__
129
+ NAME
130
+ #{ lib }
131
+
132
+ DESCRIPTION
133
+
134
+ INSTALL
135
+ gem install #{ lib }
136
+
137
+ SAMPLES
138
+ #{ samples }
139
+ __
140
+ }
141
+ end
142
+
143
+ open("README", "w"){|fd| fd.puts template}
144
+ end
145
+
146
+
147
+ task :clean do
148
+ Dir[File.join(This.pkgdir, '**/**')].each{|entry| Fu.rm_rf(entry)}
149
+ end
150
+
151
+
152
+ task :release => [:clean, :gemspec, :gem] do
153
+ gems = Dir[File.join(This.pkgdir, '*.gem')].flatten
154
+ raise "which one? : #{ gems.inspect }" if gems.size > 1
155
+ raise "no gems?" if gems.size < 1
156
+ cmd = "rubyforge login && rubyforge add_release #{ This.rubyforge_project } #{ This.lib } #{ This.version } #{ This.pkgdir }/#{ This.gem }"
157
+ puts cmd
158
+ system cmd
159
+ end
160
+
161
+
162
+
163
+
164
+
165
+ BEGIN {
166
+ $VERBOSE = nil
167
+
168
+ require 'ostruct'
169
+ require 'erb'
170
+ require 'fileutils'
171
+
172
+ Fu = FileUtils
173
+
174
+ This = OpenStruct.new
175
+
176
+ This.file = File.expand_path(__FILE__)
177
+ This.dir = File.dirname(This.file)
178
+ This.pkgdir = File.join(This.dir, 'pkg')
179
+
180
+ lib = ENV['LIB']
181
+ unless lib
182
+ lib = File.basename(Dir.pwd)
183
+ end
184
+ This.lib = lib
185
+
186
+ version = ENV['VERSION']
187
+ unless version
188
+ name = lib.capitalize
189
+ require "./lib/#{ lib }"
190
+ version = eval(name).send(:version)
191
+ end
192
+ This.version = version
193
+
194
+ abort('no lib') unless This.lib
195
+ abort('no version') unless This.version
196
+
197
+ module Util
198
+ def indent(s, n = 2)
199
+ s = unindent(s)
200
+ ws = ' ' * n
201
+ s.gsub(%r/^/, ws)
202
+ end
203
+
204
+ def unindent(s)
205
+ indent = nil
206
+ s.each do |line|
207
+ next if line =~ %r/^\s*$/
208
+ indent = line[%r/^\s*/] and break
209
+ end
210
+ indent ? s.gsub(%r/^#{ indent }/, "") : s
211
+ end
212
+ extend self
213
+ end
214
+
215
+ class Template
216
+ def initialize(&block)
217
+ @block = block
218
+ @template = block.call.to_s
219
+ end
220
+ def expand(b=nil)
221
+ ERB.new(Util.unindent(@template)).result(b||@block)
222
+ end
223
+ alias_method 'to_s', 'expand'
224
+ end
225
+ def Template(*args, &block) Template.new(*args, &block) end
226
+
227
+ Dir.chdir(This.dir)
228
+ }
@@ -1,42 +1,96 @@
1
+ # shared.rb provides a super easy way to share code between classes or modules
2
+ # in a simple way. shared code can be at the class and/or instance level and
3
+ # users deferred evaluation so this is more powerful that the normal ruby
4
+ # module inclusion facilities on which it is based.
5
+ #
6
+ # basic usage:
7
+ #
8
+ #
9
+ # require 'shared'
10
+ #
11
+ # Shared 'methods' do
12
+ # class << self
13
+ # attr :classname
14
+ # end
15
+ #
16
+ # @classname = name.downcase
17
+ #
18
+ # def objectname
19
+ # self.class.classname + "(#{ object_id })"
20
+ # end
21
+ # end
22
+ #
23
+ # class C
24
+ # include Shared('methods')
25
+ # end
26
+ #
27
+ # class B
28
+ # include Shared('methods')
29
+ # end
30
+ #
31
+ # p C.classname #=> 'c'
32
+ # p C.new.objectname #=> 'c(1234)'
33
+ #
34
+ # p B.classname #=> 'b'
35
+ # p B.new.objectname #=> 'b(4567)'
36
+ #
37
+
38
+
1
39
  unless defined?(Shared)
2
40
  module Shared
3
- Shared::VERSION = '1.0.0' unless defined?(Shared::VERSION)
41
+ Shared::VERSION = '1.1.0' unless defined?(Shared::VERSION)
4
42
  def version() Shared::VERSION end
5
43
 
6
44
  Code = {}
7
45
 
8
- def shared name, &block
46
+ def load key
47
+ key = key_for(key)
48
+ unless Code.has_key?(key)
49
+ ::Kernel.load("shared/#{ key }.rb")
50
+ end
51
+ end
52
+
53
+ def shared name, options = {}, &block
9
54
  key = key_for name
55
+ via = (options[:via]||options['via']||:eval).to_s.to_sym
10
56
 
11
- return Code[key] if block.nil?
57
+ if block.nil?
58
+ Shared.load(key)
59
+ return Code[key]
60
+ end
12
61
 
13
62
  m = (Code[key] || Module.new)
14
63
 
15
- singleton_class(m) do
16
- unless m.respond_to?(:blocks)
17
- blocks = []
18
- define_method(:blocks){ blocks }
64
+ case via
65
+ when :eval
66
+ singleton_class(m) do
67
+ unless m.respond_to?(:blocks)
68
+ blocks = []
19
69
 
20
- define_method(:included) do |other|
21
- blocks.each{|b| other.send(:module_eval, &b)}
22
- end
70
+ define_method(:blocks){ blocks }
23
71
 
24
- define_method(:extend_object) do |other|
25
- Shared.singleton_class(other) do
26
- m.blocks.each{|b| module_eval &b}
72
+ define_method(:included) do |other|
73
+ blocks.each{|b| other.send(:module_eval, &b)}
74
+ end
75
+
76
+ define_method(:extend_object) do |other|
77
+ Shared.singleton_class(other) do
78
+ m.blocks.each{|b| module_eval &b}
79
+ end
80
+ end
27
81
  end
28
82
  end
29
- end
30
- end
83
+ m.blocks << block
31
84
 
32
- m.blocks << block
85
+ when :module
86
+ m.send(:module_eval, &block)
87
+ end
33
88
 
34
89
  Code[key] ||= m
35
90
  end
36
91
 
37
92
  alias_method 'share', 'shared'
38
93
  alias_method 'for', 'shared'
39
- alias_method '[]', 'shared'
40
94
 
41
95
  def key_for name
42
96
  name.to_s.strip.downcase
@@ -55,20 +109,20 @@ unless defined?(Shared)
55
109
 
56
110
  module Kernel
57
111
  private
58
- def share(*a, &b)
59
- Shared.share(*a, &b)
60
- end
61
-
62
112
  def Share(*a, &b)
63
- Shared.share(*a, &b)
64
- end
65
-
66
- def shared(*a, &b)
67
- Shared.shared(*a, &b)
113
+ if a.empty? and b.nil?
114
+ ::Shared
115
+ else
116
+ Shared.share(*a, &b)
117
+ end
68
118
  end
69
119
 
70
120
  def Shared(*a, &b)
71
- Shared.shared(*a, &b)
121
+ if a.empty? and b.nil?
122
+ ::Shared
123
+ else
124
+ Shared.shared(*a, &b)
125
+ end
72
126
  end
73
127
  end
74
128
  end
@@ -0,0 +1,37 @@
1
+ NAME
2
+ shared.rb
3
+
4
+ DESCRIPTION
5
+ shared.rb provides a super easy way to share code between classes or modules
6
+ in a simple way. shared code can be at the class and/or instance level and
7
+ users deferred evaluation so this is more powerful that the normal ruby
8
+ module inclusion facilities on which it is based.
9
+
10
+ SYNOPSIS
11
+ require 'shared'
12
+
13
+ Shared 'methods' do
14
+ class << self
15
+ attr :classname
16
+ end
17
+
18
+ @classname = name.downcase
19
+
20
+ def objectname
21
+ self.class.classname + "(#{ object_id })"
22
+ end
23
+ end
24
+
25
+ class C
26
+ include Shared('methods')
27
+ end
28
+
29
+ class B
30
+ include Shared('methods')
31
+ end
32
+
33
+ p C.classname #=> 'c'
34
+ p C.new.objectname #=> 'c(1234)'
35
+
36
+ p B.classname #=> 'b'
37
+ p B.new.objectname #=> 'b(4567)'
@@ -0,0 +1,26 @@
1
+ ## shared.gemspec
2
+ #
3
+
4
+ Gem::Specification::new do |spec|
5
+ spec.name = "shared"
6
+ spec.version = "1.1.0"
7
+ spec.platform = Gem::Platform::RUBY
8
+ spec.summary = "shared"
9
+
10
+ spec.files = ["lib", "lib/shared.rb", "Rakefile", "README", "readme.erb", "shared.gemspec"]
11
+ spec.executables = []
12
+
13
+ spec.require_path = "lib"
14
+
15
+ spec.has_rdoc = true
16
+ spec.test_files = nil
17
+ #spec.add_dependency 'lib', '>= version'
18
+ #spec.add_dependency 'fattr'
19
+
20
+ spec.extensions.push(*[])
21
+
22
+ spec.rubyforge_project = "codeforpeople"
23
+ spec.author = "Ara T. Howard"
24
+ spec.email = "ara.t.howard@gmail.com"
25
+ spec.homepage = "http://github.com/ahoward/shared/tree/master"
26
+ 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: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ara T. Howard
@@ -9,19 +9,10 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-02-05 00:00:00 -07:00
12
+ date: 2009-07-28 00:00:00 -06:00
13
13
  default_executable:
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
16
- name: fattr
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
23
- version: "0"
24
- version:
14
+ dependencies: []
15
+
25
16
  description:
26
17
  email: ara.t.howard@gmail.com
27
18
  executables: []
@@ -31,22 +22,14 @@ extensions: []
31
22
  extra_rdoc_files: []
32
23
 
33
24
  files:
34
- - a.rb
35
- - ann-shared-0.4.2.txt
36
- - gemspec.rb
37
- - gen_readme.rb
38
- - install.rb
39
25
  - lib
40
26
  - lib/shared.rb
27
+ - Rakefile
41
28
  - README
42
- - samples
43
- - samples/a.rb
44
- - samples/b.rb
45
- - samples/c.rb
46
- - samples/d.rb
47
- - samples/e.rb
48
- has_rdoc: false
49
- homepage: http://codeforpeople.com/lib/ruby/shared/
29
+ - readme.erb
30
+ - shared.gemspec
31
+ has_rdoc: true
32
+ homepage: http://github.com/ahoward/shared/tree/master
50
33
  post_install_message:
51
34
  rdoc_options: []
52
35
 
data/a.rb DELETED
@@ -1,12 +0,0 @@
1
- Share :foo do
2
- def foo() p :foo end
3
- end
4
- include Shared(:foo)
5
-
6
- Share :bar do
7
- def bar() p :bar end
8
- end
9
- include Shared(:bar)
10
-
11
- foo
12
- bar
@@ -1,183 +0,0 @@
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
-
data/gemspec.rb DELETED
@@ -1,38 +0,0 @@
1
- #! /usr/bin/env gem build
2
-
3
- lib, version = File::basename(File::dirname(File::expand_path(__FILE__))).split %r/-/, 2
4
-
5
- require 'rubygems'
6
-
7
- Gem::Specification::new do |spec|
8
- $VERBOSE = nil
9
-
10
- shiteless = lambda do |list|
11
- list.delete_if do |file|
12
- file =~ %r/\.svn/ or
13
- file =~ %r/\.tmp/
14
- end
15
- end
16
-
17
- spec.name = lib
18
- spec.version = version
19
- spec.platform = Gem::Platform::RUBY
20
- spec.summary = lib
21
-
22
- spec.files = shiteless[Dir::glob("**/**")]
23
- spec.executables = shiteless[Dir::glob("bin/*")].map{|exe| File::basename exe}
24
-
25
- spec.require_path = "lib"
26
-
27
- spec.has_rdoc = File::exist? "doc"
28
- spec.test_suite_file = "test/#{ lib }.rb" if File::directory? "test"
29
- #spec.add_dependency 'lib', '>= version'
30
- spec.add_dependency 'fattr'
31
-
32
- spec.extensions << "extconf.rb" if File::exists? "extconf.rb"
33
-
34
- spec.rubyforge_project = 'codeforpeople'
35
- spec.author = "Ara T. Howard"
36
- spec.email = "ara.t.howard@gmail.com"
37
- spec.homepage = "http://codeforpeople.com/lib/ruby/#{ lib }/"
38
- end
@@ -1,35 +0,0 @@
1
- #! /usr/bin/env ruby
2
-
3
- require 'pathname'
4
-
5
- $VERBOSE=nil
6
-
7
- def indent s, n = 2
8
- ws = ' ' * n
9
- s.gsub %r/^/, ws
10
- end
11
-
12
- template = IO::read 'README.tmpl'
13
-
14
- samples = ''
15
- prompt = '~ > '
16
-
17
- Dir['sample*/*'].sort.each do |sample|
18
- samples << "\n" << " <========< #{ sample } >========>" << "\n\n"
19
-
20
- cmd = "cat #{ sample }"
21
- samples << indent(prompt + cmd, 2) << "\n\n"
22
- samples << indent(`#{ cmd }`, 4) << "\n"
23
-
24
- cmd = "ruby #{ sample }"
25
- samples << indent(prompt + cmd, 2) << "\n\n"
26
-
27
- cmd = "ruby -e'STDOUT.sync=true; exec %(ruby -Ilib #{ sample })'"
28
- #cmd = "ruby -Ilib #{ sample }"
29
- samples << indent(`#{ cmd } 2>&1`, 4) << "\n"
30
- end
31
-
32
- #samples.gsub! %r/^/, ' '
33
-
34
- readme = template.gsub %r/^\s*@samples\s*$/, samples
35
- print readme
data/install.rb DELETED
@@ -1,214 +0,0 @@
1
- #!/usr/bin/env ruby
2
- require 'rbconfig'
3
- require 'find'
4
- require 'ftools'
5
- require 'tempfile'
6
- include Config
7
-
8
- LIBDIR = "lib"
9
- LIBDIR_MODE = 0644
10
-
11
- BINDIR = "bin"
12
- BINDIR_MODE = 0755
13
-
14
-
15
- $srcdir = CONFIG["srcdir"]
16
- $version = CONFIG["MAJOR"]+"."+CONFIG["MINOR"]
17
- $libdir = File.join(CONFIG["libdir"], "ruby", $version)
18
- $archdir = File.join($libdir, CONFIG["arch"])
19
- $site_libdir = $:.find {|x| x =~ /site_ruby$/}
20
- $bindir = CONFIG["bindir"] || CONFIG['BINDIR']
21
- $ruby_install_name = CONFIG['ruby_install_name'] || CONFIG['RUBY_INSTALL_NAME'] || 'ruby'
22
- $ruby_ext = CONFIG['EXEEXT'] || ''
23
- $ruby = File.join($bindir, ($ruby_install_name + $ruby_ext))
24
-
25
- if !$site_libdir
26
- $site_libdir = File.join($libdir, "site_ruby")
27
- elsif $site_libdir !~ %r/#{Regexp.quote($version)}/
28
- $site_libdir = File.join($site_libdir, $version)
29
- end
30
-
31
- def install_rb(srcdir=nil, destdir=nil, mode=nil, bin=nil)
32
- #{{{
33
- path = []
34
- dir = []
35
- Find.find(srcdir) do |f|
36
- next unless FileTest.file?(f)
37
- next if (f = f[srcdir.length+1..-1]) == nil
38
- next if (/CVS$/ =~ File.dirname(f))
39
- next if (/\.svn/ =~ File.dirname(f))
40
- next if f =~ %r/\.lnk/
41
- next if f =~ %r/\.svn/
42
- next if f =~ %r/\.swp/
43
- next if f =~ %r/\.svn/
44
- path.push f
45
- dir |= [File.dirname(f)]
46
- end
47
- for f in dir
48
- next if f == "."
49
- next if f == "CVS"
50
- File::makedirs(File.join(destdir, f))
51
- end
52
- for f in path
53
- next if (/\~$/ =~ f)
54
- next if (/^\./ =~ File.basename(f))
55
- unless bin
56
- File::install(File.join(srcdir, f), File.join(destdir, f), mode, true)
57
- else
58
- from = File.join(srcdir, f)
59
- to = File.join(destdir, f)
60
- shebangify(from) do |sf|
61
- $deferr.print from, " -> ", File::catname(from, to), "\n"
62
- $deferr.printf "chmod %04o %s\n", mode, to
63
- File::install(sf, to, mode, false)
64
- end
65
- end
66
- end
67
- #}}}
68
- end
69
- def shebangify f
70
- #{{{
71
- open(f) do |fd|
72
- buf = fd.read 42
73
- if buf =~ %r/^\s*#\s*!.*ruby/o
74
- ftmp = Tempfile::new("#{ $$ }_#{ File::basename(f) }")
75
- begin
76
- fd.rewind
77
- ftmp.puts "#!#{ $ruby }"
78
- while((buf = fd.read(8192)))
79
- ftmp.write buf
80
- end
81
- ftmp.close
82
- yield ftmp.path
83
- ensure
84
- ftmp.close!
85
- end
86
- else
87
- yield f
88
- end
89
- end
90
- #}}}
91
- end
92
- def ARGV.switch
93
- #{{{
94
- return nil if self.empty?
95
- arg = self.shift
96
- return nil if arg == '--'
97
- if arg =~ /^-(.)(.*)/
98
- return arg if $1 == '-'
99
- raise 'unknown switch "-"' if $2.index('-')
100
- self.unshift "-#{$2}" if $2.size > 0
101
- "-#{$1}"
102
- else
103
- self.unshift arg
104
- nil
105
- end
106
- #}}}
107
- end
108
- def ARGV.req_arg
109
- #{{{
110
- self.shift || raise('missing argument')
111
- #}}}
112
- end
113
- def linkify d, linked = []
114
- #--{{{
115
- if test ?d, d
116
- versioned = Dir[ File::join(d, "*-[0-9].[0-9].[0-9].rb") ]
117
- versioned.each do |v|
118
- src, dst = v, v.gsub(%r/\-[\d\.]+\.rb$/, '.rb')
119
- lnk = nil
120
- begin
121
- if test ?l, dst
122
- lnk = "#{ dst }.lnk"
123
- puts "#{ dst } -> #{ lnk }"
124
- File::rename dst, lnk
125
- end
126
- unless test ?e, dst
127
- puts "#{ src } -> #{ dst }"
128
- File::copy src, dst
129
- linked << dst
130
- end
131
- ensure
132
- if lnk
133
- at_exit do
134
- puts "#{ lnk } -> #{ dst }"
135
- File::rename lnk, dst
136
- end
137
- end
138
- end
139
- end
140
- end
141
- linked
142
- #--}}}
143
- end
144
-
145
-
146
- #
147
- # main program
148
- #
149
-
150
- libdir = $site_libdir
151
- bindir = $bindir
152
- no_linkify = false
153
- linked = nil
154
- help = false
155
-
156
- usage = <<-usage
157
- #{ File::basename $0 }
158
- -d, --destdir <destdir>
159
- -l, --libdir <libdir>
160
- -b, --bindir <bindir>
161
- -r, --ruby <ruby>
162
- -n, --no_linkify
163
- -s, --sudo
164
- -h, --help
165
- usage
166
-
167
- begin
168
- while switch = ARGV.switch
169
- case switch
170
- when '-d', '--destdir'
171
- libdir = ARGV.req_arg
172
- when '-l', '--libdir'
173
- libdir = ARGV.req_arg
174
- when '-b', '--bindir'
175
- bindir = ARGV.req_arg
176
- when '-r', '--ruby'
177
- $ruby = ARGV.req_arg
178
- when '-n', '--no_linkify'
179
- no_linkify = true
180
- when '-s', '--sudo'
181
- sudo = 'sudo'
182
- when '-h', '--help'
183
- help = true
184
- else
185
- raise "unknown switch #{switch.dump}"
186
- end
187
- end
188
- rescue
189
- STDERR.puts $!.to_s
190
- STDERR.puts usage
191
- exit 1
192
- end
193
-
194
- if help
195
- STDOUT.puts usage
196
- exit
197
- end
198
-
199
- system "#{ sudo } #{ $ruby } pre-install.rb" if test(?s, 'pre-install.rb')
200
-
201
- unless no_linkify
202
- linked = linkify('lib') + linkify('bin')
203
- end
204
-
205
- system "#{ $ruby } extconf.rb && make && #{ sudo } make install" if test(?s, 'extconf.rb')
206
-
207
- install_rb(LIBDIR, libdir, LIBDIR_MODE)
208
- install_rb(BINDIR, bindir, BINDIR_MODE, bin=true)
209
-
210
- if linked
211
- linked.each{|path| File::rm_f path}
212
- end
213
-
214
- system "#{ sudo } #{ $ruby } post-install.rb" if test(?s, 'post-install.rb')
@@ -1,20 +0,0 @@
1
- # shared.rb is a very simple and very powerful method of sharing code between
2
- # classes.
3
- #
4
- require 'shared'
5
-
6
- shared(:code) do
7
- def classname() self.class.name end
8
- end
9
-
10
- class Foo
11
- include shared(:code)
12
- end
13
-
14
- class Bar
15
- include shared(:code)
16
- end
17
-
18
- p Foo.new.classname #=> "Foo"
19
-
20
- p Bar.new.classname #=> "Bar"
@@ -1,18 +0,0 @@
1
- # shared.rb allows natural declaration of both instance and class-level
2
- # methods - it's more than simple module inclusion
3
- #
4
-
5
- require 'shared'
6
-
7
- shared('methods') do
8
- def self.class_method() 40 end
9
-
10
- def instance_method() 2 end
11
- end
12
-
13
- class C
14
- include shared('methods')
15
- end
16
-
17
- p(C.class_method + C.new.instance_method) #=> 42
18
-
@@ -1,25 +0,0 @@
1
- # shared.rb works equally well with individual objects in addition to the
2
- # normal class level usage
3
- #
4
-
5
- require 'shared'
6
-
7
- shared(:meta_tools) do
8
- def singleton_class &block
9
- singleton_class =
10
- class << self
11
- self
12
- end
13
- block ? singleton_class.module_eval(&block) : singleton_class
14
- end
15
- end
16
-
17
- a = %w( a b c )
18
-
19
- a.extend shared(:meta_tools)
20
-
21
- a.singleton_class do
22
- def to_range() first .. last end
23
- end
24
-
25
- p a.to_range #=> "a".."c"
@@ -1,21 +0,0 @@
1
- # an example use-case for shared.rb is sharing code bewteen classes that
2
- # require both class level and instance level behaviour to be shared
3
- #
4
-
5
- require 'shared'
6
-
7
- shared(:acts_as_named) do
8
- validates_precence_of :name
9
-
10
- def to_html() "<blink> #{ name } </blink>" end
11
- end
12
-
13
- class Model
14
- def Model.validates_precence_of(*a) end
15
-
16
- def name() 'zaphod' end
17
-
18
- include shared(:acts_as_named)
19
- end
20
-
21
- p Model.new.to_html #=> "<blink> zaphod </blink>"
@@ -1,30 +0,0 @@
1
- # it's important to note that the shared code is injected into the reciever
2
- # fresh on each call and, in that way, the effect is rather macro like
3
- #
4
-
5
- require 'shared'
6
-
7
- share(:instance_tracker) do
8
- const_set :Instances, []
9
-
10
- def self.instances() const_get :Instances end
11
-
12
- def self.new *a, &b
13
- obj = super
14
- ensure
15
- instances << obj
16
- end
17
- end
18
-
19
- class Foo
20
- include shared(:instance_tracker)
21
- end
22
-
23
- class Bar
24
- include shared(:instance_tracker)
25
- end
26
-
27
- 2.times{ Foo.new }
28
- 40.times{ Bar.new }
29
-
30
- p(Foo.instances.size + Bar.instances.size)