conversion 0.1.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/CHANGELOG +0 -0
- data/README +3 -0
- data/Rakefile +85 -0
- data/lib/conversion/accessors.rb +137 -0
- data/lib/conversion/class_decoration.rb +107 -0
- data/lib/conversion/core.rb +336 -0
- data/lib/conversion/mode/human_input/class_decoration.rb +31 -0
- data/lib/conversion/mode/human_input/core.rb +74 -0
- data/lib/conversion/mode/human_input.rb +2 -0
- data/lib/conversion/mode/nil_on_failure.rb +33 -0
- data/lib/conversion/mode/stable_nil.rb +19 -0
- data/lib/conversion/mode/strong_type_checking.rb +24 -0
- data/lib/conversion/mode/weak.rb +33 -0
- data/lib/conversion/object_decoration.rb +23 -0
- data/lib/conversion/version.rb +9 -0
- data/lib/conversion.rb +22 -0
- data/lib/conversion_test.rb +336 -0
- data/test/conversion_test.rb +11 -0
- data/test/test_helper.rb +2 -0
- metadata +77 -0
@@ -0,0 +1,74 @@
|
|
1
|
+
module Conversion
|
2
|
+
class << self
|
3
|
+
# You're not supposed to call this method. Use Conversion.converter or Conversion.entries_converter instead.
|
4
|
+
#
|
5
|
+
# This method implements the :human_input conversion mode.
|
6
|
+
def human_input_converter(target)
|
7
|
+
base_converter = converter(target, :mode=>:weak)
|
8
|
+
|
9
|
+
return base_converter unless
|
10
|
+
base_converter &&
|
11
|
+
target.is_a?(Class)
|
12
|
+
|
13
|
+
proc do |value|
|
14
|
+
value = value.convert_to(Conversion::HumanInputString)
|
15
|
+
if value.nil? || value.empty?
|
16
|
+
nil
|
17
|
+
else
|
18
|
+
value = value.gsub(',','.').gsub(/ /,'') if value =~ /^[-+]?[ \d]*[\.,]?\d+$/
|
19
|
+
base_converter.call(value)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# This module provides conversion to clean, stripped, strings.
|
26
|
+
#
|
27
|
+
# <object>.convert_to(Conversion::HumanInputString)
|
28
|
+
#
|
29
|
+
# See Conversion::HumanInputString.to_converter_proc
|
30
|
+
module HumanInputString
|
31
|
+
class << self
|
32
|
+
# Returns a Proc that converts to a clean, stripped, string.
|
33
|
+
#
|
34
|
+
# Conversion::HumanInputString.to_converter_proc.call("\n1 2\n3 ")
|
35
|
+
# # => "1 2\n3"
|
36
|
+
#
|
37
|
+
# Blank objects are converted to nil:
|
38
|
+
#
|
39
|
+
# Conversion::HumanInputString.to_converter_proc.call("\n ")
|
40
|
+
# # => nil
|
41
|
+
#
|
42
|
+
# If the converted object has a :to_human_input_string method, it is used before cleaning, stripping and blank-checking. (See Date#to_human_input_string)
|
43
|
+
#
|
44
|
+
# class Date
|
45
|
+
# def to_human_input_string() ... end
|
46
|
+
# end
|
47
|
+
# Conversion::HumanInputString.to_converter_proc.call(Date.now)
|
48
|
+
# # => "10/22/2006"
|
49
|
+
#
|
50
|
+
# See Conversion.converter, Object#convert_to
|
51
|
+
#
|
52
|
+
# NB: if your object has a :to_human_input_string, <b>make yourself sure</b> that:
|
53
|
+
# object.convert_to(Conversion::HumanInputString).convert_to(object.class, :mode=>:human_input) == object
|
54
|
+
|
55
|
+
def to_converter_proc
|
56
|
+
proc do |value|
|
57
|
+
begin
|
58
|
+
value = value.to_human_input_string
|
59
|
+
rescue NoMethodError
|
60
|
+
value = value.to_s
|
61
|
+
end
|
62
|
+
# compact spaces and non-breaking-spaces runs
|
63
|
+
value = value.gsub(/[ ]+/,' ').strip unless value.nil?
|
64
|
+
if value.nil? || value.empty?
|
65
|
+
nil
|
66
|
+
else
|
67
|
+
value
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Conversion
|
2
|
+
class << self
|
3
|
+
# You're not supposed to call this method. Use Conversion.converter or Conversion.entries_converter instead.
|
4
|
+
#
|
5
|
+
# This method implements the :nil_on_failure conversion mode.
|
6
|
+
def nil_on_failure_converter(target)
|
7
|
+
base_converter = converter(target)
|
8
|
+
|
9
|
+
return nil unless base_converter
|
10
|
+
|
11
|
+
if target.is_a?(Class)
|
12
|
+
proc do |value|
|
13
|
+
begin
|
14
|
+
result = base_converter.call(value)
|
15
|
+
result = nil unless result.is_a?(target)
|
16
|
+
rescue Exception
|
17
|
+
result = nil
|
18
|
+
end
|
19
|
+
result
|
20
|
+
end
|
21
|
+
else
|
22
|
+
proc do |value|
|
23
|
+
begin
|
24
|
+
base_converter.call(value)
|
25
|
+
rescue Exception
|
26
|
+
nil
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Conversion
|
2
|
+
class << self
|
3
|
+
# You're not supposed to call this method. Use Conversion.converter or Conversion.entries_converter instead.
|
4
|
+
#
|
5
|
+
# This method implements the :stable_nil conversion mode.
|
6
|
+
def stable_nil_converter(target)
|
7
|
+
base_converter = converter(target)
|
8
|
+
return nil unless base_converter
|
9
|
+
proc do |value|
|
10
|
+
if value.nil?
|
11
|
+
nil
|
12
|
+
else
|
13
|
+
base_converter.call(value)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Conversion
|
2
|
+
class << self
|
3
|
+
# You're not supposed to call this method. Use Conversion.converter or Conversion.entries_converter instead.
|
4
|
+
#
|
5
|
+
# This method implements the :strong_type_checking conversion mode.
|
6
|
+
def strong_type_checking_converter(target)
|
7
|
+
base_converter = converter(target)
|
8
|
+
|
9
|
+
return base_converter unless
|
10
|
+
base_converter &&
|
11
|
+
target.is_a?(Class)
|
12
|
+
|
13
|
+
proc do |value|
|
14
|
+
if value.nil?
|
15
|
+
nil
|
16
|
+
else
|
17
|
+
raise ArgumentError.new("Invalid value for #{target}: #{value.inspect}") unless value.is_a?(target)
|
18
|
+
value
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Conversion
|
2
|
+
class << self
|
3
|
+
# You're not supposed to call this method. Use Conversion.converter or Conversion.entries_converter instead.
|
4
|
+
#
|
5
|
+
# This method implements the :weak conversion mode.
|
6
|
+
def weak_converter(target)
|
7
|
+
base_converter = converter(target)
|
8
|
+
|
9
|
+
return nil unless base_converter
|
10
|
+
|
11
|
+
if target.is_a?(Class)
|
12
|
+
proc do |value|
|
13
|
+
begin
|
14
|
+
result = base_converter.call(value)
|
15
|
+
result = value unless result.is_a?(target)
|
16
|
+
rescue Exception
|
17
|
+
result = value
|
18
|
+
end
|
19
|
+
result
|
20
|
+
end
|
21
|
+
else
|
22
|
+
proc do |value|
|
23
|
+
begin
|
24
|
+
base_converter.call(value)
|
25
|
+
rescue Exception
|
26
|
+
value
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class Object
|
2
|
+
# Converts self to target, with options
|
3
|
+
#
|
4
|
+
# '1'.convert_to(Integer)
|
5
|
+
# # => 1
|
6
|
+
#
|
7
|
+
# See Conversion.converter for options description
|
8
|
+
def convert_to(target, options={})
|
9
|
+
Conversion.converter(target, options).call(self)
|
10
|
+
end
|
11
|
+
|
12
|
+
# Converts self to target, with options and enumarable support
|
13
|
+
#
|
14
|
+
# ['1', ['2']].convert_entries_to(Integer)
|
15
|
+
# # => [1, [2]]
|
16
|
+
# '1'.convert_entries_to(Integer)
|
17
|
+
# # => 1
|
18
|
+
#
|
19
|
+
# See Conversion.entries_converter for options description
|
20
|
+
def convert_entries_to(target, options={})
|
21
|
+
Conversion.entries_converter(target, options).call(self)
|
22
|
+
end
|
23
|
+
end
|
data/lib/conversion.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__))
|
2
|
+
|
3
|
+
# Conversion.converter(<target>)
|
4
|
+
require "conversion/core"
|
5
|
+
|
6
|
+
# <Object>.convert_to(<target>)
|
7
|
+
require "conversion/object_decoration"
|
8
|
+
|
9
|
+
# class A; include Conversion::Accessors; attr_accessor <attr>, :store_as => <target>; end
|
10
|
+
require "conversion/accessors"
|
11
|
+
|
12
|
+
# Symbol, Strings, Integer, Float, etc.
|
13
|
+
require "conversion/class_decoration"
|
14
|
+
|
15
|
+
# conversion modes
|
16
|
+
require "conversion/mode/weak"
|
17
|
+
require "conversion/mode/strong_type_checking"
|
18
|
+
require "conversion/mode/stable_nil"
|
19
|
+
require "conversion/mode/nil_on_failure"
|
20
|
+
require "conversion/mode/human_input"
|
21
|
+
|
22
|
+
$:.shift
|
@@ -0,0 +1,336 @@
|
|
1
|
+
require 'conversion'
|
2
|
+
require 'date'
|
3
|
+
|
4
|
+
$assertions = 0
|
5
|
+
def assert(condition)
|
6
|
+
raise "Assertion failed" unless condition
|
7
|
+
$assertions += 1
|
8
|
+
end
|
9
|
+
|
10
|
+
def assert_exception(klass=Exception)
|
11
|
+
begin
|
12
|
+
yield
|
13
|
+
rescue klass
|
14
|
+
$assertions += 1
|
15
|
+
rescue Exception
|
16
|
+
raise
|
17
|
+
else
|
18
|
+
raise "Assertion failed"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# -----------------
|
23
|
+
|
24
|
+
class Money
|
25
|
+
include Conversion::Accessors
|
26
|
+
attr_accessor :amount, :store_as => Float
|
27
|
+
attr_accessor :currency, :store_as => Symbol
|
28
|
+
def initialize(amount, currency=:euro)
|
29
|
+
self.amount = amount
|
30
|
+
self.currency = currency
|
31
|
+
end
|
32
|
+
def to_f() amount.to_f end
|
33
|
+
def ==(other) currency == other.currency && (amount - other.amount).abs/[amount.abs, other.amount.abs].max < 1e-10 end
|
34
|
+
def to_s() amount.to_s end
|
35
|
+
end
|
36
|
+
|
37
|
+
class Date
|
38
|
+
class << self
|
39
|
+
def to_french_human_input_converter_proc
|
40
|
+
proc { |value|
|
41
|
+
value = value.convert_to(Conversion::HumanInputString)
|
42
|
+
if value.nil?
|
43
|
+
nil
|
44
|
+
else
|
45
|
+
# "d/m/y" => date(y,m,d)
|
46
|
+
elements = value.split(/[-\/]/).convert_entries_to(Integer)
|
47
|
+
begin
|
48
|
+
Date.civil(elements[2], elements[1], elements[0])
|
49
|
+
rescue
|
50
|
+
value
|
51
|
+
end
|
52
|
+
end
|
53
|
+
}
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
class Test
|
59
|
+
include Conversion::Accessors
|
60
|
+
|
61
|
+
attr_reader :base1
|
62
|
+
attr_writer :base1, :store_as => Conversion.converter(Integer)
|
63
|
+
attr_accessor :weak1, :store_as => Integer, :store_mode=>:weak
|
64
|
+
|
65
|
+
store_attributes_with_mode :weak
|
66
|
+
|
67
|
+
attr_accessor :weak2, :store_as => Integer
|
68
|
+
attr_accessor :base2, :store_as => Integer, :store_mode=>:base
|
69
|
+
|
70
|
+
store_enumerable_attributes
|
71
|
+
|
72
|
+
attr_accessor :weak_enumerable1, :store_as => Integer
|
73
|
+
attr_accessor :base_enumerable1, :store_as => Integer, :store_mode=>:base
|
74
|
+
attr_accessor :weak_enumerable2, :store_as => Conversion.converter(Integer, :mode=>:base)
|
75
|
+
|
76
|
+
store_enumerable_attributes false
|
77
|
+
|
78
|
+
attr_accessor :base4, :store_as => Integer, :store_mode=>:base
|
79
|
+
attr_accessor :weak3, :store_as => Conversion.converter(Integer, :mode=>:base)
|
80
|
+
|
81
|
+
store_attributes_with_mode nil
|
82
|
+
|
83
|
+
attr_accessor :base5, :store_as => Integer
|
84
|
+
attr_accessor :base_enumerable3, :store_as => Conversion.entries_converter(Integer)
|
85
|
+
|
86
|
+
store_attributes_with_mode :weak
|
87
|
+
|
88
|
+
attr_accessor :weak_enumerable3, :store_as => Conversion.entries_converter(Integer)
|
89
|
+
end
|
90
|
+
|
91
|
+
test=Test.new
|
92
|
+
symbol=:hello
|
93
|
+
string=:hello.to_s
|
94
|
+
fixnum=:hello.to_i
|
95
|
+
bignum=12345678*12345678
|
96
|
+
bigbignum=1234567890*1234567890
|
97
|
+
float=fixnum+0.5
|
98
|
+
bigfloat=bignum+0.5
|
99
|
+
euro = Money.new(bigfloat, :euro)
|
100
|
+
dollar = Money.new(float, :dollar)
|
101
|
+
numbers = [[fixnum, float], [bignum, bigfloat]]
|
102
|
+
integers = [[fixnum, fixnum+1], [bignum, bignum+1]]
|
103
|
+
hash=Hash.new(fixnum)
|
104
|
+
date = Date.civil(1973,9,18)
|
105
|
+
objects = [nil, date, test, hash, symbol, string, fixnum, bignum, bigbignum, integers, numbers, float, euro, dollar]
|
106
|
+
flat_objects = [nil, symbol, string, fixnum, bignum, bigbignum, float, date, euro]
|
107
|
+
|
108
|
+
# tests are based on these assertions
|
109
|
+
assert(bignum.to_f.to_i == bignum)
|
110
|
+
assert(bigbignum.to_f.to_i != bigbignum)
|
111
|
+
assert(float.to_i == fixnum)
|
112
|
+
assert(bigfloat.to_i == bignum)
|
113
|
+
assert(fixnum.to_sym == symbol)
|
114
|
+
assert(symbol.to_s == string)
|
115
|
+
assert(euro.to_f == bigfloat)
|
116
|
+
assert(dollar.to_f == float)
|
117
|
+
|
118
|
+
|
119
|
+
# Core
|
120
|
+
# ----
|
121
|
+
|
122
|
+
# object that can't be used as converters
|
123
|
+
assert_exception(ArgumentError) { Conversion.converter(test).call(1) }
|
124
|
+
assert_exception(ArgumentError) { Conversion.converter(nil).call(1) }
|
125
|
+
assert_exception(ArgumentError) { Conversion.converter(fixnum).call(1) }
|
126
|
+
|
127
|
+
# to_proc < to_converter_proc
|
128
|
+
def test.to_proc() proc { |x| 'to_proc'} end
|
129
|
+
assert(Conversion.converter(test).call(nil) == 'to_proc')
|
130
|
+
def test.to_converter_proc() proc { |x| 'to_converter_proc'} end
|
131
|
+
assert(Conversion.converter(test).call(nil) == 'to_converter_proc')
|
132
|
+
|
133
|
+
# string conversion
|
134
|
+
assert_exception(TypeError) { Conversion.converter('abc').call(1) }
|
135
|
+
|
136
|
+
# symbol conversion
|
137
|
+
objects.each { |x| assert(Conversion.converter(:inspect).call(x) == x.inspect) }
|
138
|
+
|
139
|
+
# Symbol conversion
|
140
|
+
assert_exception { Conversion.converter(Symbol).call(nil) }
|
141
|
+
a = Conversion.converter(Integer).call(symbol)
|
142
|
+
assert(Conversion.converter(Symbol).call(a) == symbol)
|
143
|
+
assert(Conversion.converter(Symbol).call(symbol) == symbol)
|
144
|
+
assert(Conversion.converter(Symbol).call(string) == symbol)
|
145
|
+
assert_exception { Conversion.converter(Symbol).call(float) }
|
146
|
+
|
147
|
+
# String conversion
|
148
|
+
assert(Conversion.converter(String).call(nil) == '')
|
149
|
+
assert(Conversion.converter(String).call(symbol) == string)
|
150
|
+
|
151
|
+
# Integer conversion
|
152
|
+
assert(Conversion.converter(Fixnum).call(nil) == 0)
|
153
|
+
assert(Conversion.converter(Fixnum).call(fixnum.to_s) == fixnum)
|
154
|
+
assert(Conversion.converter(Fixnum).call(bignum.to_s) == bignum)
|
155
|
+
assert(Conversion.converter(Fixnum).call(bigbignum.to_s) == bigbignum)
|
156
|
+
assert(Conversion.converter(Fixnum).call(float) == fixnum+1)
|
157
|
+
assert(Conversion.converter(Fixnum).call(euro) == bignum+1)
|
158
|
+
assert(Conversion.converter(Bignum).call(nil) == 0)
|
159
|
+
assert(Conversion.converter(Bignum).call(fixnum.to_s) == fixnum)
|
160
|
+
assert(Conversion.converter(Bignum).call(bignum.to_s) == bignum)
|
161
|
+
assert(Conversion.converter(Bignum).call(bigbignum.to_s) == bigbignum)
|
162
|
+
assert(Conversion.converter(Bignum).call(float) == fixnum+1)
|
163
|
+
assert(Conversion.converter(Bignum).call(euro) == bignum+1)
|
164
|
+
assert(Conversion.converter(Integer).call(nil) == 0)
|
165
|
+
assert(Conversion.converter(Integer).call(fixnum.to_s) == fixnum)
|
166
|
+
assert(Conversion.converter(Integer).call(bignum.to_s) == bignum)
|
167
|
+
assert(Conversion.converter(Integer).call(bigbignum.to_s) == bigbignum)
|
168
|
+
assert(Conversion.converter(Integer).call(float) == fixnum+1)
|
169
|
+
assert(Conversion.converter(Integer).call(euro) == bignum+1)
|
170
|
+
assert_exception { Conversion.converter(Integer).call(string) }
|
171
|
+
|
172
|
+
# Float conversion
|
173
|
+
assert(Conversion.converter(Float).call(float.to_s) == float)
|
174
|
+
assert(Conversion.converter(Float).call(euro) == bigfloat)
|
175
|
+
assert_exception { Conversion.converter(Float).call(string) }
|
176
|
+
|
177
|
+
# Nil conversion
|
178
|
+
objects.each { |x| assert(Conversion.converter(NilClass).call(x) == nil) }
|
179
|
+
|
180
|
+
# Money conversion
|
181
|
+
assert(Conversion.converter(Money).call(bigfloat, :euro) == euro)
|
182
|
+
assert(Conversion.converter(Money).call(bigfloat) == euro)
|
183
|
+
assert(Conversion.converter(Money).call(float, :dollar) == dollar)
|
184
|
+
assert(Conversion.converter(Money).call(float.to_s, 'dollar') == dollar)
|
185
|
+
|
186
|
+
# other classes conversion
|
187
|
+
assert(Conversion.converter(Array).call(1) == [nil])
|
188
|
+
assert(Conversion.converter(Hash).call(fixnum)[string] == fixnum)
|
189
|
+
|
190
|
+
# enumerable conversion
|
191
|
+
assert_exception { Conversion.converter(Integer).call(numbers) }
|
192
|
+
assert(Conversion.entries_converter(Integer).call(numbers.flatten) == integers.flatten)
|
193
|
+
assert(Conversion.entries_converter(Integer).call(numbers) == integers)
|
194
|
+
hash[symbol] = bignum.to_s
|
195
|
+
assert(Conversion.entries_converter(Integer).call(hash)[symbol] == bignum)
|
196
|
+
assert(Conversion.entries_converter(Integer).call(hash)[euro] == fixnum)
|
197
|
+
|
198
|
+
# Class stability
|
199
|
+
objects.each { |x| assert(Conversion.converter(x.class).call(x).equal?(x)) }
|
200
|
+
|
201
|
+
|
202
|
+
# Object decoration
|
203
|
+
# -----------------
|
204
|
+
|
205
|
+
assert(float.convert_to(Integer) == fixnum+1)
|
206
|
+
|
207
|
+
# Conversion mode
|
208
|
+
# ---------------
|
209
|
+
|
210
|
+
assert_exception(ArgumentError) { nil.convert_to(Float, :mode=>:foo) }
|
211
|
+
|
212
|
+
# stable nil
|
213
|
+
# ----------
|
214
|
+
|
215
|
+
assert(nil.convert_to(Integer) == 0)
|
216
|
+
assert(nil.convert_to(Integer, :mode=>:stable_nil).nil?)
|
217
|
+
assert(nil.convert_to(String) == '')
|
218
|
+
assert(nil.convert_to(String, :mode=>:stable_nil).nil?)
|
219
|
+
|
220
|
+
# nil on failure
|
221
|
+
|
222
|
+
#assert(string.convert_to(Integer, :mode=>:nil_on_failure).nil?)
|
223
|
+
assert(fixnum.convert_to(Bignum, :mode=>:nil_on_failure).nil?)
|
224
|
+
assert(bignum.convert_to(Fixnum, :mode=>:nil_on_failure).nil?)
|
225
|
+
|
226
|
+
# weak type checking
|
227
|
+
# ------------------
|
228
|
+
|
229
|
+
assert_exception { Conversion.converter(Float).call(string) }
|
230
|
+
assert(nil.convert_to(Float, :mode=>:weak) == nil)
|
231
|
+
assert(string.convert_to(Float, :mode=>:weak) == string)
|
232
|
+
assert(float.to_s.convert_to(Float, :mode=>:weak) == float)
|
233
|
+
assert([1, '2', 'a'].convert_entries_to(Integer, :mode=>:weak) == [1, 2, 'a'])
|
234
|
+
|
235
|
+
# type checking before
|
236
|
+
# --------------------
|
237
|
+
|
238
|
+
assert(nil.convert_to(Float, :mode=>:strong_type_checking) == nil)
|
239
|
+
assert(float.convert_to(Float, :mode=>:strong_type_checking) == float)
|
240
|
+
assert_exception(ArgumentError) { float.to_s.convert_to(Float, :mode=>:strong_type_checking) }
|
241
|
+
assert_exception(ArgumentError) { [1, '2', 'a'].convert_entries_to(Integer, :mode=>:strong_type_checking) }
|
242
|
+
|
243
|
+
# human input
|
244
|
+
# -----------
|
245
|
+
|
246
|
+
assert(''.convert_to(String, :mode=>:human_input)==nil)
|
247
|
+
assert(''.convert_to(Integer, :mode=>:human_input)==nil)
|
248
|
+
assert(' '.convert_to(String, :mode=>:human_input)==nil)
|
249
|
+
assert(' '.convert_to(Integer, :mode=>:human_input)==nil)
|
250
|
+
assert("\t".convert_to(String, :mode=>:human_input)==nil)
|
251
|
+
assert("\t".convert_to(Integer, :mode=>:human_input)==nil)
|
252
|
+
assert("\n".convert_to(String, :mode=>:human_input)==nil)
|
253
|
+
assert("\n".convert_to(Integer, :mode=>:human_input)==nil)
|
254
|
+
assert(' '.convert_to(String, :mode=>:human_input)==nil) # non-breaking space
|
255
|
+
assert(' '.convert_to(Integer, :mode=>:human_input)==nil)
|
256
|
+
|
257
|
+
assert(' 1 234.5 '.convert_to(String, :mode=>:human_input)=='1234.5')
|
258
|
+
assert(' 1 234.5 '.convert_to(Float, :mode=>:human_input)==1234.5)
|
259
|
+
assert(' 1 234,5 '.convert_to(Integer, :mode=>:human_input)==1235)
|
260
|
+
assert(' 1 234, 5 '.convert_to(Integer, :mode=>:human_input)=='1 234, 5')
|
261
|
+
assert(' 1 234.5 ,6 '.convert_to(Integer, :mode=>:human_input)=='1 234.5 ,6')
|
262
|
+
assert([1, '2', 'a'].convert_entries_to(Integer, :mode=>:human_input) == [1, 2, 'a'])
|
263
|
+
|
264
|
+
assert(' '.convert_to(Date, :mode=>:human_input) == nil)
|
265
|
+
assert('18/9/1973'.convert_to(Date, :mode=>:french_human_input) == date)
|
266
|
+
|
267
|
+
flat_objects.each { |x| assert(Conversion.converter(x.class, :mode=>:human_input).call(x.convert_to(Conversion::HumanInputString)) == x) }
|
268
|
+
|
269
|
+
# Accessor modes
|
270
|
+
# --------------
|
271
|
+
|
272
|
+
test.base1 = fixnum.to_s; assert(test.base1 == fixnum)
|
273
|
+
assert_exception { test.base1 = string }
|
274
|
+
assert_exception { test.base1 = [fixnum.to_s] }
|
275
|
+
assert_exception { test.base1 = [string] }
|
276
|
+
|
277
|
+
test.base2 = fixnum.to_s; assert(test.base2 == fixnum)
|
278
|
+
assert_exception { test.base2 = string }
|
279
|
+
assert_exception { test.base2 = [fixnum.to_s] }
|
280
|
+
assert_exception { test.base2 = [string] }
|
281
|
+
|
282
|
+
test.base4 = fixnum.to_s; assert(test.base4 == fixnum)
|
283
|
+
assert_exception { test.base4 = string }
|
284
|
+
assert_exception { test.base4 = [fixnum.to_s] }
|
285
|
+
assert_exception { test.base4 = [string] }
|
286
|
+
|
287
|
+
test.base5 = fixnum.to_s; assert(test.base5 == fixnum)
|
288
|
+
assert_exception { test.base5 = string }
|
289
|
+
assert_exception { test.base5 = [fixnum.to_s] }
|
290
|
+
assert_exception { test.base5 = [string] }
|
291
|
+
|
292
|
+
test.weak1 = fixnum.to_s; assert(test.weak1 == fixnum)
|
293
|
+
test.weak1 = string; assert(test.weak1 == string)
|
294
|
+
test.weak1 = [fixnum.to_s]; assert(test.weak1 == [fixnum.to_s])
|
295
|
+
test.weak1 = [string]; assert(test.weak1 == [string])
|
296
|
+
|
297
|
+
test.weak2 = fixnum.to_s; assert(test.weak2 == fixnum)
|
298
|
+
test.weak2 = string; assert(test.weak2 == string)
|
299
|
+
test.weak2 = [fixnum.to_s]; assert(test.weak2 == [fixnum.to_s])
|
300
|
+
test.weak2 = [string]; assert(test.weak2 == [string])
|
301
|
+
|
302
|
+
test.weak3 = fixnum.to_s; assert(test.weak3 == fixnum)
|
303
|
+
test.weak3 = string; assert(test.weak3 == string)
|
304
|
+
test.weak3 = [fixnum.to_s]; assert(test.weak3 == [fixnum.to_s])
|
305
|
+
test.weak3 = [string]; assert(test.weak3 == [string])
|
306
|
+
|
307
|
+
test.weak_enumerable1 = fixnum.to_s; assert(test.weak_enumerable1 == fixnum)
|
308
|
+
test.weak_enumerable1 = string; assert(test.weak_enumerable1 == string)
|
309
|
+
test.weak_enumerable1 = [fixnum.to_s]; assert(test.weak_enumerable1 == [fixnum])
|
310
|
+
test.weak_enumerable1 = [string]; assert(test.weak_enumerable1 == [string])
|
311
|
+
|
312
|
+
test.weak_enumerable2 = fixnum.to_s; assert(test.weak_enumerable2 == fixnum)
|
313
|
+
test.weak_enumerable2 = string; assert(test.weak_enumerable2 == string)
|
314
|
+
test.weak_enumerable2 = [fixnum.to_s]; assert(test.weak_enumerable2 == [fixnum])
|
315
|
+
test.weak_enumerable2 = [string]; assert(test.weak_enumerable2 == [string])
|
316
|
+
|
317
|
+
test.weak_enumerable3 = fixnum.to_s; assert(test.weak_enumerable3 == fixnum)
|
318
|
+
test.weak_enumerable3 = string; assert(test.weak_enumerable3 == string)
|
319
|
+
test.weak_enumerable3 = [fixnum.to_s]; assert(test.weak_enumerable3 == [fixnum])
|
320
|
+
test.weak_enumerable3 = [string]; assert(test.weak_enumerable3 == [string])
|
321
|
+
|
322
|
+
test.base_enumerable1 = fixnum.to_s; assert(test.base_enumerable1 == fixnum)
|
323
|
+
assert_exception { test.base_enumerable1 = string }
|
324
|
+
test.base_enumerable1 = [fixnum.to_s]; assert(test.base_enumerable1 == [fixnum])
|
325
|
+
assert_exception { test.base_enumerable1 = [string] }
|
326
|
+
|
327
|
+
test.base_enumerable3 = fixnum.to_s; assert(test.base_enumerable3 == fixnum)
|
328
|
+
assert_exception { test.base_enumerable3 = string }
|
329
|
+
test.base_enumerable3 = [fixnum.to_s]; assert(test.base_enumerable3 == [fixnum])
|
330
|
+
assert_exception { test.base_enumerable3 = [string] }
|
331
|
+
|
332
|
+
|
333
|
+
|
334
|
+
# -----------------
|
335
|
+
|
336
|
+
puts "#{$assertions} assertions OK"
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.8.11
|
3
|
+
specification_version: 1
|
4
|
+
name: conversion
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.1.0
|
7
|
+
date: 2006-10-22 00:00:00 +02:00
|
8
|
+
summary: Conversion module obviously allows object conversion and extends attr_writer/accessor
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: gr@pierlis.com
|
12
|
+
homepage: http://conversion.rubyforge.org
|
13
|
+
rubyforge_project: conversion
|
14
|
+
description: Conversion module obviously allows object conversion and extends attr_writer/accessor
|
15
|
+
autorequire: conversion
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
authors:
|
29
|
+
- "Gwendal Rou\xC3\xA9"
|
30
|
+
files:
|
31
|
+
- README
|
32
|
+
- CHANGELOG
|
33
|
+
- Rakefile
|
34
|
+
- test/conversion_test.rb
|
35
|
+
- test/test_helper.rb
|
36
|
+
- lib/conversion
|
37
|
+
- lib/conversion.rb
|
38
|
+
- lib/conversion_test.rb
|
39
|
+
- lib/conversion/accessors.rb
|
40
|
+
- lib/conversion/class_decoration.rb
|
41
|
+
- lib/conversion/core.rb
|
42
|
+
- lib/conversion/mode
|
43
|
+
- lib/conversion/object_decoration.rb
|
44
|
+
- lib/conversion/version.rb
|
45
|
+
- lib/conversion/mode/human_input
|
46
|
+
- lib/conversion/mode/human_input.rb
|
47
|
+
- lib/conversion/mode/nil_on_failure.rb
|
48
|
+
- lib/conversion/mode/stable_nil.rb
|
49
|
+
- lib/conversion/mode/strong_type_checking.rb
|
50
|
+
- lib/conversion/mode/weak.rb
|
51
|
+
- lib/conversion/mode/human_input/class_decoration.rb
|
52
|
+
- lib/conversion/mode/human_input/core.rb
|
53
|
+
test_files: []
|
54
|
+
|
55
|
+
rdoc_options:
|
56
|
+
- --quiet
|
57
|
+
- --title
|
58
|
+
- conversion documentation
|
59
|
+
- --opname
|
60
|
+
- index.html
|
61
|
+
- --line-numbers
|
62
|
+
- --main
|
63
|
+
- README
|
64
|
+
- --inline-source
|
65
|
+
- --exclude
|
66
|
+
- ^(examples|extras)/
|
67
|
+
extra_rdoc_files:
|
68
|
+
- README
|
69
|
+
- CHANGELOG
|
70
|
+
executables: []
|
71
|
+
|
72
|
+
extensions: []
|
73
|
+
|
74
|
+
requirements: []
|
75
|
+
|
76
|
+
dependencies: []
|
77
|
+
|