conversion 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README +5 -2
- data/Rakefile +2 -1
- data/lib/conversion.rb +0 -3
- data/lib/conversion/core.rb +25 -6
- data/lib/conversion/version.rb +2 -2
- data/lib/conversion_test.rb +6 -5
- metadata +4 -3
- data/lib/conversion/class_decoration.rb +0 -107
data/README
CHANGED
data/Rakefile
CHANGED
@@ -26,7 +26,8 @@ RDOC_OPTS = ['--quiet', '--title', "conversion documentation",
|
|
26
26
|
"--opname", "index.html",
|
27
27
|
"--line-numbers",
|
28
28
|
"--main", "README",
|
29
|
-
"--inline-source"
|
29
|
+
"--inline-source",
|
30
|
+
"--charset", "utf-8"]
|
30
31
|
|
31
32
|
desc "Packages up conversion gem."
|
32
33
|
task :default => [:test]
|
data/lib/conversion.rb
CHANGED
@@ -9,9 +9,6 @@ require "conversion/object_decoration"
|
|
9
9
|
# class A; include Conversion::Accessors; attr_accessor <attr>, :store_as => <target>; end
|
10
10
|
require "conversion/accessors"
|
11
11
|
|
12
|
-
# Symbol, Strings, Integer, Float, etc.
|
13
|
-
require "conversion/class_decoration"
|
14
|
-
|
15
12
|
# conversion modes
|
16
13
|
require "conversion/mode/weak"
|
17
14
|
require "conversion/mode/strong_type_checking"
|
data/lib/conversion/core.rb
CHANGED
@@ -1,10 +1,4 @@
|
|
1
1
|
# = Conversion Module
|
2
|
-
#
|
3
|
-
# Author:: Gwendal Roué (mailto:gr@pierlis.com)
|
4
|
-
# Copyright:: Copyright (c) 2006 Pierlis
|
5
|
-
# License:: Distributes under the same terms as Ruby
|
6
|
-
#
|
7
|
-
# -------
|
8
2
|
#
|
9
3
|
# The Conversion Module provides a framework for:
|
10
4
|
#
|
@@ -282,6 +276,31 @@ module Conversion
|
|
282
276
|
|
283
277
|
elsif target.is_a?(Symbol)
|
284
278
|
proc { |value| value.send(target) }
|
279
|
+
|
280
|
+
elsif target <= Integer
|
281
|
+
proc do |value|
|
282
|
+
if value.is_a?(Float)
|
283
|
+
value.round
|
284
|
+
else
|
285
|
+
begin
|
286
|
+
Integer(value)
|
287
|
+
rescue Exception
|
288
|
+
Float(value).round
|
289
|
+
end
|
290
|
+
end
|
291
|
+
end
|
292
|
+
|
293
|
+
elsif target == Float
|
294
|
+
proc { |value| value.is_a?(Float) ? value : Float(value) }
|
295
|
+
|
296
|
+
elsif target == NilClass
|
297
|
+
proc { |value| nil }
|
298
|
+
|
299
|
+
elsif target == String
|
300
|
+
proc { |value| value.to_s }
|
301
|
+
|
302
|
+
elsif target == Symbol
|
303
|
+
proc { |value| value.to_sym }
|
285
304
|
|
286
305
|
elsif target.is_a?(Class) &&
|
287
306
|
target.respond_to?(:new) &&
|
data/lib/conversion/version.rb
CHANGED
data/lib/conversion_test.rb
CHANGED
@@ -1,13 +1,14 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__))
|
1
2
|
require 'conversion'
|
2
3
|
require 'date'
|
3
4
|
|
4
5
|
$assertions = 0
|
5
|
-
def assert(condition)
|
6
|
+
def assert(condition) #:nodoc:
|
6
7
|
raise "Assertion failed" unless condition
|
7
8
|
$assertions += 1
|
8
9
|
end
|
9
10
|
|
10
|
-
def assert_exception(klass=Exception)
|
11
|
+
def assert_exception(klass=Exception) #:nodoc:
|
11
12
|
begin
|
12
13
|
yield
|
13
14
|
rescue klass
|
@@ -21,7 +22,7 @@ end
|
|
21
22
|
|
22
23
|
# -----------------
|
23
24
|
|
24
|
-
class Money
|
25
|
+
class Money #:nodoc:
|
25
26
|
include Conversion::Accessors
|
26
27
|
attr_accessor :amount, :store_as => Float
|
27
28
|
attr_accessor :currency, :store_as => Symbol
|
@@ -34,7 +35,7 @@ class Money
|
|
34
35
|
def to_s() amount.to_s end
|
35
36
|
end
|
36
37
|
|
37
|
-
class Date
|
38
|
+
class Date #:nodoc:
|
38
39
|
class << self
|
39
40
|
def to_french_human_input_converter_proc
|
40
41
|
proc { |value|
|
@@ -55,7 +56,7 @@ class Date
|
|
55
56
|
end
|
56
57
|
end
|
57
58
|
|
58
|
-
class Test
|
59
|
+
class Test #:nodoc:
|
59
60
|
include Conversion::Accessors
|
60
61
|
|
61
62
|
attr_reader :base1
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
|
|
3
3
|
specification_version: 1
|
4
4
|
name: conversion
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.1.
|
7
|
-
date: 2006-10-
|
6
|
+
version: 0.1.1
|
7
|
+
date: 2006-10-23 00:00:00 +02:00
|
8
8
|
summary: Conversion module obviously allows object conversion and extends attr_writer/accessor
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -37,7 +37,6 @@ files:
|
|
37
37
|
- lib/conversion.rb
|
38
38
|
- lib/conversion_test.rb
|
39
39
|
- lib/conversion/accessors.rb
|
40
|
-
- lib/conversion/class_decoration.rb
|
41
40
|
- lib/conversion/core.rb
|
42
41
|
- lib/conversion/mode
|
43
42
|
- lib/conversion/object_decoration.rb
|
@@ -62,6 +61,8 @@ rdoc_options:
|
|
62
61
|
- --main
|
63
62
|
- README
|
64
63
|
- --inline-source
|
64
|
+
- --charset
|
65
|
+
- utf-8
|
65
66
|
- --exclude
|
66
67
|
- ^(examples|extras)/
|
67
68
|
extra_rdoc_files:
|
@@ -1,107 +0,0 @@
|
|
1
|
-
class Bignum
|
2
|
-
class << self
|
3
|
-
# Returns a Proc that converts to Integer
|
4
|
-
#
|
5
|
-
# Bignum.to_converter_proc.call('1') # => 1
|
6
|
-
#
|
7
|
-
# Note that the result may not be a Bignum.
|
8
|
-
#
|
9
|
-
# See Integer.to_converter_proc, Conversion.converter, Object#convert_to
|
10
|
-
def to_converter_proc
|
11
|
-
Integer.to_converter_proc
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
class Fixnum
|
17
|
-
class << self
|
18
|
-
# Returns a Proc that converts to Integer
|
19
|
-
#
|
20
|
-
# Fixnum.to_converter_proc.call('1') # => 1
|
21
|
-
#
|
22
|
-
# Note that the result may not be a Fixnum.
|
23
|
-
#
|
24
|
-
# See Conversion.converter, Object#convert_to
|
25
|
-
def to_converter_proc
|
26
|
-
Integer.to_converter_proc
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
class Float
|
32
|
-
class << self
|
33
|
-
# Returns a Proc that converts to Float
|
34
|
-
#
|
35
|
-
# Float.to_converter_proc.call(1) # => 1.0
|
36
|
-
#
|
37
|
-
# See Conversion.converter, Object#convert_to
|
38
|
-
def to_converter_proc
|
39
|
-
proc { |value| value.is_a?(Float) ? value : Float(value) }
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
class Integer
|
45
|
-
class << self
|
46
|
-
# Returns a Proc that converts to Integer
|
47
|
-
#
|
48
|
-
# Integer.to_converter_proc.call(1) # => 1
|
49
|
-
# Integer.to_converter_proc.call(1.9) # => 2
|
50
|
-
# Integer.to_converter_proc.call('1.9') # => 2
|
51
|
-
# Integer.to_converter_proc.call('abc') # ArgumentError: invalid value for Float(): "abc"
|
52
|
-
#
|
53
|
-
# See Conversion.converter, Object#convert_to
|
54
|
-
def to_converter_proc
|
55
|
-
proc do |value|
|
56
|
-
if value.is_a?(Float)
|
57
|
-
value.round
|
58
|
-
else
|
59
|
-
begin
|
60
|
-
Integer(value)
|
61
|
-
rescue Exception
|
62
|
-
Float(value).round
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
class NilClass
|
71
|
-
class << self
|
72
|
-
# returns a Proc that converts to nil
|
73
|
-
#
|
74
|
-
# NilClass.to_converter_proc.call(1) # => nil
|
75
|
-
#
|
76
|
-
# See Conversion.converter, Object#convert_to
|
77
|
-
def to_converter_proc
|
78
|
-
proc { |value| nil }
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
class String
|
84
|
-
class << self
|
85
|
-
# returns a Proc that converts to String
|
86
|
-
#
|
87
|
-
# String.to_converter_proc.call(1) # => '1'
|
88
|
-
#
|
89
|
-
# See Conversion.converter, Object#convert_to
|
90
|
-
def to_converter_proc
|
91
|
-
proc { |value| value.to_s }
|
92
|
-
end
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
class Symbol
|
97
|
-
class << self
|
98
|
-
# returns a Proc that converts to Symbol
|
99
|
-
#
|
100
|
-
# Symbol.to_converter_proc.call('abc') # => :abc
|
101
|
-
#
|
102
|
-
# See Conversion.converter, Object#convert_to
|
103
|
-
def to_converter_proc
|
104
|
-
proc { |value| value.to_sym }
|
105
|
-
end
|
106
|
-
end
|
107
|
-
end
|