typelib 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,80 +1,104 @@
1
- # $Id$
2
- $:.push 'lib'
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
- include FileUtils::Verbose
4
+ version = (File.exist?('VERSION') ? File.read('VERSION') : "").chomp
11
5
 
12
- task :default => [ :test, :dist ]
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
- task :fixperms do
15
- chmod(0755, Dir['bin/*'])
16
- end
16
+ gem.add_development_dependency 'test-unit'
17
+ gem.add_development_dependency 'rdoc'
17
18
 
18
- #
19
- # Tests
20
- #
19
+ ## for now, install hanna from here: http://github.com/raggi/hanna
20
+ gem.add_development_dependency 'hanna'
21
21
 
22
- Rake::TestTask.new do |t|
23
- t.libs << 'lib'
24
- t.test_files = FileList['test/test*.rb']
25
- t.verbose = true
22
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
23
+ end
24
+ Jeweler::GemcutterTasks.new
25
+ rescue LoadError
26
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
26
27
  end
27
28
 
28
- #
29
- # Distribution
30
- #
31
-
32
- task :dist => [:fixperms, :repackage, :gem, :rdoc]
33
- task :distclean => [:clobber_package, :clobber_rdoc]
34
- task :clean => [:distclean]
29
+ begin
30
+ gem 'test-unit'
31
+ require 'rake/testtask'
32
+ Rake::TestTask.new(:test) do |test|
33
+ test.libs << 'lib' << 'test'
34
+ test.pattern = 'test/**/test_*.rb'
35
+ test.verbose = true
36
+ end
37
+ rescue LoadError
38
+ task :test do
39
+ abort "test-unit gem is not available. In order to run test-unit, you must: sudo gem install test-unit"
40
+ end
41
+ end
35
42
 
36
- #
37
- # Documentation
38
- #
39
43
 
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)
44
+ begin
45
+ require 'rcov/rcovtask'
46
+ Rcov::RcovTask.new do |test|
47
+ test.libs << 'test'
48
+ test.pattern = 'test/**/test_*.rb'
49
+ test.verbose = true
50
+ end
51
+ rescue LoadError
52
+ task :rcov do
53
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
54
+ end
46
55
  end
47
56
 
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
57
+ task :test => :check_dependencies
65
58
 
66
- Rake::GemPackageTask.new(spec) do |s|
59
+ begin
60
+ require 'roodi'
61
+ require 'roodi_task'
62
+ RoodiTask.new do |t|
63
+ t.verbose = false
64
+ end
65
+ rescue LoadError
66
+ task :roodi do
67
+ abort "Roodi is not available. In order to run roodi, you must: sudo gem install roodi"
68
+ end
67
69
  end
68
70
 
69
- Rake::PackageTask.new(spec.name, spec.version) do |p|
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")
71
+ task :default => :test
72
+
73
+ begin
74
+ require 'hanna'
75
+ require 'rdoc/task'
76
+ RDoc::Task.new do |rdoc|
77
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
78
+
79
+ rdoc.options.push '-f', 'hanna'
80
+ rdoc.main = 'README.rdoc'
81
+ rdoc.rdoc_dir = 'rdoc'
82
+ rdoc.title = "RDBI #{version} Documentation"
83
+ rdoc.rdoc_files.include('README*')
84
+ rdoc.rdoc_files.include('lib/**/*.rb')
85
+ end
86
+ rescue LoadError => e
87
+ rdoc_missing = lambda do
88
+ abort "What, were you born in a barn? Install rdoc and hanna at http://github.com/erikh/hanna ."
89
+ end
90
+ task :rdoc, &rdoc_missing
91
+ task :clobber_rdoc, &rdoc_missing
76
92
  end
77
93
 
78
94
  task :to_blog => [:clobber_rdoc, :rdoc] do
79
- sh "rm -fr $git/blog/content/docs/typelib && mv rdoc $git/blog/content/docs/typelib"
95
+ sh "rm -fr $git/blog/content/docs/typelib && mv doc $git/blog/content/docs/typelib"
80
96
  end
97
+
98
+ task :install => [:test, :build]
99
+
100
+ task :docview => [:rerdoc] do
101
+ sh "open rdoc/index.html"
102
+ end
103
+
104
+ # vim: syntax=ruby ts=2 et sw=2 sts=2
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -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
- VERSION = "0.0.3"
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 < 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
-
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, *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
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, *addl)
63
- check_proc.call(obj, *addl)
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, *addl)
69
- ret = convert_proc.call(obj, *addl)
70
- filters.execute(ret, *addl)
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, *addl)
77
- ret = obj
78
- if check(obj, *addl)
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
@@ -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,6 +50,7 @@ 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
55
  STR_TO_BIGDECIMAL = proc { |obj| BigDecimal.new(obj.to_s) }
54
56
  end
@@ -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
 
@@ -0,0 +1,55 @@
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
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{typelib}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Erik Hollensbe"]
12
+ s.date = %q{2010-12-10}
13
+ s.email = %q{erik@hollensbe.org}
14
+ s.extra_rdoc_files = [
15
+ "README.rdoc"
16
+ ]
17
+ s.files = [
18
+ "README.rdoc",
19
+ "Rakefile",
20
+ "VERSION",
21
+ "lib/typelib.rb",
22
+ "lib/typelib/canned.rb",
23
+ "test/test_01_basic.rb",
24
+ "test/test_02_canned.rb",
25
+ "typelib.gemspec"
26
+ ]
27
+ s.homepage = %q{http://github.com/RDBI/typelib}
28
+ s.require_paths = ["lib"]
29
+ s.rubygems_version = %q{1.3.7}
30
+ s.summary = %q{An on-demand arbitrary check and conversion library that won't destroy your data.}
31
+ s.test_files = [
32
+ "test/test_01_basic.rb",
33
+ "test/test_02_canned.rb"
34
+ ]
35
+
36
+ if s.respond_to? :specification_version then
37
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
38
+ s.specification_version = 3
39
+
40
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
41
+ s.add_development_dependency(%q<test-unit>, [">= 0"])
42
+ s.add_development_dependency(%q<rdoc>, [">= 0"])
43
+ s.add_development_dependency(%q<hanna>, [">= 0"])
44
+ else
45
+ s.add_dependency(%q<test-unit>, [">= 0"])
46
+ s.add_dependency(%q<rdoc>, [">= 0"])
47
+ s.add_dependency(%q<hanna>, [">= 0"])
48
+ end
49
+ else
50
+ s.add_dependency(%q<test-unit>, [">= 0"])
51
+ s.add_dependency(%q<rdoc>, [">= 0"])
52
+ s.add_dependency(%q<hanna>, [">= 0"])
53
+ end
54
+ end
55
+
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: typelib
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
5
4
  prerelease: false
6
5
  segments:
7
6
  - 0
7
+ - 1
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ version: 0.1.0
11
10
  platform: ruby
12
11
  authors:
13
12
  - Erik Hollensbe
@@ -15,7 +14,7 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2010-06-18 00:00:00 -04:00
17
+ date: 2010-12-10 00:00:00 -05:00
19
18
  default_executable:
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
@@ -26,34 +25,61 @@ dependencies:
26
25
  requirements:
27
26
  - - ">="
28
27
  - !ruby/object:Gem::Version
29
- hash: 3
30
28
  segments:
31
29
  - 0
32
30
  version: "0"
33
31
  type: :development
34
32
  version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: rdoc
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ version: "0"
44
+ type: :development
45
+ version_requirements: *id002
46
+ - !ruby/object:Gem::Dependency
47
+ name: hanna
48
+ prerelease: false
49
+ requirement: &id003 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ type: :development
58
+ version_requirements: *id003
35
59
  description:
36
60
  email: erik@hollensbe.org
37
61
  executables: []
38
62
 
39
63
  extensions: []
40
64
 
41
- extra_rdoc_files: []
42
-
65
+ extra_rdoc_files:
66
+ - README.rdoc
43
67
  files:
44
- - Rakefile
45
68
  - README.rdoc
46
- - lib/typelib/canned.rb
69
+ - Rakefile
70
+ - VERSION
47
71
  - lib/typelib.rb
72
+ - lib/typelib/canned.rb
48
73
  - test/test_01_basic.rb
49
74
  - test/test_02_canned.rb
75
+ - typelib.gemspec
50
76
  has_rdoc: true
51
- homepage: http://erik.hollensbe.org/docs/typelib/
77
+ homepage: http://github.com/RDBI/typelib
52
78
  licenses: []
53
79
 
54
80
  post_install_message:
55
- rdoc_options:
56
- - -a
81
+ rdoc_options: []
82
+
57
83
  require_paths:
58
84
  - lib
59
85
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -61,7 +87,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
61
87
  requirements:
62
88
  - - ">="
63
89
  - !ruby/object:Gem::Version
64
- hash: 3
65
90
  segments:
66
91
  - 0
67
92
  version: "0"
@@ -70,7 +95,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
70
95
  requirements:
71
96
  - - ">="
72
97
  - !ruby/object:Gem::Version
73
- hash: 3
74
98
  segments:
75
99
  - 0
76
100
  version: "0"
@@ -81,5 +105,6 @@ rubygems_version: 1.3.7
81
105
  signing_key:
82
106
  specification_version: 3
83
107
  summary: An on-demand arbitrary check and conversion library that won't destroy your data.
84
- test_files: []
85
-
108
+ test_files:
109
+ - test/test_01_basic.rb
110
+ - test/test_02_canned.rb