typed_accessors 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,19 +1,46 @@
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
+
1
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.
2
22
  def bool_yn_accessor( *symbols )
3
23
  attr_reader( *symbols )
4
24
  bool_yn_writer( *symbols )
5
25
  end
6
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.
7
30
  def float_accessor( *symbols )
8
- float_reader( *symbols )
31
+ attr_reader( *symbols )
9
32
  float_writer( *symbols )
10
33
  end
11
34
 
35
+ # Creates an int accessor, using built in .to_i functions on
36
+ # objects. Any object that has a .to_i is supported.
12
37
  def int_accessor( *symbols )
13
- int_reader( *symbols )
38
+ attr_reader( *symbols )
14
39
  int_writer( *symbols )
15
40
  end
16
41
 
42
+ # Creates a data accessor using the Date parse function on
43
+ # strings. Not defined for other input types.
17
44
  def date_accessor( *symbols )
18
45
  attr_reader( *symbols )
19
46
  date_writer( *symbols )
@@ -25,7 +52,7 @@ class Class
25
52
  symbols.each do |symbol|
26
53
  class_eval(<<-EOS, __FILE__, __LINE__)
27
54
  def #{symbol}=(val)
28
- if val.class.to_s == 'String'
55
+ if val.is_a? String
29
56
  instance_variable_set("@#{symbol}", Date.parse(val))
30
57
  else
31
58
  instance_variable_set("@#{symbol}", val)
@@ -39,7 +66,7 @@ class Class
39
66
  symbols.each do |symbol|
40
67
  class_eval(<<-EOS, __FILE__, __LINE__)
41
68
  def #{symbol}=(val)
42
- if val.upcase == 'Y' || val == true then
69
+ if (val.is_a? String and val =~ /^(y(es)?|t(rue)?)$/i) or val == true then
43
70
  instance_variable_set("@#{symbol}", true)
44
71
  else
45
72
  instance_variable_set("@#{symbol}", false)
@@ -49,42 +76,22 @@ class Class
49
76
  end
50
77
  end
51
78
 
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
79
  def float_writer( *symbols )
63
80
  symbols.each do |symbol|
64
81
  class_eval(<<-EOS, __FILE__, __LINE__)
65
82
  def #{symbol}=(val)
66
- if !val.respond_to?('to_f') then raise ArgumentError, "#{symbol} must be float" end
83
+ if !val.respond_to?('to_f') then raise ArgumentError, "#{symbol} must be Float" end
67
84
  instance_variable_set("@#{symbol}", val.to_f)
68
85
  end
69
86
  EOS
70
87
  end
71
88
  end
72
89
 
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
90
  def int_writer( *symbols )
84
91
  symbols.each do |symbol|
85
92
  class_eval(<<-EOS, __FILE__, __LINE__)
86
93
  def #{symbol}=(val)
87
- if !val.respond_to?('to_i') then raise ArgumentError, "#{symbol} must be int" end
94
+ if !val.respond_to?('to_i') then raise ArgumentError, "#{symbol} must be Integer" end
88
95
  instance_variable_set("@#{symbol}", val.to_i)
89
96
  end
90
97
  EOS
@@ -0,0 +1,118 @@
1
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
2
+
3
+ require 'test/unit'
4
+ require 'date'
5
+ require 'typed_accessors'
6
+
7
+ class TypedExample
8
+ float_accessor :dollars
9
+ int_accessor :count
10
+ bool_yn_accessor :onfire
11
+ date_accessor :day
12
+ end
13
+
14
+ class TestTypedAccessors < Test::Unit::TestCase
15
+
16
+ def test_date
17
+ t = TypedExample.new
18
+ t.day = Date.today
19
+ assert_instance_of Date, t.day
20
+ assert_equal Date.today, t.day
21
+
22
+ t.day = Date.today.strftime("%Y-%m-%d")
23
+ assert_instance_of Date, t.day
24
+ assert_equal Date.today, t.day
25
+
26
+ # this might fail depending on locale, need to figure out a
27
+ # better way to handle that
28
+ t.day = Date.today.strftime("%m/%d/%Y")
29
+ assert_instance_of Date, t.day
30
+ assert_equal Date.today, t.day
31
+
32
+ assert_raise ArgumentError do
33
+ t.day = Date.today.strftime("foo")
34
+ end
35
+ end
36
+
37
+ def test_float
38
+ t = TypedExample.new
39
+
40
+ t.dollars = "54"
41
+ assert_instance_of Float, t.dollars
42
+ assert_equal 54.0, t.dollars
43
+
44
+ t.dollars = "54.200000"
45
+ assert_instance_of Float, t.dollars
46
+ assert_equal 54.2, t.dollars
47
+
48
+ t.dollars = 54
49
+ assert_instance_of Float, t.dollars
50
+ assert_equal 54.0, t.dollars
51
+
52
+ t.dollars = "foo"
53
+ assert_instance_of Float, t.dollars
54
+ assert_equal 0.0, t.dollars
55
+
56
+ end
57
+
58
+ def test_int
59
+ t = TypedExample.new
60
+
61
+ t.count = "54"
62
+ assert_instance_of Fixnum, t.count
63
+ assert_equal 54, t.count
64
+
65
+ t.count = "54.200000"
66
+ assert_instance_of Fixnum, t.count
67
+ assert_equal 54, t.count
68
+
69
+ t.count = 54.0
70
+ assert_instance_of Fixnum, t.count
71
+ assert_equal 54, t.count
72
+ end
73
+
74
+ def test_bool
75
+ t = TypedExample.new
76
+
77
+ t.onfire = "Y"
78
+ assert_instance_of TrueClass, t.onfire
79
+ assert_equal true, t.onfire
80
+
81
+ t.onfire = "54.200000"
82
+ assert_instance_of FalseClass, t.onfire
83
+ assert_equal false, t.onfire
84
+
85
+ t.onfire = "y"
86
+ assert_instance_of TrueClass, t.onfire
87
+ assert_equal true, t.onfire
88
+
89
+ t.onfire = "yes"
90
+ assert_instance_of TrueClass, t.onfire
91
+ assert_equal true, t.onfire
92
+
93
+ t.onfire = "yessir"
94
+ assert_instance_of FalseClass, t.onfire
95
+ assert_equal false, t.onfire
96
+
97
+ t.onfire = "t"
98
+ assert_instance_of TrueClass, t.onfire
99
+ assert_equal true, t.onfire
100
+
101
+ t.onfire = "TRUE"
102
+ assert_instance_of TrueClass, t.onfire
103
+ assert_equal true, t.onfire
104
+
105
+ t.onfire = "truth"
106
+ assert_instance_of FalseClass, t.onfire
107
+ assert_equal false, t.onfire
108
+
109
+ t.onfire = false
110
+ assert_instance_of FalseClass, t.onfire
111
+ assert_equal false, t.onfire
112
+
113
+ t.onfire = true
114
+ assert_instance_of TrueClass, t.onfire
115
+ assert_equal true, t.onfire
116
+ end
117
+ end
118
+
metadata CHANGED
@@ -1,20 +1,20 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: typed_accessors
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.1"
4
+ version: "0.2"
5
5
  platform: ruby
6
6
  authors:
7
- - Pat Ladd
7
+ - Sean Dague
8
8
  autorequire: typed_accessors
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-03-26 00:00:00 -04:00
12
+ date: 2009-03-29 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
16
16
  description: Defines easy to use additional functions to creating typed accessors, which auto convert the attributes to non string types
17
- email:
17
+ email: sean@dague.net
18
18
  executables: []
19
19
 
20
20
  extensions: []
@@ -23,7 +23,22 @@ extra_rdoc_files:
23
23
  - README
24
24
  - COPYING
25
25
  files:
26
+ - test/basic_test.rb
26
27
  - lib/typed_accessors.rb
28
+ - docs/typed_accessors
29
+ - docs/typed_accessors/created.rid
30
+ - docs/typed_accessors/rdoc-style.css
31
+ - docs/typed_accessors/classes
32
+ - docs/typed_accessors/classes/Class.html
33
+ - docs/typed_accessors/fr_method_index.html
34
+ - docs/typed_accessors/fr_class_index.html
35
+ - docs/typed_accessors/fr_file_index.html
36
+ - docs/typed_accessors/index.html
37
+ - docs/typed_accessors/files
38
+ - docs/typed_accessors/files/COPYING.html
39
+ - docs/typed_accessors/files/README.html
40
+ - docs/typed_accessors/files/lib
41
+ - docs/typed_accessors/files/lib/typed_accessors_rb.html
27
42
  - Rakefile
28
43
  - README
29
44
  - COPYING
@@ -33,6 +48,7 @@ post_install_message:
33
48
  rdoc_options:
34
49
  - --main
35
50
  - README
51
+ - --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"
36
52
  require_paths:
37
53
  - lib
38
54
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -49,7 +65,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
49
65
  version:
50
66
  requirements: []
51
67
 
52
- rubyforge_project:
68
+ rubyforge_project: http://rubyforge.org/projects/sdaguegems
53
69
  rubygems_version: 1.3.1
54
70
  signing_key:
55
71
  specification_version: 2