attributes 3.0.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 ADDED
@@ -0,0 +1,146 @@
1
+ NAME
2
+
3
+ attributes.rb
4
+
5
+ URIS
6
+
7
+ http://rubyforge.org/projects/codeforpeople/
8
+ http://codeforpeople.com/lib/ruby
9
+
10
+ SYNOPSIS
11
+
12
+ attributes.rb provides an attr_* like method will several user friendly
13
+ additions. attributes.rb is similar to the traits.rb package but sacrafices
14
+ a few features for simplicity of implementation: attributes.rb is only 42
15
+ lines of code.
16
+
17
+ the implimentation of attributes.rb borrows many of the best ideas from the
18
+ metakoans.rb ruby quiz
19
+
20
+ http://www.rubyquiz.com/quiz67.html
21
+
22
+ in particular the solutions of Christian Neukirchen and Florian Gross.
23
+
24
+ SAMPLES
25
+
26
+ <========< samples/a.rb >========>
27
+
28
+ ~ > cat samples/a.rb
29
+
30
+ #
31
+ # basic usage is like attr, but note that attribute defines three methods,
32
+ # getter, setter, and query
33
+ #
34
+ require 'attributes'
35
+
36
+ class C
37
+ attribute 'a'
38
+ end
39
+
40
+ c = C.new
41
+
42
+ c.a = 42 # setter
43
+ p c.a # getter
44
+ p 'forty-two' if c.a? # query
45
+
46
+ ~ > ruby samples/a.rb
47
+
48
+ 42
49
+ "forty-two"
50
+
51
+
52
+ <========< samples/b.rb >========>
53
+
54
+ ~ > cat samples/b.rb
55
+
56
+ #
57
+ # default values may be given either directly or as a block which will be
58
+ # evaluated in the context of self. in both cases (value or block) the
59
+ # default is set only once and only if needed - it's a lazy evaluation.
60
+ #
61
+ require 'attributes'
62
+
63
+ class C
64
+ attribute :a => 42
65
+ attribute(:b){ Float a }
66
+ end
67
+
68
+ c = C.new
69
+
70
+ p c.a
71
+ p c.b
72
+
73
+ ~ > ruby samples/b.rb
74
+
75
+ 42
76
+ 42.0
77
+
78
+
79
+ <========< samples/c.rb >========>
80
+
81
+ ~ > cat samples/c.rb
82
+
83
+ #
84
+ # multiple values may by given, plain names and key/val pairs may be mixed.
85
+ #
86
+ require 'attributes'
87
+
88
+ class C
89
+ attributes 'x', 'y' => 0b101000, 'z' => 0b10
90
+ end
91
+
92
+ c = C.new
93
+ c.x = c.y + c.z
94
+ p c.x
95
+
96
+
97
+ ~ > ruby samples/c.rb
98
+
99
+ 42
100
+
101
+
102
+ <========< samples/d.rb >========>
103
+
104
+ ~ > cat samples/d.rb
105
+
106
+ #
107
+ # a nice feature is that all attributes are enumerated in the class. this,
108
+ # combined with the fact that the getter method is defined so as to delegate
109
+ # to the setter when an argument is given, means bulk initialization and/or
110
+ # attribute traversal is very easy.
111
+ #
112
+ require 'attributes'
113
+
114
+ class C
115
+ attributes %w( x y z )
116
+
117
+ def attributes
118
+ self.class.attributes
119
+ end
120
+
121
+ def initialize
122
+ attributes.each_with_index{|a,i| send a, i}
123
+ end
124
+
125
+ def to_hash
126
+ attributes.inject({}){|h,a| h.update a => send(a)}
127
+ end
128
+
129
+ def inspect
130
+ to_hash.inspect
131
+ end
132
+ end
133
+
134
+ c = C.new
135
+ p c.attributes
136
+ p c
137
+
138
+ c.x 'forty-two'
139
+ p c.x
140
+
141
+ ~ > ruby samples/d.rb
142
+
143
+ ["x", "y", "z"]
144
+ {"x"=>0, "y"=>1, "z"=>2}
145
+ "forty-two"
146
+
@@ -0,0 +1,26 @@
1
+ NAME
2
+
3
+ attributes.rb
4
+
5
+ URIS
6
+
7
+ http://rubyforge.org/projects/codeforpeople/
8
+ http://codeforpeople.com/lib/ruby
9
+
10
+ SYNOPSIS
11
+
12
+ attributes.rb provides an attr_* like method will several user friendly
13
+ additions. attributes.rb is similar to the traits.rb package but sacrafices
14
+ a few features for simplicity of implementation: attributes.rb is only 42
15
+ lines of code.
16
+
17
+ the implimentation of attributes.rb borrows many of the best ideas from the
18
+ metakoans.rb ruby quiz
19
+
20
+ http://www.rubyquiz.com/quiz67.html
21
+
22
+ in particular the solutions of Christian Neukirchen and Florian Gross.
23
+
24
+ SAMPLES
25
+
26
+ @samples
@@ -0,0 +1,23 @@
1
+ lib, version = File::basename(File::dirname(File::expand_path(__FILE__))).split %r/-/, 2
2
+
3
+ require 'rubygems'
4
+
5
+ Gem::Specification::new do |spec|
6
+ spec.name = lib
7
+ spec.version = version
8
+ spec.platform = Gem::Platform::RUBY
9
+ spec.summary = lib
10
+
11
+ spec.files = Dir::glob "**/**"
12
+ spec.executables = Dir::glob("bin/*").map{|exe| File::basename exe}
13
+
14
+ spec.require_path = "lib"
15
+ spec.autorequire = lib
16
+
17
+ spec.has_rdoc = File::exist? "doc"
18
+ spec.test_suite_file = "test/#{ lib }.rb" if File::directory? "test"
19
+
20
+ spec.author = "Ara T. Howard"
21
+ spec.email = "ara.t.howard@noaa.gov"
22
+ spec.homepage = "http://codeforpeople.com/lib/ruby/#{ lib }/"
23
+ end
@@ -0,0 +1,32 @@
1
+ require 'pathname'
2
+
3
+ $VERBOSE=nil
4
+
5
+ def indent s, n = 2
6
+ ws = ' ' * n
7
+ s.gsub %r/^/, ws
8
+ end
9
+
10
+ template = IO::read 'README.tmpl'
11
+
12
+ samples = ''
13
+ prompt = '~ > '
14
+
15
+ Dir['sample*/*'] .each do |sample|
16
+ samples << "\n" << " <========< #{ sample } >========>" << "\n\n"
17
+
18
+ cmd = "cat #{ sample }"
19
+ samples << indent(prompt + cmd, 2) << "\n\n"
20
+ samples << indent(`#{ cmd }`, 4) << "\n"
21
+
22
+ cmd = "ruby #{ sample }"
23
+ samples << indent(prompt + cmd, 2) << "\n\n"
24
+
25
+ cmd = "ruby -Ilib #{ sample }"
26
+ samples << indent(`#{ cmd } 2>&1`, 4) << "\n"
27
+ end
28
+
29
+ #samples.gsub! %r/^/, ' '
30
+
31
+ readme = template.gsub %r/^\s*@samples\s*$/, samples
32
+ print readme
@@ -0,0 +1,206 @@
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 f =~ %r/\.lnk/
40
+ path.push f
41
+ dir |= [File.dirname(f)]
42
+ end
43
+ for f in dir
44
+ next if f == "."
45
+ next if f == "CVS"
46
+ File::makedirs(File.join(destdir, f))
47
+ end
48
+ for f in path
49
+ next if (/\~$/ =~ f)
50
+ next if (/^\./ =~ File.basename(f))
51
+ unless bin
52
+ File::install(File.join(srcdir, f), File.join(destdir, f), mode, true)
53
+ else
54
+ from = File.join(srcdir, f)
55
+ to = File.join(destdir, f)
56
+ shebangify(from) do |sf|
57
+ $deferr.print from, " -> ", File::catname(from, to), "\n"
58
+ $deferr.printf "chmod %04o %s\n", mode, to
59
+ File::install(sf, to, mode, false)
60
+ end
61
+ end
62
+ end
63
+ #}}}
64
+ end
65
+ def shebangify f
66
+ #{{{
67
+ open(f) do |fd|
68
+ buf = fd.read 42
69
+ if buf =~ %r/^\s*#\s*!.*ruby/o
70
+ ftmp = Tempfile::new("#{ $$ }_#{ File::basename(f) }")
71
+ begin
72
+ fd.rewind
73
+ ftmp.puts "#!#{ $ruby }"
74
+ while((buf = fd.read(8192)))
75
+ ftmp.write buf
76
+ end
77
+ ftmp.close
78
+ yield ftmp.path
79
+ ensure
80
+ ftmp.close!
81
+ end
82
+ else
83
+ yield f
84
+ end
85
+ end
86
+ #}}}
87
+ end
88
+ def ARGV.switch
89
+ #{{{
90
+ return nil if self.empty?
91
+ arg = self.shift
92
+ return nil if arg == '--'
93
+ if arg =~ /^-(.)(.*)/
94
+ return arg if $1 == '-'
95
+ raise 'unknown switch "-"' if $2.index('-')
96
+ self.unshift "-#{$2}" if $2.size > 0
97
+ "-#{$1}"
98
+ else
99
+ self.unshift arg
100
+ nil
101
+ end
102
+ #}}}
103
+ end
104
+ def ARGV.req_arg
105
+ #{{{
106
+ self.shift || raise('missing argument')
107
+ #}}}
108
+ end
109
+ def linkify d, linked = []
110
+ #--{{{
111
+ if test ?d, d
112
+ versioned = Dir[ File::join(d, "*-[0-9].[0-9].[0-9].rb") ]
113
+ versioned.each do |v|
114
+ src, dst = v, v.gsub(%r/\-[\d\.]+\.rb$/, '.rb')
115
+ lnk = nil
116
+ begin
117
+ if test ?l, dst
118
+ lnk = "#{ dst }.lnk"
119
+ puts "#{ dst } -> #{ lnk }"
120
+ File::rename dst, lnk
121
+ end
122
+ unless test ?e, dst
123
+ puts "#{ src } -> #{ dst }"
124
+ File::copy src, dst
125
+ linked << dst
126
+ end
127
+ ensure
128
+ if lnk
129
+ at_exit do
130
+ puts "#{ lnk } -> #{ dst }"
131
+ File::rename lnk, dst
132
+ end
133
+ end
134
+ end
135
+ end
136
+ end
137
+ linked
138
+ #--}}}
139
+ end
140
+
141
+
142
+ #
143
+ # main program
144
+ #
145
+
146
+ libdir = $site_libdir
147
+ bindir = $bindir
148
+ no_linkify = false
149
+ linked = nil
150
+ help = false
151
+
152
+ usage = <<-usage
153
+ #{ File::basename $0 }
154
+ -d, --destdir <destdir>
155
+ -l, --libdir <libdir>
156
+ -b, --bindir <bindir>
157
+ -r, --ruby <ruby>
158
+ -n, --no_linkify
159
+ -s, --sudo
160
+ -h, --help
161
+ usage
162
+
163
+ begin
164
+ while switch = ARGV.switch
165
+ case switch
166
+ when '-d', '--destdir'
167
+ libdir = ARGV.req_arg
168
+ when '-l', '--libdir'
169
+ libdir = ARGV.req_arg
170
+ when '-b', '--bindir'
171
+ bindir = ARGV.req_arg
172
+ when '-r', '--ruby'
173
+ $ruby = ARGV.req_arg
174
+ when '-n', '--no_linkify'
175
+ no_linkify = true
176
+ when '-s', '--sudo'
177
+ sudo = 'sudo'
178
+ when '-h', '--help'
179
+ help = true
180
+ else
181
+ raise "unknown switch #{switch.dump}"
182
+ end
183
+ end
184
+ rescue
185
+ STDERR.puts $!.to_s
186
+ STDERR.puts usage
187
+ exit 1
188
+ end
189
+
190
+ if help
191
+ STDOUT.puts usage
192
+ exit
193
+ end
194
+
195
+ unless no_linkify
196
+ linked = linkify('lib') + linkify('bin')
197
+ end
198
+
199
+ system "#{ $ruby } extconf.rb && make && #{ sudo } make install" if test(?s, 'extconf.rb')
200
+
201
+ install_rb(LIBDIR, libdir, LIBDIR_MODE)
202
+ install_rb(BINDIR, bindir, BINDIR_MODE, bin=true)
203
+
204
+ if linked
205
+ linked.each{|path| File::rm_f path}
206
+ end
@@ -0,0 +1,49 @@
1
+ module Attrbiutes
2
+ def attributes *a, &b
3
+ unless a.empty?
4
+ hashes, names = a.partition{|x| Hash === x}
5
+
6
+ names_and_defaults = {}
7
+ hashes.each{|h| names_and_defaults.update h}
8
+ names.flatten.compact.each{|name| names_and_defaults.update name => nil}
9
+
10
+ names_and_defaults.each do |name, default|
11
+ init = b || lambda { default }
12
+ ivar = "@#{ name }"
13
+ getter = "#{ name }"
14
+ setter = "#{ name }="
15
+ query = "#{ name }?"
16
+
17
+ define_method(setter) do |value|
18
+ instance_variable_set ivar, value
19
+ end
20
+
21
+ define_method(getter) do |*value|
22
+ unless value.empty?
23
+ send setter, value.shift
24
+ else
25
+ defined = instance_eval "defined? #{ ivar }"
26
+ unless defined
27
+ send setter, instance_eval(&init)
28
+ end
29
+ instance_variable_get ivar
30
+ end
31
+ end
32
+
33
+ alias_method query, getter
34
+
35
+ (attributes << name).uniq!
36
+ attributes
37
+ end
38
+ else
39
+ @attributes ||= []
40
+ end
41
+ end
42
+ def attribute *a, &b
43
+ attributes *a, &b
44
+ end
45
+ end
46
+
47
+ class Module
48
+ include Attrbiutes
49
+ end
@@ -0,0 +1,49 @@
1
+ module Attrbiutes
2
+ def attributes *a, &b
3
+ unless a.empty?
4
+ hashes, names = a.partition{|x| Hash === x}
5
+
6
+ names_and_defaults = {}
7
+ hashes.each{|h| names_and_defaults.update h}
8
+ names.flatten.compact.each{|name| names_and_defaults.update name => nil}
9
+
10
+ names_and_defaults.each do |name, default|
11
+ init = b || lambda { default }
12
+ ivar = "@#{ name }"
13
+ getter = "#{ name }"
14
+ setter = "#{ name }="
15
+ query = "#{ name }?"
16
+
17
+ define_method(setter) do |value|
18
+ instance_variable_set ivar, value
19
+ end
20
+
21
+ define_method(getter) do |*value|
22
+ unless value.empty?
23
+ send setter, value.shift
24
+ else
25
+ defined = instance_eval "defined? #{ ivar }"
26
+ unless defined
27
+ send setter, instance_eval(&init)
28
+ end
29
+ instance_variable_get ivar
30
+ end
31
+ end
32
+
33
+ alias_method query, getter
34
+
35
+ (attributes << name).uniq!
36
+ attributes
37
+ end
38
+ else
39
+ @attributes ||= []
40
+ end
41
+ end
42
+ def attribute *a, &b
43
+ attributes *a, &b
44
+ end
45
+ end
46
+
47
+ class Module
48
+ include Attrbiutes
49
+ end
@@ -0,0 +1,15 @@
1
+ #
2
+ # basic usage is like attr, but note that attribute defines three methods,
3
+ # getter, setter, and query
4
+ #
5
+ require 'attributes'
6
+
7
+ class C
8
+ attribute 'a'
9
+ end
10
+
11
+ c = C.new
12
+
13
+ c.a = 42 # setter
14
+ p c.a # getter
15
+ p 'forty-two' if c.a? # query
@@ -0,0 +1,16 @@
1
+ #
2
+ # default values may be given either directly or as a block which will be
3
+ # evaluated in the context of self. in both cases (value or block) the
4
+ # default is set only once and only if needed - it's a lazy evaluation.
5
+ #
6
+ require 'attributes'
7
+
8
+ class C
9
+ attribute :a => 42
10
+ attribute(:b){ Float a }
11
+ end
12
+
13
+ c = C.new
14
+
15
+ p c.a
16
+ p c.b
@@ -0,0 +1,13 @@
1
+ #
2
+ # multiple values may by given, plain names and key/val pairs may be mixed.
3
+ #
4
+ require 'attributes'
5
+
6
+ class C
7
+ attributes 'x', 'y' => 0b101000, 'z' => 0b10
8
+ end
9
+
10
+ c = C.new
11
+ c.x = c.y + c.z
12
+ p c.x
13
+
@@ -0,0 +1,34 @@
1
+ #
2
+ # a nice feature is that all attributes are enumerated in the class. this,
3
+ # combined with the fact that the getter method is defined so as to delegate
4
+ # to the setter when an argument is given, means bulk initialization and/or
5
+ # attribute traversal is very easy.
6
+ #
7
+ require 'attributes'
8
+
9
+ class C
10
+ attributes %w( x y z )
11
+
12
+ def attributes
13
+ self.class.attributes
14
+ end
15
+
16
+ def initialize
17
+ attributes.each_with_index{|a,i| send a, i}
18
+ end
19
+
20
+ def to_hash
21
+ attributes.inject({}){|h,a| h.update a => send(a)}
22
+ end
23
+
24
+ def inspect
25
+ to_hash.inspect
26
+ end
27
+ end
28
+
29
+ c = C.new
30
+ p c.attributes
31
+ p c
32
+
33
+ c.x 'forty-two'
34
+ p c.x
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.11
3
+ specification_version: 1
4
+ name: attributes
5
+ version: !ruby/object:Gem::Version
6
+ version: 3.0.0
7
+ date: 2006-07-13 00:00:00.000000 -06:00
8
+ summary: attributes
9
+ require_paths:
10
+ - lib
11
+ email: ara.t.howard@noaa.gov
12
+ homepage: http://codeforpeople.com/lib/ruby/attributes/
13
+ rubyforge_project:
14
+ description:
15
+ autorequire: attributes
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: false
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ -
22
+ - ">"
23
+ - !ruby/object:Gem::Version
24
+ version: 0.0.0
25
+ version:
26
+ platform: ruby
27
+ signing_key:
28
+ cert_chain:
29
+ authors:
30
+ - Ara T. Howard
31
+ files:
32
+ - lib
33
+ - samples
34
+ - install.rb
35
+ - gemspec.rb
36
+ - gen_readme.rb
37
+ - README.tmpl
38
+ - README
39
+ - lib/attributes-3.0.0.rb
40
+ - lib/attributes.rb
41
+ - samples/a.rb
42
+ - samples/b.rb
43
+ - samples/c.rb
44
+ - samples/d.rb
45
+ test_files: []
46
+ rdoc_options: []
47
+ extra_rdoc_files: []
48
+ executables: []
49
+ extensions: []
50
+ requirements: []
51
+ dependencies: []