typed_accessors 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/COPYING +21 -0
  2. data/README +30 -0
  3. data/Rakefile +109 -0
  4. data/lib/typed_accessors.rb +93 -0
  5. metadata +58 -0
data/COPYING ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2009 Patrick Ladd, Sean Dague <http://dague.net>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,30 @@
1
+ This rails plugin creates a set of typed accessors for data types.
2
+ This is very handy when you are interacting with a service that
3
+ returns strings and you want them automatically turned into more
4
+ useful types.
5
+
6
+ Ex:
7
+
8
+ class Foo
9
+ float_accessor :float
10
+ date_accessor :date
11
+ end
12
+
13
+ >> f = Foo.new
14
+ => #<Foo:0xb7a3dd44>
15
+ >> f.float = "1.4"
16
+ => "1.4"
17
+ >> f.float
18
+ => 1.4
19
+ >> f.float = "1"
20
+ => "1"
21
+ >> f.float
22
+ => 1.0
23
+ >> f.date = "2009-10-30"
24
+ => "2009-10-30"
25
+ >> f.date
26
+ => #<Date: 4910269/2,0,2299161>
27
+ >> f.date.to_s
28
+ => "2009-10-30"
29
+
30
+ This work was done by Patt Ladd. Sean Dague has helped in packaging it.
data/Rakefile ADDED
@@ -0,0 +1,109 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+ require 'rake/rdoctask'
5
+ require 'rake/clean'
6
+ require 'rake/contrib/sshpublisher'
7
+
8
+ PKG_VERSION = "0.1"
9
+
10
+ $VERBOSE = nil
11
+ TEST_CHANGES_SINCE = Time.now - 600 # Recent tests = changed in last 10 minutes
12
+
13
+ desc "Run all the unit tests"
14
+ task :default => [ :test, :lines ]
15
+
16
+ desc "Run the unit tests in test"
17
+ Rake::TestTask.new(:test) { |t|
18
+ t.libs << "test"
19
+ t.test_files = FileList['test/*_test.rb', 'test/component/*_test.rb']
20
+ t.verbose = true
21
+ }
22
+
23
+ # rcov code coverage
24
+ rcov_path = '/usr/local/bin/rcov'
25
+ rcov_test_output = "./test/coverage"
26
+ rcov_exclude = "interactive.rb,read_write.rb,fixtures"
27
+
28
+ # Add our created paths to the 'rake clobber' list
29
+ CLOBBER.include(rcov_test_output)
30
+
31
+ desc 'Removes all previous unit test coverage information'
32
+ task (:reset_unit_test_coverage) do |t|
33
+ rm_rf rcov_unit_test_output
34
+ mkdir rcov_unit_test_output
35
+ end
36
+
37
+ desc 'Run all unit tests with Rcov to measure coverage'
38
+ Rake::TestTask.new(:rcov) do |t|
39
+ t.libs << "test"
40
+ t.pattern = 'test/**/*_test.rb'
41
+ t.ruby_opts << rcov_path
42
+ t.ruby_opts << "-o #{rcov_test_output}"
43
+ t.ruby_opts << "-x #{rcov_exclude}"
44
+ t.verbose = true
45
+ end
46
+
47
+ # Generate the RDoc documentation
48
+ Rake::RDocTask.new(:doc) { |rdoc|
49
+ rdoc.main = 'README'
50
+ rdoc.rdoc_files.include('lib/**/*.rb', 'README')
51
+ rdoc.rdoc_files.include('COPYING')
52
+ rdoc.rdoc_dir = 'docs/api'
53
+ rdoc.title = "Typed Accessors"
54
+ rdoc.options << "--include=examples --line-numbers --inline-source"
55
+ rdoc.options << "--accessor=ical_component,ical_property,ical_multi_property"
56
+ }
57
+
58
+ Gem::manage_gems
59
+ require 'rake/gempackagetask'
60
+
61
+ spec = Gem::Specification.new do |s|
62
+ s.name = "typed_accessors"
63
+ s.version = PKG_VERSION
64
+ s.homepage = "http://github.com/sdague/typed_accessors"
65
+ s.platform = Gem::Platform::RUBY
66
+ s.summary = "Predefined typed accessors"
67
+ s.description = "Defines easy to use additional functions to creating typed accessors, which auto convert the attributes to non string types"
68
+
69
+ s.files = FileList["{test,lib,docs,examples}/**/*"].to_a
70
+ s.files += ["Rakefile", "README", "COPYING" ]
71
+ s.require_path = "lib"
72
+ s.autorequire = "typed_accessors"
73
+ s.has_rdoc = true
74
+ s.extra_rdoc_files = ["README", "COPYING"]
75
+ s.rdoc_options.concat ['--main', 'README']
76
+
77
+ s.author = "Pat Ladd"
78
+ end
79
+
80
+ Rake::GemPackageTask.new(spec) do |pkg|
81
+ pkg.gem_spec = spec
82
+ pkg.need_tar = true
83
+ pkg.need_zip = true
84
+ end
85
+
86
+ desc 'Install the gem globally (requires sudo)'
87
+ task :install => :package do |t|
88
+ `sudo gem install pkg/typed_accessors-#{PKG_VERSION}.gem`
89
+ end
90
+
91
+ task :lines do
92
+ lines = 0
93
+ codelines = 0
94
+ Dir.foreach("lib/typed_accessors") { |file_name|
95
+ next unless file_name =~ /.*rb/
96
+
97
+ f = File.open("lib/typed_accessors/" + file_name)
98
+
99
+ while line = f.gets
100
+ lines += 1
101
+ next if line =~ /^\s*$/
102
+ next if line =~ /^\s*#/
103
+ codelines += 1
104
+ end
105
+ }
106
+ puts "\n------------------------------\n"
107
+ puts "Total Lines: #{lines}"
108
+ puts "Lines of Code: #{codelines}"
109
+ end
@@ -0,0 +1,93 @@
1
+ class Class
2
+ def bool_yn_accessor( *symbols )
3
+ attr_reader( *symbols )
4
+ bool_yn_writer( *symbols )
5
+ end
6
+
7
+ def float_accessor( *symbols )
8
+ float_reader( *symbols )
9
+ float_writer( *symbols )
10
+ end
11
+
12
+ def int_accessor( *symbols )
13
+ int_reader( *symbols )
14
+ int_writer( *symbols )
15
+ end
16
+
17
+ def date_accessor( *symbols )
18
+ attr_reader( *symbols )
19
+ date_writer( *symbols )
20
+ end
21
+
22
+ private
23
+
24
+ def date_writer( *symbols )
25
+ symbols.each do |symbol|
26
+ class_eval(<<-EOS, __FILE__, __LINE__)
27
+ def #{symbol}=(val)
28
+ if val.class.to_s == 'String'
29
+ instance_variable_set("@#{symbol}", Date.parse(val))
30
+ else
31
+ instance_variable_set("@#{symbol}", val)
32
+ end
33
+ end
34
+ EOS
35
+ end
36
+ end
37
+
38
+ def bool_yn_writer( *symbols )
39
+ symbols.each do |symbol|
40
+ class_eval(<<-EOS, __FILE__, __LINE__)
41
+ def #{symbol}=(val)
42
+ if val.upcase == 'Y' || val == true then
43
+ instance_variable_set("@#{symbol}", true)
44
+ else
45
+ instance_variable_set("@#{symbol}", false)
46
+ end
47
+ end
48
+ EOS
49
+ end
50
+ end
51
+
52
+ def float_reader( *symbols )
53
+ symbols.each do |symbol|
54
+ class_eval(<<-EOS, __FILE__, __LINE__)
55
+ def #{symbol}
56
+ instance_variable_get("@#{symbol}")
57
+ end
58
+ EOS
59
+ end
60
+ end
61
+
62
+ def float_writer( *symbols )
63
+ symbols.each do |symbol|
64
+ class_eval(<<-EOS, __FILE__, __LINE__)
65
+ def #{symbol}=(val)
66
+ if !val.respond_to?('to_f') then raise ArgumentError, "#{symbol} must be float" end
67
+ instance_variable_set("@#{symbol}", val.to_f)
68
+ end
69
+ EOS
70
+ end
71
+ end
72
+
73
+ def int_reader( *symbols )
74
+ symbols.each do |symbol|
75
+ class_eval(<<-EOS, __FILE__, __LINE__)
76
+ def #{symbol}
77
+ instance_variable_get("@#{symbol}")
78
+ end
79
+ EOS
80
+ end
81
+ end
82
+
83
+ def int_writer( *symbols )
84
+ symbols.each do |symbol|
85
+ class_eval(<<-EOS, __FILE__, __LINE__)
86
+ def #{symbol}=(val)
87
+ if !val.respond_to?('to_i') then raise ArgumentError, "#{symbol} must be int" end
88
+ instance_variable_set("@#{symbol}", val.to_i)
89
+ end
90
+ EOS
91
+ end
92
+ end
93
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: typed_accessors
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ruby
6
+ authors:
7
+ - Pat Ladd
8
+ autorequire: typed_accessors
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-26 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Defines easy to use additional functions to creating typed accessors, which auto convert the attributes to non string types
17
+ email:
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ - COPYING
25
+ files:
26
+ - lib/typed_accessors.rb
27
+ - Rakefile
28
+ - README
29
+ - COPYING
30
+ has_rdoc: true
31
+ homepage: http://github.com/sdague/typed_accessors
32
+ post_install_message:
33
+ rdoc_options:
34
+ - --main
35
+ - README
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: "0"
43
+ version:
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ requirements: []
51
+
52
+ rubyforge_project:
53
+ rubygems_version: 1.3.1
54
+ signing_key:
55
+ specification_version: 2
56
+ summary: Predefined typed accessors
57
+ test_files: []
58
+