typed_accessors 0.2 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +8 -0
- data/Manifest.txt +19 -0
- data/PostInstall.txt +4 -0
- data/README.rdoc +69 -0
- data/Rakefile +28 -143
- data/config/website.yml +2 -0
- data/lib/typed_accessors.rb +5 -97
- data/lib/typed_accessors/class.rb +100 -0
- data/script/console +10 -0
- data/script/destroy +14 -0
- data/script/generate +14 -0
- data/script/txt2html +71 -0
- data/test/test_helper.rb +3 -0
- data/test/test_typed_accessors.rb +11 -0
- data/website/index.html +93 -0
- data/website/index.txt +64 -0
- data/website/javascripts/rounded_corners_lite.inc.js +285 -0
- data/website/stylesheets/screen.css +159 -0
- data/website/template.html.erb +50 -0
- metadata +75 -36
- data/COPYING +0 -21
- data/README +0 -50
- data/docs/typed_accessors/classes/Class.html +0 -260
- data/docs/typed_accessors/created.rid +0 -1
- data/docs/typed_accessors/files/COPYING.html +0 -133
- data/docs/typed_accessors/files/README.html +0 -156
- data/docs/typed_accessors/files/lib/typed_accessors_rb.html +0 -123
- data/docs/typed_accessors/fr_class_index.html +0 -27
- data/docs/typed_accessors/fr_file_index.html +0 -29
- data/docs/typed_accessors/fr_method_index.html +0 -30
- data/docs/typed_accessors/index.html +0 -24
- data/docs/typed_accessors/rdoc-style.css +0 -208
- data/test/basic_test.rb +0 -118
data/History.txt
ADDED
data/Manifest.txt
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
History.txt
|
2
|
+
Manifest.txt
|
3
|
+
PostInstall.txt
|
4
|
+
README.rdoc
|
5
|
+
Rakefile
|
6
|
+
config/website.yml
|
7
|
+
lib/typed_accessors.rb
|
8
|
+
lib/typed_accessors/class.rb
|
9
|
+
script/console
|
10
|
+
script/destroy
|
11
|
+
script/generate
|
12
|
+
script/txt2html
|
13
|
+
test/test_helper.rb
|
14
|
+
test/test_typed_accessors.rb
|
15
|
+
website/index.html
|
16
|
+
website/index.txt
|
17
|
+
website/javascripts/rounded_corners_lite.inc.js
|
18
|
+
website/stylesheets/screen.css
|
19
|
+
website/template.html.erb
|
data/PostInstall.txt
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
= typed_accessors
|
2
|
+
|
3
|
+
* http://github.com/sdague/typed_accessors
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
Typed accessors creates a set of accessors with a predefined typed.
|
8
|
+
Often when dealing with webservices all the data comes back as
|
9
|
+
strings, but you really want to be working with these things as
|
10
|
+
numbers or dates. Manually converting them is duplication, and can be
|
11
|
+
error prone.
|
12
|
+
|
13
|
+
== SYNOPSIS:
|
14
|
+
|
15
|
+
class Foo
|
16
|
+
float_accessor :float
|
17
|
+
date_accessor :date
|
18
|
+
end
|
19
|
+
|
20
|
+
>> f = Foo.new
|
21
|
+
=> #<Foo:0xb7a3dd44>
|
22
|
+
>> f.float = "1.4"
|
23
|
+
=> "1.4"
|
24
|
+
>> f.float
|
25
|
+
=> 1.4
|
26
|
+
>> f.float = "1"
|
27
|
+
=> "1"
|
28
|
+
>> f.float
|
29
|
+
=> 1.0
|
30
|
+
>> f.date = "2009-10-30"
|
31
|
+
=> "2009-10-30"
|
32
|
+
>> f.date
|
33
|
+
=> #<Date: 4910269/2,0,2299161>
|
34
|
+
>> f.date.to_s
|
35
|
+
=> "2009-10-30"
|
36
|
+
|
37
|
+
|
38
|
+
== REQUIREMENTS:
|
39
|
+
|
40
|
+
|
41
|
+
== INSTALL:
|
42
|
+
|
43
|
+
sudo gem install typed_accessors
|
44
|
+
|
45
|
+
== LICENSE:
|
46
|
+
|
47
|
+
(The MIT License)
|
48
|
+
|
49
|
+
Copyright (c) 2009-2010 Pat Ladd
|
50
|
+
Copyright (c) 2009-2010 Sean Dague
|
51
|
+
|
52
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
53
|
+
a copy of this software and associated documentation files (the
|
54
|
+
'Software'), to deal in the Software without restriction, including
|
55
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
56
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
57
|
+
permit persons to whom the Software is furnished to do so, subject to
|
58
|
+
the following conditions:
|
59
|
+
|
60
|
+
The above copyright notice and this permission notice shall be
|
61
|
+
included in all copies or substantial portions of the Software.
|
62
|
+
|
63
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
64
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
65
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
66
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
67
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
68
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
69
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
CHANGED
@@ -1,143 +1,28 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
|
3
|
-
require '
|
4
|
-
require '
|
5
|
-
require '
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
# Add our created paths to the 'rake clobber' list
|
31
|
-
CLOBBER.include(rcov_test_output)
|
32
|
-
|
33
|
-
desc 'Removes all previous unit test coverage information'
|
34
|
-
task (:reset_unit_test_coverage) do |t|
|
35
|
-
rm_rf rcov_unit_test_output
|
36
|
-
mkdir rcov_unit_test_output
|
37
|
-
end
|
38
|
-
|
39
|
-
desc 'Run all unit tests with Rcov to measure coverage'
|
40
|
-
Rake::TestTask.new(:rcov) do |t|
|
41
|
-
t.libs << "test"
|
42
|
-
t.pattern = 'test/**/*_test.rb'
|
43
|
-
t.ruby_opts << rcov_path
|
44
|
-
t.ruby_opts << "-o #{rcov_test_output}"
|
45
|
-
t.ruby_opts << "-x #{rcov_exclude}"
|
46
|
-
t.verbose = true
|
47
|
-
end
|
48
|
-
|
49
|
-
# Generate the RDoc documentation
|
50
|
-
Rake::RDocTask.new(:doc) { |rdoc|
|
51
|
-
rdoc.main = 'README'
|
52
|
-
rdoc.rdoc_files.include('lib/**/*.rb', 'README')
|
53
|
-
rdoc.rdoc_files.include('COPYING')
|
54
|
-
rdoc.rdoc_dir = 'docs/typed_accessors'
|
55
|
-
rdoc.title = "Typed Accessors"
|
56
|
-
rdoc.options << "--include=examples --line-numbers --inline-source"
|
57
|
-
}
|
58
|
-
|
59
|
-
Gem::manage_gems
|
60
|
-
require 'rake/gempackagetask'
|
61
|
-
|
62
|
-
spec = Gem::Specification.new do |s|
|
63
|
-
s.name = "typed_accessors"
|
64
|
-
s.version = PKG_VERSION
|
65
|
-
s.homepage = "http://github.com/sdague/typed_accessors"
|
66
|
-
s.platform = Gem::Platform::RUBY
|
67
|
-
s.summary = "Predefined typed accessors"
|
68
|
-
s.description = "Defines easy to use additional functions to creating typed accessors, which auto convert the attributes to non string types"
|
69
|
-
s.rubyforge_project = "http://rubyforge.org/projects/sdaguegems"
|
70
|
-
s.files = FileList["{test,lib,docs,examples}/**/*"].to_a
|
71
|
-
s.files += ["Rakefile", "README", "COPYING" ]
|
72
|
-
s.require_path = "lib"
|
73
|
-
s.autorequire = "typed_accessors"
|
74
|
-
s.has_rdoc = true
|
75
|
-
s.extra_rdoc_files = ["README", "COPYING"]
|
76
|
-
s.rdoc_options.concat ['--main', 'README']
|
77
|
-
s.rdoc_options.concat ['--accessor int_accessor="RW int" --accessor int_reader="R int" --accessor int_writer="W int" --accessor float_accessor="RW float" --accessor float_reader="R float" --accessor float_writer="W float" --accessor bool_yn_accessor="RW bool" --accessor bool_yn_reader="R bool" --accessor bool_yn_writer="W bool"']
|
78
|
-
s.author = "Sean Dague"
|
79
|
-
s.email = "sean@dague.net"
|
80
|
-
end
|
81
|
-
|
82
|
-
Rake::GemPackageTask.new(spec) do |pkg|
|
83
|
-
pkg.gem_spec = spec
|
84
|
-
pkg.need_tar = true
|
85
|
-
pkg.need_zip = true
|
86
|
-
end
|
87
|
-
|
88
|
-
desc "Publish the release files to RubyForge."
|
89
|
-
task :release => [ :package, :publish_docs ] do
|
90
|
-
require 'rubyforge'
|
91
|
-
packages = %w( gem tgz zip ).collect{ |ext| "pkg/#{PKG_NAME}-#{PKG_VERSION}.#{ext}" }
|
92
|
-
rubyforge = RubyForge.new
|
93
|
-
rubyforge.configure
|
94
|
-
rubyforge.login
|
95
|
-
rubyforge.scrape_project("sdaguegems")
|
96
|
-
rubyforge.add_release("sdaguegems", PKG_NAME, "v#{PKG_VERSION}", *packages)
|
97
|
-
end
|
98
|
-
|
99
|
-
desc 'Install the gem globally (requires sudo)'
|
100
|
-
task :install => :package do |t|
|
101
|
-
`sudo gem install pkg/typed_accessors-#{PKG_VERSION}.gem`
|
102
|
-
end
|
103
|
-
|
104
|
-
desc "Upload Docs to RubyForge"
|
105
|
-
task :publish_docs => [:doc] do
|
106
|
-
publisher = Rake::SshDirPublisher.new(
|
107
|
-
"sdague@rubyforge.org",
|
108
|
-
"/var/www/gforge-projects/sdaguegems",
|
109
|
-
"docs"
|
110
|
-
)
|
111
|
-
publisher.upload
|
112
|
-
end
|
113
|
-
|
114
|
-
# task :release => [:clobber, :verify_committed, :spec, :publish_packages, :tag, :publish_news]
|
115
|
-
|
116
|
-
desc "Verifies that there is no uncommitted code"
|
117
|
-
task :verify_committed do
|
118
|
-
IO.popen('svn stat') do |io|
|
119
|
-
io.each_line do |line|
|
120
|
-
raise "\n!!! Do a svn commit first !!!\n\n" if line =~ /^\s*modified:\s*/
|
121
|
-
end
|
122
|
-
end
|
123
|
-
end
|
124
|
-
|
125
|
-
task :lines do
|
126
|
-
lines = 0
|
127
|
-
codelines = 0
|
128
|
-
Dir.foreach("lib") { |file_name|
|
129
|
-
next unless file_name =~ /.*rb/
|
130
|
-
|
131
|
-
f = File.open("lib/" + file_name)
|
132
|
-
|
133
|
-
while line = f.gets
|
134
|
-
lines += 1
|
135
|
-
next if line =~ /^\s*$/
|
136
|
-
next if line =~ /^\s*#/
|
137
|
-
codelines += 1
|
138
|
-
end
|
139
|
-
}
|
140
|
-
puts "\n------------------------------\n"
|
141
|
-
puts "Total Lines: #{lines}"
|
142
|
-
puts "Lines of Code: #{codelines}"
|
143
|
-
end
|
1
|
+
require 'rubygems'
|
2
|
+
gem 'hoe', '>= 2.1.0'
|
3
|
+
require 'hoe'
|
4
|
+
require 'fileutils'
|
5
|
+
require './lib/typed_accessors'
|
6
|
+
|
7
|
+
ENV['VERSION'] = TypedAccessors::VERSION
|
8
|
+
|
9
|
+
Hoe.plugin :newgem
|
10
|
+
Hoe.plugin :website
|
11
|
+
# Hoe.plugin :cucumberfeatures
|
12
|
+
|
13
|
+
# Generate all the Rake tasks
|
14
|
+
# Run 'rake -T' to see list of generated tasks (from gem root directory)
|
15
|
+
$hoe = Hoe.spec 'typed_accessors' do
|
16
|
+
self.developer 'Sean Dague', 'sean@dague.net'
|
17
|
+
self.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
|
18
|
+
self.rubyforge_name = "sdaguegems" # TODO this is default value
|
19
|
+
# self.extra_deps = [['activesupport','>= 2.0.2']]
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
require 'newgem/tasks'
|
24
|
+
Dir['tasks/**/*.rake'].each { |t| load t }
|
25
|
+
|
26
|
+
# TODO - want other tests/tasks run by default? Add them to the list
|
27
|
+
# remove_task :default
|
28
|
+
# task :default => [:spec, :features]
|
data/config/website.yml
ADDED
data/lib/typed_accessors.rb
CHANGED
@@ -1,100 +1,8 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
# create new accessor methods at class eval time (so little
|
4
|
-
# performance penalty). For now, this is limitted to built in types,
|
5
|
-
# float, int, boolean, and date, though this approach could clearly be
|
6
|
-
# expanded.
|
7
|
-
#
|
8
|
-
# Example of a class definition
|
9
|
-
#
|
10
|
-
# class MyClass
|
11
|
-
# float_accessor :distance
|
12
|
-
# int_accessor :count
|
13
|
-
# bool_yn_accessor :onfire
|
14
|
-
# date_accessor :day
|
15
|
-
# end
|
1
|
+
$:.unshift(File.dirname(__FILE__)) unless
|
2
|
+
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
16
3
|
|
17
|
-
class
|
18
|
-
|
19
|
-
# Creates a boolean accessor. It will convert and incoming string
|
20
|
-
# to a true / false value. If the string is "y" or "yes" it will be
|
21
|
-
# true, otherwise false.
|
22
|
-
def bool_yn_accessor( *symbols )
|
23
|
-
attr_reader( *symbols )
|
24
|
-
bool_yn_writer( *symbols )
|
25
|
-
end
|
4
|
+
require "typed_accessors/class.rb"
|
26
5
|
|
27
|
-
|
28
|
-
|
29
|
-
# objects. Any object that has a .to_f is supported.
|
30
|
-
def float_accessor( *symbols )
|
31
|
-
attr_reader( *symbols )
|
32
|
-
float_writer( *symbols )
|
33
|
-
end
|
34
|
-
|
35
|
-
# Creates an int accessor, using built in .to_i functions on
|
36
|
-
# objects. Any object that has a .to_i is supported.
|
37
|
-
def int_accessor( *symbols )
|
38
|
-
attr_reader( *symbols )
|
39
|
-
int_writer( *symbols )
|
40
|
-
end
|
41
|
-
|
42
|
-
# Creates a data accessor using the Date parse function on
|
43
|
-
# strings. Not defined for other input types.
|
44
|
-
def date_accessor( *symbols )
|
45
|
-
attr_reader( *symbols )
|
46
|
-
date_writer( *symbols )
|
47
|
-
end
|
48
|
-
|
49
|
-
private
|
50
|
-
|
51
|
-
def date_writer( *symbols )
|
52
|
-
symbols.each do |symbol|
|
53
|
-
class_eval(<<-EOS, __FILE__, __LINE__)
|
54
|
-
def #{symbol}=(val)
|
55
|
-
if val.is_a? String
|
56
|
-
instance_variable_set("@#{symbol}", Date.parse(val))
|
57
|
-
else
|
58
|
-
instance_variable_set("@#{symbol}", val)
|
59
|
-
end
|
60
|
-
end
|
61
|
-
EOS
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
def bool_yn_writer( *symbols )
|
66
|
-
symbols.each do |symbol|
|
67
|
-
class_eval(<<-EOS, __FILE__, __LINE__)
|
68
|
-
def #{symbol}=(val)
|
69
|
-
if (val.is_a? String and val =~ /^(y(es)?|t(rue)?)$/i) or val == true then
|
70
|
-
instance_variable_set("@#{symbol}", true)
|
71
|
-
else
|
72
|
-
instance_variable_set("@#{symbol}", false)
|
73
|
-
end
|
74
|
-
end
|
75
|
-
EOS
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
def float_writer( *symbols )
|
80
|
-
symbols.each do |symbol|
|
81
|
-
class_eval(<<-EOS, __FILE__, __LINE__)
|
82
|
-
def #{symbol}=(val)
|
83
|
-
if !val.respond_to?('to_f') then raise ArgumentError, "#{symbol} must be Float" end
|
84
|
-
instance_variable_set("@#{symbol}", val.to_f)
|
85
|
-
end
|
86
|
-
EOS
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
def int_writer( *symbols )
|
91
|
-
symbols.each do |symbol|
|
92
|
-
class_eval(<<-EOS, __FILE__, __LINE__)
|
93
|
-
def #{symbol}=(val)
|
94
|
-
if !val.respond_to?('to_i') then raise ArgumentError, "#{symbol} must be Integer" end
|
95
|
-
instance_variable_set("@#{symbol}", val.to_i)
|
96
|
-
end
|
97
|
-
EOS
|
98
|
-
end
|
99
|
-
end
|
6
|
+
module TypedAccessors
|
7
|
+
VERSION = '0.3.0'
|
100
8
|
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
# Typed accessors works as a Mixin on Class. It creates a set of
|
2
|
+
# functions that are used in the class definition which eval and
|
3
|
+
# create new accessor methods at class eval time (so little
|
4
|
+
# performance penalty). For now, this is limitted to built in types,
|
5
|
+
# float, int, boolean, and date, though this approach could clearly be
|
6
|
+
# expanded.
|
7
|
+
#
|
8
|
+
# Example of a class definition
|
9
|
+
#
|
10
|
+
# class MyClass
|
11
|
+
# float_accessor :distance
|
12
|
+
# int_accessor :count
|
13
|
+
# bool_yn_accessor :onfire
|
14
|
+
# date_accessor :day
|
15
|
+
# end
|
16
|
+
|
17
|
+
class Class
|
18
|
+
|
19
|
+
# Creates a boolean accessor. It will convert and incoming string
|
20
|
+
# to a true / false value. If the string is "y" or "yes" it will be
|
21
|
+
# true, otherwise false.
|
22
|
+
def bool_yn_accessor( *symbols )
|
23
|
+
attr_reader( *symbols )
|
24
|
+
bool_yn_writer( *symbols )
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
# Creates a float accessor, using built in .to_f functions on
|
29
|
+
# objects. Any object that has a .to_f is supported.
|
30
|
+
def float_accessor( *symbols )
|
31
|
+
attr_reader( *symbols )
|
32
|
+
float_writer( *symbols )
|
33
|
+
end
|
34
|
+
|
35
|
+
# Creates an int accessor, using built in .to_i functions on
|
36
|
+
# objects. Any object that has a .to_i is supported.
|
37
|
+
def int_accessor( *symbols )
|
38
|
+
attr_reader( *symbols )
|
39
|
+
int_writer( *symbols )
|
40
|
+
end
|
41
|
+
|
42
|
+
# Creates a data accessor using the Date parse function on
|
43
|
+
# strings. Not defined for other input types.
|
44
|
+
def date_accessor( *symbols )
|
45
|
+
attr_reader( *symbols )
|
46
|
+
date_writer( *symbols )
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def date_writer( *symbols )
|
52
|
+
symbols.each do |symbol|
|
53
|
+
class_eval(<<-EOS, __FILE__, __LINE__)
|
54
|
+
def #{symbol}=(val)
|
55
|
+
if val.is_a? String
|
56
|
+
instance_variable_set("@#{symbol}", Date.parse(val))
|
57
|
+
else
|
58
|
+
instance_variable_set("@#{symbol}", val)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
EOS
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def bool_yn_writer( *symbols )
|
66
|
+
symbols.each do |symbol|
|
67
|
+
class_eval(<<-EOS, __FILE__, __LINE__)
|
68
|
+
def #{symbol}=(val)
|
69
|
+
if (val.is_a? String and val =~ /^(y(es)?|t(rue)?)$/i) or val == true then
|
70
|
+
instance_variable_set("@#{symbol}", true)
|
71
|
+
else
|
72
|
+
instance_variable_set("@#{symbol}", false)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
EOS
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def float_writer( *symbols )
|
80
|
+
symbols.each do |symbol|
|
81
|
+
class_eval(<<-EOS, __FILE__, __LINE__)
|
82
|
+
def #{symbol}=(val)
|
83
|
+
if !val.respond_to?('to_f') then raise ArgumentError, "#{symbol} must be Float" end
|
84
|
+
instance_variable_set("@#{symbol}", val.to_f)
|
85
|
+
end
|
86
|
+
EOS
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def int_writer( *symbols )
|
91
|
+
symbols.each do |symbol|
|
92
|
+
class_eval(<<-EOS, __FILE__, __LINE__)
|
93
|
+
def #{symbol}=(val)
|
94
|
+
if !val.respond_to?('to_i') then raise ArgumentError, "#{symbol} must be Integer" end
|
95
|
+
instance_variable_set("@#{symbol}", val.to_i)
|
96
|
+
end
|
97
|
+
EOS
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|