typelib 0.0.3 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Rakefile +81 -62
- data/VERSION +1 -0
- data/lib/typelib/canned.rb +4 -2
- data/lib/typelib.rb +16 -31
- data/test/test_01_basic.rb +0 -10
- data/test/test_02_canned.rb +1 -1
- data/typelib.gemspec +45 -0
- metadata +57 -63
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c6c56c6fd9daadf03661c3db85605d8bddc730a5f9788d94a289b1e26d5dee2b
|
4
|
+
data.tar.gz: 12f7cdc9baed7a9a1c3a2ca3347a0a180a0276ea24ef3ae2766d454784a3cd9c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e0794762a0e037c2723b1c35cb2847813000205b56e3bff52cf3706cc68fe4ef0364f08a9e9df6f3e1e612510fae2851ecd9c713ae36ae3b6ffaf9e5015494c4
|
7
|
+
data.tar.gz: 2c5fbb32eed282c6bef0eaf4cbe4c59fbbea3b89e3c2d354a34453376a886b70a2fe130404fc7dcb69860fec63e752907fe0c741bab4c71f669bf5b5f74f3681
|
data/Rakefile
CHANGED
@@ -1,80 +1,99 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require 'rake/testtask'
|
4
|
-
require 'rake/packagetask'
|
5
|
-
require 'rake/gempackagetask'
|
6
|
-
require 'rdoc/task'
|
7
|
-
require 'fileutils'
|
8
|
-
require 'typelib'
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
9
3
|
|
10
|
-
|
4
|
+
version = (File.exist?('VERSION') ? File.read('VERSION') : "").chomp
|
11
5
|
|
12
|
-
|
6
|
+
begin
|
7
|
+
require 'jeweler'
|
8
|
+
Jeweler::Tasks.new do |gem|
|
9
|
+
gem.name = "typelib"
|
10
|
+
gem.authors = ["Erik Hollensbe"]
|
11
|
+
gem.email = "erik@hollensbe.org"
|
12
|
+
gem.summary = "An on-demand arbitrary check and conversion library that won't destroy your data."
|
13
|
+
gem.homepage = "http://github.com/RDBI/typelib"
|
14
|
+
gem.authors = ["Erik Hollensbe"]
|
13
15
|
|
14
|
-
|
15
|
-
|
16
|
-
end
|
16
|
+
gem.add_development_dependency 'test-unit'
|
17
|
+
gem.add_development_dependency 'rdoc'
|
17
18
|
|
18
|
-
#
|
19
|
-
|
20
|
-
|
19
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
20
|
+
end
|
21
|
+
Jeweler::GemcutterTasks.new
|
22
|
+
rescue LoadError
|
23
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
24
|
+
end
|
21
25
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
+
begin
|
27
|
+
gem 'test-unit'
|
28
|
+
require 'rake/testtask'
|
29
|
+
Rake::TestTask.new(:test) do |test|
|
30
|
+
test.libs << 'lib' << 'test'
|
31
|
+
test.pattern = 'test/**/test_*.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
rescue LoadError
|
35
|
+
task :test do
|
36
|
+
abort "test-unit gem is not available. In order to run test-unit, you must: sudo gem install test-unit"
|
37
|
+
end
|
26
38
|
end
|
27
39
|
|
28
|
-
#
|
29
|
-
# Distribution
|
30
|
-
#
|
31
40
|
|
32
|
-
|
33
|
-
|
34
|
-
|
41
|
+
begin
|
42
|
+
require 'rcov/rcovtask'
|
43
|
+
Rcov::RcovTask.new do |test|
|
44
|
+
test.libs << 'test'
|
45
|
+
test.pattern = 'test/**/test_*.rb'
|
46
|
+
test.verbose = true
|
47
|
+
end
|
48
|
+
rescue LoadError
|
49
|
+
task :rcov do
|
50
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
51
|
+
end
|
52
|
+
end
|
35
53
|
|
36
|
-
|
37
|
-
# Documentation
|
38
|
-
#
|
54
|
+
task :test => :check_dependencies
|
39
55
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
56
|
+
begin
|
57
|
+
require 'roodi'
|
58
|
+
require 'roodi_task'
|
59
|
+
RoodiTask.new do |t|
|
60
|
+
t.verbose = false
|
61
|
+
end
|
62
|
+
rescue LoadError
|
63
|
+
task :roodi do
|
64
|
+
abort "Roodi is not available. In order to run roodi, you must: sudo gem install roodi"
|
65
|
+
end
|
46
66
|
end
|
47
67
|
|
48
|
-
|
49
|
-
# Packaging
|
50
|
-
#
|
51
|
-
|
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."
|
58
|
-
s.homepage = "http://erik.hollensbe.org/docs/typelib/"
|
59
|
-
s.files = Dir["Rakefile"] + Dir["README.rdoc"] + Dir["lib/**/*"] + Dir['test/**/*']
|
60
|
-
|
61
|
-
s.has_rdoc = true
|
62
|
-
s.add_development_dependency 'test-unit'
|
63
|
-
s.rdoc_options = %w(-a)
|
64
|
-
end
|
68
|
+
task :default => :test
|
65
69
|
|
66
|
-
|
67
|
-
|
70
|
+
begin
|
71
|
+
require 'rdoc/task'
|
72
|
+
RDoc::Task.new do |rdoc|
|
73
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
68
74
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
75
|
+
rdoc.main = 'README.rdoc'
|
76
|
+
rdoc.rdoc_dir = 'rdoc'
|
77
|
+
rdoc.title = "RDBI #{version} Documentation"
|
78
|
+
rdoc.rdoc_files.include('README*')
|
79
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
80
|
+
end
|
81
|
+
rescue LoadError => e
|
82
|
+
rdoc_missing = lambda do
|
83
|
+
# no-op
|
84
|
+
end
|
85
|
+
task :rdoc, &rdoc_missing
|
86
|
+
task :clobber_rdoc, &rdoc_missing
|
76
87
|
end
|
77
88
|
|
78
89
|
task :to_blog => [:clobber_rdoc, :rdoc] do
|
79
|
-
sh "rm -fr $git/blog/content/docs/typelib && mv
|
90
|
+
sh "rm -fr $git/blog/content/docs/typelib && mv doc $git/blog/content/docs/typelib"
|
80
91
|
end
|
92
|
+
|
93
|
+
task :install => [:test, :build]
|
94
|
+
|
95
|
+
task :docview => [:rerdoc] do
|
96
|
+
sh "open rdoc/index.html"
|
97
|
+
end
|
98
|
+
|
99
|
+
# vim: syntax=ruby ts=2 et sw=2 sts=2
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.1
|
data/lib/typelib/canned.rb
CHANGED
@@ -22,7 +22,7 @@ module TypeLib
|
|
22
22
|
end
|
23
23
|
|
24
24
|
check = proc do |obj|
|
25
|
-
(DateTime.strptime(obj, format).strftime(format) == obj) rescue false
|
25
|
+
(DateTime.strptime(obj, format).strftime(format) == obj) rescue false
|
26
26
|
end
|
27
27
|
|
28
28
|
convert = proc { |obj| DateTime.strptime(obj, format) }
|
@@ -40,6 +40,7 @@ module TypeLib
|
|
40
40
|
IS_STRING = proc { |obj| obj.kind_of?(String) }
|
41
41
|
IS_NUMERIC = proc { |obj| obj.kind_of?(Numeric) }
|
42
42
|
IS_INTEGER = proc { |obj| obj.kind_of?(Integer) }
|
43
|
+
IS_BOOLEAN = proc { |obj| obj.kind_of?(TrueClass) || obj.kind_of?(FalseClass) }
|
43
44
|
end
|
44
45
|
|
45
46
|
# Canned Conversions.
|
@@ -49,8 +50,9 @@ module TypeLib
|
|
49
50
|
TO_STRING = proc { |obj| obj.to_s }
|
50
51
|
TO_BINARY = proc { |obj| obj.to_s(2) }
|
51
52
|
TO_HEX = proc { |obj| obj.to_s(16) }
|
53
|
+
TO_BOOLEAN = proc { |obj| !!obj }
|
52
54
|
|
53
|
-
STR_TO_BIGDECIMAL = proc { |obj| BigDecimal
|
55
|
+
STR_TO_BIGDECIMAL = proc { |obj| BigDecimal(obj.to_s) }
|
54
56
|
end
|
55
57
|
|
56
58
|
# Fully canned filters.
|
data/lib/typelib.rb
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
require 'delegate'
|
2
|
-
|
3
1
|
# Typelib is a way of checking and converting data. It operates as a "filter
|
4
2
|
# chain" system which allows it to gradually normalize disparate data into a
|
5
3
|
# common type. Each chain is optionally a part of a list which allows it to
|
@@ -12,32 +10,22 @@ require 'delegate'
|
|
12
10
|
#
|
13
11
|
module TypeLib
|
14
12
|
|
15
|
-
|
13
|
+
def self.execute_filterlist(filters, obj)
|
14
|
+
f = filters.find { |filter| filter.check(obj) }
|
15
|
+
f ? f.convert(obj) : obj
|
16
|
+
end
|
16
17
|
|
17
18
|
# A FilterList is a ... list of filters. It includes all the methods that
|
18
19
|
# Array contains, plus an additional method -- execute. See
|
19
20
|
# TypeLib::Filter.
|
20
|
-
class FilterList <
|
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
|
-
|
21
|
+
class FilterList < Array
|
28
22
|
# Execute the checks in this list against +obj+, passing in +addl+
|
29
23
|
# if any additional arguments are provided. If the check passes, the
|
30
24
|
# conversion is run and the chain supplied to the constructor is
|
31
25
|
# followed. If no checks pass, the original item is returned.
|
32
|
-
def execute(obj
|
33
|
-
|
34
|
-
|
35
|
-
if filter.check(obj, *addl)
|
36
|
-
ret = filter.convert(obj, *addl)
|
37
|
-
break
|
38
|
-
end
|
39
|
-
end
|
40
|
-
return ret
|
26
|
+
def execute(obj)
|
27
|
+
f = find { |filter| filter.check(obj) }
|
28
|
+
f ? f.convert(obj) : obj
|
41
29
|
end
|
42
30
|
end
|
43
31
|
|
@@ -59,26 +47,23 @@ module TypeLib
|
|
59
47
|
|
60
48
|
# Check this object against the filter. If additional data is supplied,
|
61
49
|
# it will be provided to the Filter#check_proc.
|
62
|
-
def check(obj
|
63
|
-
check_proc.call(obj
|
50
|
+
def check(obj)
|
51
|
+
check_proc.call(obj)
|
64
52
|
end
|
65
53
|
|
66
54
|
# Convert this object unconditionally. If additional data is supplied,
|
67
55
|
# it will be provided to the Filter#convert_proc.
|
68
|
-
def convert(obj
|
69
|
-
ret = convert_proc.call(obj
|
70
|
-
filters.execute(ret
|
56
|
+
def convert(obj)
|
57
|
+
ret = convert_proc.call(obj)
|
58
|
+
filters.execute(ret)
|
71
59
|
end
|
72
60
|
|
73
61
|
#
|
74
62
|
# Same as TypeLib::FilterList#execute, only just for this filter.
|
75
63
|
#
|
76
|
-
def execute(obj
|
77
|
-
|
78
|
-
|
79
|
-
ret = convert(obj, *addl)
|
80
|
-
end
|
81
|
-
return ret
|
64
|
+
def execute(obj)
|
65
|
+
return convert(obj) if check(obj)
|
66
|
+
return obj
|
82
67
|
end
|
83
68
|
end
|
84
69
|
end
|
data/test/test_01_basic.rb
CHANGED
@@ -61,16 +61,6 @@ class TestBasic < Test::Unit::TestCase
|
|
61
61
|
assert_raises(ArgumentError) { filters.execute("1.25") }
|
62
62
|
end
|
63
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
|
73
|
-
|
74
64
|
def test_05_chains
|
75
65
|
filters = TypeLib::FilterList.new
|
76
66
|
|
data/test/test_02_canned.rb
CHANGED
@@ -30,7 +30,7 @@ class TestCanned < Test::Unit::TestCase
|
|
30
30
|
assert_kind_of(Float, filters.execute("1"))
|
31
31
|
|
32
32
|
filters = create_filterlist(STR_TO_DEC)
|
33
|
-
assert_equal(BigDecimal
|
33
|
+
assert_equal(BigDecimal("1.0"), filters.execute("1"))
|
34
34
|
assert_kind_of(BigDecimal, filters.execute("1"))
|
35
35
|
end
|
36
36
|
|
data/typelib.gemspec
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
# stub: typelib 0.1.1 ruby lib
|
6
|
+
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = "typelib".freeze
|
9
|
+
s.version = "0.1.1"
|
10
|
+
|
11
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
|
12
|
+
s.require_paths = ["lib".freeze]
|
13
|
+
s.authors = ["Erik Hollensbe".freeze]
|
14
|
+
s.date = "2023-06-05"
|
15
|
+
s.email = "erik@hollensbe.org".freeze
|
16
|
+
s.extra_rdoc_files = [
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
"README.rdoc",
|
21
|
+
"Rakefile",
|
22
|
+
"VERSION",
|
23
|
+
"lib/typelib.rb",
|
24
|
+
"lib/typelib/canned.rb",
|
25
|
+
"test/test_01_basic.rb",
|
26
|
+
"test/test_02_canned.rb",
|
27
|
+
"typelib.gemspec"
|
28
|
+
]
|
29
|
+
s.homepage = "http://github.com/RDBI/typelib".freeze
|
30
|
+
s.rubygems_version = "3.3.7".freeze
|
31
|
+
s.summary = "An on-demand arbitrary check and conversion library that won't destroy your data.".freeze
|
32
|
+
|
33
|
+
if s.respond_to? :specification_version then
|
34
|
+
s.specification_version = 4
|
35
|
+
end
|
36
|
+
|
37
|
+
if s.respond_to? :add_runtime_dependency then
|
38
|
+
s.add_development_dependency(%q<test-unit>.freeze, [">= 0"])
|
39
|
+
s.add_development_dependency(%q<rdoc>.freeze, [">= 0"])
|
40
|
+
else
|
41
|
+
s.add_dependency(%q<test-unit>.freeze, [">= 0"])
|
42
|
+
s.add_dependency(%q<rdoc>.freeze, [">= 0"])
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
metadata
CHANGED
@@ -1,85 +1,79 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: typelib
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease: false
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 0
|
9
|
-
- 3
|
10
|
-
version: 0.0.3
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
11
5
|
platform: ruby
|
12
|
-
authors:
|
6
|
+
authors:
|
13
7
|
- Erik Hollensbe
|
14
|
-
autorequire:
|
8
|
+
autorequire:
|
15
9
|
bindir: bin
|
16
10
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
dependencies:
|
21
|
-
- !ruby/object:Gem::Dependency
|
11
|
+
date: 2023-06-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
22
14
|
name: test-unit
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
23
21
|
prerelease: false
|
24
|
-
|
25
|
-
|
26
|
-
requirements:
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
27
24
|
- - ">="
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rdoc
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
33
34
|
type: :development
|
34
|
-
|
35
|
-
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description:
|
36
42
|
email: erik@hollensbe.org
|
37
43
|
executables: []
|
38
|
-
|
39
44
|
extensions: []
|
40
|
-
|
41
|
-
extra_rdoc_files: []
|
42
|
-
|
43
|
-
files:
|
44
|
-
- Rakefile
|
45
|
+
extra_rdoc_files:
|
45
46
|
- README.rdoc
|
46
|
-
|
47
|
+
files:
|
48
|
+
- README.rdoc
|
49
|
+
- Rakefile
|
50
|
+
- VERSION
|
47
51
|
- lib/typelib.rb
|
52
|
+
- lib/typelib/canned.rb
|
48
53
|
- test/test_01_basic.rb
|
49
54
|
- test/test_02_canned.rb
|
50
|
-
|
51
|
-
homepage: http://
|
55
|
+
- typelib.gemspec
|
56
|
+
homepage: http://github.com/RDBI/typelib
|
52
57
|
licenses: []
|
53
|
-
|
54
|
-
post_install_message:
|
55
|
-
rdoc_options:
|
56
|
-
|
57
|
-
require_paths:
|
58
|
+
metadata: {}
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options: []
|
61
|
+
require_paths:
|
58
62
|
- lib
|
59
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
-
|
61
|
-
requirements:
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
62
65
|
- - ">="
|
63
|
-
- !ruby/object:Gem::Version
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
version: "0"
|
68
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
-
none: false
|
70
|
-
requirements:
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
71
70
|
- - ">="
|
72
|
-
- !ruby/object:Gem::Version
|
73
|
-
|
74
|
-
segments:
|
75
|
-
- 0
|
76
|
-
version: "0"
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
77
73
|
requirements: []
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
summary: An on-demand arbitrary check and conversion library that won't destroy your data.
|
74
|
+
rubygems_version: 3.3.7
|
75
|
+
signing_key:
|
76
|
+
specification_version: 4
|
77
|
+
summary: An on-demand arbitrary check and conversion library that won't destroy your
|
78
|
+
data.
|
84
79
|
test_files: []
|
85
|
-
|