typelib 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,80 @@
1
+ = TypeLib
2
+
3
+ Typelib is a way of checking and converting data. It operates as a "filter
4
+ chain" system which allows it to gradually normalize disparate data into a
5
+ common type. Each chain is optionally a part of a list which allows it to
6
+ provide several paths in a single external execution.
7
+
8
+ The library is arguably very simple and therefore has simple requirements and
9
+ needs. This is intentional.
10
+
11
+ = Synopsis
12
+
13
+ Here's a basic example which converts strings OR integers to integers. This
14
+ sounds anti-climactic, but it allows us to take any kind of integer data and
15
+ convert it safely.
16
+
17
+ filters = TypeLib::FilterList.new
18
+ check = proc { |obj| obj.kind_of?(Integer) }
19
+ convert = proc { |obj| obj }
20
+
21
+ check2 = proc { |obj| obj.kind_of?(String) and obj =~ /^\d+$/ }
22
+ convert2 = proc { |obj| Integer(obj) }
23
+
24
+ filters << TypeLib::Filter.new(check, convert)
25
+ filters << TypeLib::Filter.new(check2, convert2)
26
+
27
+ filters.execute(1) # => 1
28
+ filters.execute("1") # => 1
29
+ filters.execute("1.0") # => "1.0"
30
+
31
+ TypeLib will just not do anything with data it doesn't understand, and return
32
+ it. This is why the float in this example does nothing -- it doesn't match
33
+ either of the checks.
34
+
35
+ Filters can also take arguments that propogate into the checks and conversions
36
+ via TypeLib::Filter#check and TypeLib::Filter#convert. Additionally, these
37
+ arguments will be passed on from TypeLib::FilterList#execute.
38
+
39
+ As mentioned prior, TypeLib can also chain. This allows us to say "if this
40
+ check passes, do/try this, then run this next set of checks/conversions". This
41
+ is the foundation of portable conversion, allowing us to sew together many
42
+ checks and conversions to handle coercing any data into the right type.
43
+
44
+ In the below check, any integers are converted to strings, and any string that
45
+ looks like an integer gets coerced to float. Additionally, any string from the
46
+ start that looks like an integer will also get converted to float. This is
47
+ reuse of the chain without implying recursion. All parts of TypeLib can be
48
+ reused at any point within the library's storage.
49
+
50
+ filters = TypeLib::FilterList.new
51
+
52
+ check = proc { |obj| obj.kind_of?(Integer) }
53
+ convert = proc { |obj| obj.to_s }
54
+
55
+ check2 = proc { |obj| obj.kind_of?(String) and obj =~ /^\d+$/ }
56
+ convert2 = proc { |obj| obj.to_f }
57
+
58
+ string_to_float = TypeLib::Filter.new(check2, convert2)
59
+
60
+ filters << TypeLib::Filter.new(
61
+ check,
62
+ convert,
63
+ TypeLib::FilterList.new([string_to_float])
64
+ )
65
+
66
+ filters << string_to_float
67
+
68
+ filters.execute(1) # => 1.0
69
+ filters.execute("1.0") # => 1.0
70
+
71
+ TypeLib also comes with a (slowly growing) number of canned checks that are
72
+ optionally available. These are accessible in the 'typelib/canned' library and
73
+ live in the TypeLib::Canned namespace.
74
+
75
+ = Thanks
76
+
77
+ == Contributed to design and review of the initial product
78
+
79
+ * Dav3xor (David Case)
80
+ * raggi (James Tucker)
data/Rakefile CHANGED
@@ -12,7 +12,7 @@ include FileUtils::Verbose
12
12
  task :default => [ :test, :dist ]
13
13
 
14
14
  task :fixperms do
15
- chmod(0755, Dir['bin/*'])
15
+ chmod(0755, Dir['bin/*'])
16
16
  end
17
17
 
18
18
  #
@@ -20,9 +20,9 @@ end
20
20
  #
21
21
 
22
22
  Rake::TestTask.new do |t|
23
- t.libs << 'lib'
24
- t.test_files = FileList['test/test*.rb']
25
- t.verbose = true
23
+ t.libs << 'lib'
24
+ t.test_files = FileList['test/test*.rb']
25
+ t.verbose = true
26
26
  end
27
27
 
28
28
  #
@@ -38,11 +38,11 @@ task :clean => [:distclean]
38
38
  #
39
39
 
40
40
  RDoc::Task.new do |rd|
41
- rd.rdoc_dir = "rdoc"
42
- rd.main = "README.rdoc"
43
- rd.rdoc_files.include("README.rdoc")
44
- rd.rdoc_files.include("./lib/**/*.rb")
45
- rd.options = %w(-a)
41
+ rd.rdoc_dir = "rdoc"
42
+ rd.main = "README.rdoc"
43
+ rd.rdoc_files.include("README.rdoc")
44
+ rd.rdoc_files.include("./lib/**/*.rb")
45
+ rd.options = %w(-a)
46
46
  end
47
47
 
48
48
  #
@@ -50,25 +50,31 @@ end
50
50
  #
51
51
 
52
52
  spec = Gem::Specification.new do |s|
53
- s.name = "typelib"
54
- s.version = TypeLib::VERSION
55
- s.author = "Erik Hollensbe"
56
- s.email = "erik@hollensbe.org"
57
- s.summary = "An on-demand arbitrary check and conversion library that won't destroy your data."
53
+ s.name = "typelib"
54
+ s.version = TypeLib::VERSION
55
+ s.author = "Erik Hollensbe"
56
+ s.email = "erik@hollensbe.org"
57
+ s.summary = "An on-demand arbitrary check and conversion library that won't destroy your data."
58
+ s.homepage = "http://erik.hollensbe.org/docs/typelib/"
59
+ s.files = Dir["Rakefile"] + Dir["README.rdoc"] + Dir["lib/**/*"] + Dir['test/**/*']
58
60
 
59
- s.files = Dir["Rakefile"] + Dir["README"] + Dir["lib/**/*"] + Dir['test/**/*']
60
-
61
- s.has_rdoc = true
61
+ s.has_rdoc = true
62
+ s.add_development_dependency 'test-unit'
63
+ s.rdoc_options = %w(-a)
62
64
  end
63
65
 
64
66
  Rake::GemPackageTask.new(spec) do |s|
65
67
  end
66
68
 
67
69
  Rake::PackageTask.new(spec.name, spec.version) do |p|
68
- p.need_tar_gz = true
69
- p.need_zip = true
70
- p.package_files.include("./bin/**/*")
71
- p.package_files.include("./Rakefile")
72
- p.package_files.include("./lib/**/*.rb")
73
- p.package_files.include("README")
70
+ p.need_tar_gz = true
71
+ p.need_zip = true
72
+ p.package_files.include("./bin/**/*")
73
+ p.package_files.include("./Rakefile")
74
+ p.package_files.include("./lib/**/*.rb")
75
+ p.package_files.include("README")
76
+ end
77
+
78
+ task :to_blog => [:clobber_rdoc, :rdoc] do
79
+ sh "rm -fr $git/blog/content/docs/typelib && mv rdoc $git/blog/content/docs/typelib"
74
80
  end
@@ -12,62 +12,73 @@ require 'delegate'
12
12
  #
13
13
  module TypeLib
14
14
 
15
- VERSION = "0.0.1"
15
+ VERSION = "0.0.3"
16
16
 
17
- # A FilterList is a ... list of filters. It includes all the methods that
18
- # Array contains, plus an additional method -- execute. See
19
- # TypeLib::Filter.
20
- class FilterList < DelegateClass(Array)
21
- # Create a new FilterList. An array of TypeLib::Filter objects is
22
- # accepted for construction.
23
- def initialize(ary=[])
24
- @filters = ary
25
- super(@filters)
26
- end
17
+ # A FilterList is a ... list of filters. It includes all the methods that
18
+ # Array contains, plus an additional method -- execute. See
19
+ # TypeLib::Filter.
20
+ class FilterList < DelegateClass(Array)
21
+ # Create a new FilterList. An array of TypeLib::Filter objects is
22
+ # accepted for construction.
23
+ def initialize(ary=[])
24
+ @filters = ary
25
+ super(@filters)
26
+ end
27
27
 
28
- # Execute the checks in this list against +obj+, passing in +addl+
29
- # if any additional arguments are provided. If the check passes, the
30
- # conversion is run and the chain supplied to the constructor is
31
- # followed. If no checks pass, the original item is returned.
32
- def execute(obj, *addl)
33
- ret = obj
34
- @filters.each do |filter|
35
- if filter.check(obj, *addl)
36
- ret = filter.convert(obj, *addl)
37
- break
38
- end
39
- end
40
- return ret
28
+ # Execute the checks in this list against +obj+, passing in +addl+
29
+ # if any additional arguments are provided. If the check passes, the
30
+ # conversion is run and the chain supplied to the constructor is
31
+ # followed. If no checks pass, the original item is returned.
32
+ def execute(obj, *addl)
33
+ ret = obj
34
+ @filters.each do |filter|
35
+ if filter.check(obj, *addl)
36
+ ret = filter.convert(obj, *addl)
37
+ break
41
38
  end
39
+ end
40
+ return ret
42
41
  end
42
+ end
43
43
 
44
- # TypeLib::Filter is a way of checking, then optionally converting data
45
- # based on whether or not the check passes. At that point, an additional
46
- # TypeLib::FilterList may be provided which indicates that more checks need
47
- # to be followed with the new data.
48
- class Filter
49
- attr_reader :check_proc, :convert_proc, :filters
44
+ # TypeLib::Filter is a way of checking, then optionally converting data
45
+ # based on whether or not the check passes. At that point, an additional
46
+ # TypeLib::FilterList may be provided which indicates that more checks need
47
+ # to be followed with the new data.
48
+ class Filter
49
+ attr_reader :check_proc, :convert_proc, :filters
50
50
 
51
- # A TypeLib::Filter consists of a check procedure, a conversion procedure,
52
- # and a TypeLib::FilterList which may be empty. Checks return boolean
53
- # which indicates whether or not the conversion will be attempted.
54
- def initialize(check, convert, filters=FilterList.new)
55
- @check_proc = check
56
- @convert_proc = convert
57
- @filters = filters
58
- end
51
+ # A TypeLib::Filter consists of a check procedure, a conversion procedure,
52
+ # and a TypeLib::FilterList which may be empty. Checks return boolean
53
+ # which indicates whether or not the conversion will be attempted.
54
+ def initialize(check, convert, filters=FilterList.new)
55
+ @check_proc = check
56
+ @convert_proc = convert
57
+ @filters = filters
58
+ end
59
59
 
60
- # Check this object against the filter. If additional data is supplied,
61
- # it will be provided to the Filter#check_proc.
62
- def check(obj, *addl)
63
- check_proc.call(obj, *addl)
64
- end
60
+ # Check this object against the filter. If additional data is supplied,
61
+ # it will be provided to the Filter#check_proc.
62
+ def check(obj, *addl)
63
+ check_proc.call(obj, *addl)
64
+ end
65
65
 
66
- # Convert this object unconditionally. If additional data is supplied,
67
- # it will be provided to the Filter#convert_proc.
68
- def convert(obj, *addl)
69
- ret = convert_proc.call(obj, *addl)
70
- filters.execute(ret, *addl)
71
- end
66
+ # Convert this object unconditionally. If additional data is supplied,
67
+ # it will be provided to the Filter#convert_proc.
68
+ def convert(obj, *addl)
69
+ ret = convert_proc.call(obj, *addl)
70
+ filters.execute(ret, *addl)
71
+ end
72
+
73
+ #
74
+ # Same as TypeLib::FilterList#execute, only just for this filter.
75
+ #
76
+ def execute(obj, *addl)
77
+ ret = obj
78
+ if check(obj, *addl)
79
+ ret = convert(obj, *addl)
80
+ end
81
+ return ret
72
82
  end
83
+ end
73
84
  end
@@ -3,64 +3,65 @@ require 'date'
3
3
  require 'typelib'
4
4
 
5
5
  module TypeLib
6
- # Canned checks are just that -- already written for you.
7
- module Canned
8
- #
9
- # build_strptime_filter works with DateTime objects and a format that
10
- # you provide. It will build and return a new TypeLib::Filter that can
11
- # check strings for the format you provide and if they pass, convert
12
- # them to a DateTime object.
13
- #
14
- # Due to limitations in the DateTime system itself, you must include
15
- # zone information -- if not, you will get a value that is offset to
16
- # GMT, which is not what DateTime.now and pals will return. An
17
- # ArgumentError will be raised if you do not supply this in the filter.
18
- #
19
- def build_strptime_filter(format)
20
- if format !~ /%z/
21
- raise ArgumentError, "format must include %z due to DateTime fail"
22
- end
6
+ # Canned checks are just that -- already written for you.
7
+ module Canned
8
+ #
9
+ # build_strptime_filter works with DateTime objects and a format that
10
+ # you provide. It will build and return a new TypeLib::Filter that can
11
+ # check strings for the format you provide and if they pass, convert
12
+ # them to a DateTime object.
13
+ #
14
+ # Due to limitations in the DateTime system itself, you must include
15
+ # zone information -- if not, you will get a value that is offset to
16
+ # GMT, which is not what DateTime.now and pals will return. An
17
+ # ArgumentError will be raised if you do not supply this in the filter.
18
+ #
19
+ def build_strptime_filter(format)
20
+ if format !~ /%z/
21
+ raise ArgumentError, "format must include %z due to DateTime fail"
22
+ end
23
23
 
24
- check = proc do |obj|
25
- (DateTime.strptime(obj, format).strftime(format) == obj) rescue false
26
- end
24
+ check = proc do |obj|
25
+ (DateTime.strptime(obj, format).strftime(format) == obj) rescue false
26
+ end
27
27
 
28
- convert = proc { |obj| DateTime.strptime(obj, format) }
28
+ convert = proc { |obj| DateTime.strptime(obj, format) }
29
29
 
30
- return Filter.new(check, convert)
31
- end
30
+ return Filter.new(check, convert)
31
+ end
32
32
 
33
- module_function :build_strptime_filter
33
+ module_function :build_strptime_filter
34
34
 
35
- # Canned Checks.
36
- module Checks
37
- STR_IS_INT = proc { |obj| obj.kind_of?(String) and obj =~ /^\d+$/ }
38
- STR_IS_DEC = proc { |obj| obj.kind_of?(String) and obj =~ /^[\d.]+$/ }
35
+ # Canned Checks.
36
+ module Checks
37
+ STR_IS_INT = proc { |obj| obj.kind_of?(String) and obj =~ /^-?\d+$/ }
38
+ STR_IS_DEC = proc { |obj| obj.kind_of?(String) and obj =~ /^-?[\d.]+$/ }
39
39
 
40
- IS_NUMERIC = proc { |obj| obj.kind_of?(Numeric) }
41
- IS_INTEGER = proc { |obj| obj.kind_of?(Integer) }
42
- end
40
+ IS_STRING = proc { |obj| obj.kind_of?(String) }
41
+ IS_NUMERIC = proc { |obj| obj.kind_of?(Numeric) }
42
+ IS_INTEGER = proc { |obj| obj.kind_of?(Integer) }
43
+ end
43
44
 
44
- # Canned Conversions.
45
- module Conversions
46
- TO_INTEGER = proc { |obj| obj.to_i }
47
- TO_FLOAT = proc { |obj| obj.to_f }
48
- TO_STRING = proc { |obj| obj.to_s }
49
- TO_BINARY = proc { |obj| obj.to_s(2) }
50
- TO_HEX = proc { |obj| obj.to_s(16) }
45
+ # Canned Conversions.
46
+ module Conversions
47
+ TO_INTEGER = proc { |obj| obj.to_i }
48
+ TO_FLOAT = proc { |obj| obj.to_f }
49
+ TO_STRING = proc { |obj| obj.to_s }
50
+ TO_BINARY = proc { |obj| obj.to_s(2) }
51
+ TO_HEX = proc { |obj| obj.to_s(16) }
51
52
 
52
- STR_TO_BIGDECIMAL = proc { |obj| BigDecimal.new(obj.to_s) }
53
- end
53
+ STR_TO_BIGDECIMAL = proc { |obj| BigDecimal.new(obj.to_s) }
54
+ end
54
55
 
55
- # Fully canned filters.
56
- module Filters
57
- STR_TO_INT = Filter.new(Checks::STR_IS_INT, Conversions::TO_INTEGER)
58
- STR_TO_FLOAT = Filter.new(Checks::STR_IS_DEC, Conversions::TO_FLOAT)
59
- STR_TO_DEC = Filter.new(Checks::STR_IS_DEC, Conversions::STR_TO_BIGDECIMAL)
56
+ # Fully canned filters.
57
+ module Filters
58
+ STR_TO_INT = Filter.new(Checks::STR_IS_INT, Conversions::TO_INTEGER)
59
+ STR_TO_FLOAT = Filter.new(Checks::STR_IS_DEC, Conversions::TO_FLOAT)
60
+ STR_TO_DEC = Filter.new(Checks::STR_IS_DEC, Conversions::STR_TO_BIGDECIMAL)
60
61
 
61
- NUM_TO_STR = Filter.new(Checks::IS_NUMERIC, Conversions::TO_STRING)
62
- INT_TO_BIN = Filter.new(Checks::IS_INTEGER, Conversions::TO_BINARY)
63
- INT_TO_HEX = Filter.new(Checks::IS_INTEGER, Conversions::TO_HEX)
64
- end
62
+ NUM_TO_STR = Filter.new(Checks::IS_NUMERIC, Conversions::TO_STRING)
63
+ INT_TO_BIN = Filter.new(Checks::IS_INTEGER, Conversions::TO_BINARY)
64
+ INT_TO_HEX = Filter.new(Checks::IS_INTEGER, Conversions::TO_HEX)
65
65
  end
66
+ end
66
67
  end
@@ -6,85 +6,85 @@ $:.unshift 'lib'
6
6
  require 'typelib'
7
7
 
8
8
  class TestBasic < Test::Unit::TestCase
9
- def test_01_classes
10
- assert(TypeLib)
11
- assert(TypeLib::FilterList)
12
- assert(TypeLib::Filter)
9
+ def test_01_classes
10
+ assert(TypeLib)
11
+ assert(TypeLib::FilterList)
12
+ assert(TypeLib::Filter)
13
+ end
14
+
15
+ def test_02_object_properties
16
+ filters = TypeLib::FilterList.new
17
+
18
+ assert_respond_to(filters, :[])
19
+ assert_respond_to(filters, :<<)
20
+ assert_respond_to(filters, :execute)
21
+
22
+ check = proc { }
23
+ convert = proc { }
24
+ filter = TypeLib::Filter.new(check, convert)
25
+
26
+ assert_respond_to(filter, :check_proc)
27
+ assert_respond_to(filter, :convert_proc)
28
+ assert_respond_to(filter, :filters)
29
+ assert_respond_to(filter, :check)
30
+ assert_respond_to(filter, :convert)
31
+
32
+ assert_kind_of(Proc, filter.check_proc)
33
+ assert_kind_of(Proc, filter.convert_proc)
34
+ assert_kind_of(TypeLib::FilterList, filter.filters)
35
+ end
36
+
37
+ def test_03_basic_conversions
38
+ filters = TypeLib::FilterList.new
39
+ check = proc { |obj| obj.kind_of?(Integer) }
40
+ convert = proc { |obj| obj }
41
+
42
+ check2 = proc { |obj| obj.kind_of?(String) and obj =~ /^\d+$/ }
43
+ convert2 = proc { |obj| Integer(obj) }
44
+
45
+ filters << TypeLib::Filter.new(check, convert)
46
+ filters << TypeLib::Filter.new(check2, convert2)
47
+
48
+ assert_equal(2, filters.count)
49
+ assert_equal(1, filters.execute(1))
50
+ assert_equal(1, filters.execute("1"))
51
+
52
+ 10.times do
53
+ x = rand(250000).to_i
54
+ assert_equal(x, filters.execute(x))
55
+ assert_equal(x, filters.execute(x.to_s))
13
56
  end
14
57
 
15
- def test_02_object_properties
16
- filters = TypeLib::FilterList.new
58
+ assert_equal("1.25", filters.execute("1.25"))
17
59
 
18
- assert_respond_to(filters, :[])
19
- assert_respond_to(filters, :<<)
20
- assert_respond_to(filters, :execute)
60
+ filters << TypeLib::Filter.new(proc { true }, proc { |obj| Integer(obj) })
61
+ assert_raises(ArgumentError) { filters.execute("1.25") }
62
+ end
21
63
 
22
- check = proc { }
23
- convert = proc { }
24
- filter = TypeLib::Filter.new(check, convert)
64
+ def test_04_args
65
+ check = proc { |obj, *addl| addl[0] }
66
+ convert = proc { |obj, *addl| addl[0] }
67
+ filter = TypeLib::Filter.new(check, convert)
25
68
 
26
- assert_respond_to(filter, :check_proc)
27
- assert_respond_to(filter, :convert_proc)
28
- assert_respond_to(filter, :filters)
29
- assert_respond_to(filter, :check)
30
- assert_respond_to(filter, :convert)
69
+ assert(filter.check(true, true))
70
+ assert(!filter.check(true, false))
71
+ assert_equal("fart", filter.convert(true, "fart"))
72
+ end
31
73
 
32
- assert_kind_of(Proc, filter.check_proc)
33
- assert_kind_of(Proc, filter.convert_proc)
34
- assert_kind_of(TypeLib::FilterList, filter.filters)
35
- end
36
-
37
- def test_03_basic_conversions
38
- filters = TypeLib::FilterList.new
39
- check = proc { |obj| obj.kind_of?(Integer) }
40
- convert = proc { |obj| obj }
41
-
42
- check2 = proc { |obj| obj.kind_of?(String) and obj =~ /^\d+$/ }
43
- convert2 = proc { |obj| Integer(obj) }
44
-
45
- filters << TypeLib::Filter.new(check, convert)
46
- filters << TypeLib::Filter.new(check2, convert2)
47
-
48
- assert_equal(2, filters.count)
49
- assert_equal(1, filters.execute(1))
50
- assert_equal(1, filters.execute("1"))
51
-
52
- 10.times do
53
- x = rand(250000).to_i
54
- assert_equal(x, filters.execute(x))
55
- assert_equal(x, filters.execute(x.to_s))
56
- end
57
-
58
- assert_equal("1.25", filters.execute("1.25"))
74
+ def test_05_chains
75
+ filters = TypeLib::FilterList.new
59
76
 
60
- filters << TypeLib::Filter.new(proc { true }, proc { |obj| Integer(obj) })
61
- assert_raises(ArgumentError) { filters.execute("1.25") }
62
- end
63
-
64
- def test_04_args
65
- check = proc { |obj, *addl| addl[0] }
66
- convert = proc { |obj, *addl| addl[0] }
67
- filter = TypeLib::Filter.new(check, convert)
68
-
69
- assert(filter.check(true, true))
70
- assert(!filter.check(true, false))
71
- assert_equal("fart", filter.convert(true, "fart"))
72
- end
77
+ check = proc { |obj| obj.kind_of?(Integer) }
78
+ convert = proc { |obj| obj.to_s }
73
79
 
74
- def test_05_chains
75
- filters = TypeLib::FilterList.new
80
+ check2 = proc { |obj| obj.kind_of?(String) and obj =~ /^\d+$/ }
81
+ convert2 = proc { |obj| obj.to_f }
76
82
 
77
- check = proc { |obj| obj.kind_of?(Integer) }
78
- convert = proc { |obj| obj.to_s }
83
+ filters << TypeLib::Filter.new(check, convert, TypeLib::FilterList.new([TypeLib::Filter.new(check2, convert2)]))
84
+ filters << TypeLib::Filter.new(check2, convert2)
79
85
 
80
- check2 = proc { |obj| obj.kind_of?(String) and obj =~ /^\d+$/ }
81
- convert2 = proc { |obj| obj.to_f }
82
-
83
- filters << TypeLib::Filter.new(check, convert, TypeLib::FilterList.new([TypeLib::Filter.new(check2, convert2)]))
84
- filters << TypeLib::Filter.new(check2, convert2)
85
-
86
- assert_equal(1.0, filters.execute(1))
87
- assert_kind_of(Float, filters.execute(1))
88
- assert_equal(1.0, filters.execute("1"))
89
- end
86
+ assert_equal(1.0, filters.execute(1))
87
+ assert_kind_of(Float, filters.execute(1))
88
+ assert_equal(1.0, filters.execute("1"))
89
+ end
90
90
  end
@@ -7,50 +7,54 @@ require 'typelib'
7
7
  require 'typelib/canned'
8
8
 
9
9
  class TestCanned < Test::Unit::TestCase
10
- include TypeLib::Canned
11
- include TypeLib::Canned::Filters
12
-
13
- def create_filterlist(arg)
14
- filters = TypeLib::FilterList.new
15
- filters << arg
16
- return filters
17
- end
18
-
19
- def test_01_string_to_numeric
20
- filters = create_filterlist(STR_TO_INT)
21
- assert_equal(1, filters.execute("1"))
22
- assert_kind_of(Integer, filters.execute("1"))
23
-
24
- filters = create_filterlist(STR_TO_FLOAT)
25
- assert_equal(1.0, filters.execute("1"))
26
- assert_kind_of(Float, filters.execute("1"))
27
-
28
- filters = create_filterlist(STR_TO_DEC)
29
- assert_equal(BigDecimal.new("1.0"), filters.execute("1"))
30
- assert_kind_of(BigDecimal, filters.execute("1"))
31
- end
32
-
33
- def test_02_numeric_to_string
34
- filters = create_filterlist(NUM_TO_STR)
35
- assert_equal("1", filters.execute(1))
36
- assert_kind_of(String, filters.execute(1))
37
-
38
- filters = create_filterlist(INT_TO_BIN)
39
- assert_equal("10", filters.execute(2))
40
- assert_kind_of(String, filters.execute(2))
41
-
42
- filters = create_filterlist(INT_TO_HEX)
43
- assert_equal("a", filters.execute(10))
44
- assert_kind_of(String, filters.execute(10))
45
-
46
- end
47
-
48
- def test_03_datetime
49
- filters = create_filterlist(build_strptime_filter("%H:%M:%S%z"))
50
- time = DateTime.now
51
- newtime = filters.execute(time.strftime("%H:%M:%S%z"))
52
- assert_equal(time.to_s, newtime.to_s)
53
-
54
- assert_raises(ArgumentError.new("format must include %z due to DateTime fail")) { build_strptime_filter("%H:%M:%S") }
55
- end
10
+ include TypeLib::Canned
11
+ include TypeLib::Canned::Filters
12
+
13
+ def create_filterlist(arg)
14
+ filters = TypeLib::FilterList.new
15
+ filters << arg
16
+ return filters
17
+ end
18
+
19
+ def test_01_string_to_numeric
20
+ filters = create_filterlist(STR_TO_INT)
21
+ assert_equal(1, filters.execute("1"))
22
+ assert_kind_of(Integer, filters.execute("1"))
23
+
24
+ filters = create_filterlist(STR_TO_INT)
25
+ assert_equal(-1, filters.execute("-1"))
26
+ assert_kind_of(Integer, filters.execute("-1"))
27
+
28
+ filters = create_filterlist(STR_TO_FLOAT)
29
+ assert_equal(1.0, filters.execute("1"))
30
+ assert_kind_of(Float, filters.execute("1"))
31
+
32
+ filters = create_filterlist(STR_TO_DEC)
33
+ assert_equal(BigDecimal.new("1.0"), filters.execute("1"))
34
+ assert_kind_of(BigDecimal, filters.execute("1"))
35
+ end
36
+
37
+ def test_02_numeric_to_string
38
+ filters = create_filterlist(NUM_TO_STR)
39
+ assert_equal("1", filters.execute(1))
40
+ assert_kind_of(String, filters.execute(1))
41
+
42
+ filters = create_filterlist(INT_TO_BIN)
43
+ assert_equal("10", filters.execute(2))
44
+ assert_kind_of(String, filters.execute(2))
45
+
46
+ filters = create_filterlist(INT_TO_HEX)
47
+ assert_equal("a", filters.execute(10))
48
+ assert_kind_of(String, filters.execute(10))
49
+
50
+ end
51
+
52
+ def test_03_datetime
53
+ filters = create_filterlist(build_strptime_filter("%H:%M:%S%z"))
54
+ time = DateTime.now
55
+ newtime = filters.execute(time.strftime("%H:%M:%S%z"))
56
+ assert_equal(time.to_s, newtime.to_s)
57
+
58
+ assert_raises(ArgumentError.new("format must include %z due to DateTime fail")) { build_strptime_filter("%H:%M:%S") }
59
+ end
56
60
  end
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: typelib
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 25
4
5
  prerelease: false
5
6
  segments:
6
7
  - 0
7
8
  - 0
8
- - 1
9
- version: 0.0.1
9
+ - 3
10
+ version: 0.0.3
10
11
  platform: ruby
11
12
  authors:
12
13
  - Erik Hollensbe
@@ -14,10 +15,23 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2010-05-17 00:00:00 -04:00
18
+ date: 2010-06-18 00:00:00 -04:00
18
19
  default_executable:
19
- dependencies: []
20
-
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: test-unit
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
21
35
  description:
22
36
  email: erik@hollensbe.org
23
37
  executables: []
@@ -28,37 +42,42 @@ extra_rdoc_files: []
28
42
 
29
43
  files:
30
44
  - Rakefile
45
+ - README.rdoc
31
46
  - lib/typelib/canned.rb
32
47
  - lib/typelib.rb
33
48
  - test/test_01_basic.rb
34
49
  - test/test_02_canned.rb
35
50
  has_rdoc: true
36
- homepage:
51
+ homepage: http://erik.hollensbe.org/docs/typelib/
37
52
  licenses: []
38
53
 
39
54
  post_install_message:
40
- rdoc_options: []
41
-
55
+ rdoc_options:
56
+ - -a
42
57
  require_paths:
43
58
  - lib
44
59
  required_ruby_version: !ruby/object:Gem::Requirement
60
+ none: false
45
61
  requirements:
46
62
  - - ">="
47
63
  - !ruby/object:Gem::Version
64
+ hash: 3
48
65
  segments:
49
66
  - 0
50
67
  version: "0"
51
68
  required_rubygems_version: !ruby/object:Gem::Requirement
69
+ none: false
52
70
  requirements:
53
71
  - - ">="
54
72
  - !ruby/object:Gem::Version
73
+ hash: 3
55
74
  segments:
56
75
  - 0
57
76
  version: "0"
58
77
  requirements: []
59
78
 
60
79
  rubyforge_project:
61
- rubygems_version: 1.3.6
80
+ rubygems_version: 1.3.7
62
81
  signing_key:
63
82
  specification_version: 3
64
83
  summary: An on-demand arbitrary check and conversion library that won't destroy your data.