to_parsed_obj 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", "~> 2.1.0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.5.2"
12
+ gem "rcov", ">= 0"
13
+ gem 'win32console'
14
+ end
15
+
16
+ gem 'mharris_ext'
17
+ gem 'fattr'
data/Gemfile.lock ADDED
@@ -0,0 +1,37 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.2)
5
+ facets (2.9.0)
6
+ fattr (2.2.0)
7
+ git (1.2.5)
8
+ jeweler (1.5.2)
9
+ bundler (~> 1.0.0)
10
+ git (>= 1.2.5)
11
+ rake
12
+ mharris_ext (1.5.0)
13
+ facets
14
+ fattr
15
+ rake (0.8.7)
16
+ rcov (0.9.9)
17
+ rspec (2.1.0)
18
+ rspec-core (~> 2.1.0)
19
+ rspec-expectations (~> 2.1.0)
20
+ rspec-mocks (~> 2.1.0)
21
+ rspec-core (2.1.0)
22
+ rspec-expectations (2.1.0)
23
+ diff-lcs (~> 1.1.2)
24
+ rspec-mocks (2.1.0)
25
+ win32console (1.3.0-x86-mingw32)
26
+
27
+ PLATFORMS
28
+ x86-mingw32
29
+
30
+ DEPENDENCIES
31
+ bundler (~> 1.0.0)
32
+ fattr
33
+ jeweler (~> 1.5.2)
34
+ mharris_ext
35
+ rcov
36
+ rspec (~> 2.1.0)
37
+ win32console
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 mharris717
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = to_parsed_obj
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to to_parsed_obj
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 mharris717. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,50 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "to_parsed_obj"
16
+ gem.homepage = "http://github.com/mharris717/to_parsed_obj"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{parse strings into the appropriate object}
19
+ gem.description = %Q{parse strings into the appropriate object}
20
+ gem.email = "mharris717@gmail.com"
21
+ gem.authors = ["mharris717"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rspec/core'
30
+ require 'rspec/core/rake_task'
31
+ RSpec::Core::RakeTask.new(:spec) do |spec|
32
+ spec.pattern = FileList['spec/**/*_spec.rb']
33
+ end
34
+
35
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
36
+ spec.pattern = 'spec/**/*_spec.rb'
37
+ spec.rcov = true
38
+ end
39
+
40
+ task :default => :spec
41
+
42
+ require 'rake/rdoctask'
43
+ Rake::RDocTask.new do |rdoc|
44
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
45
+
46
+ rdoc.rdoc_dir = 'rdoc'
47
+ rdoc.title = "to_parsed_obj #{version}"
48
+ rdoc.rdoc_files.include('README*')
49
+ rdoc.rdoc_files.include('lib/**/*.rb')
50
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,7 @@
1
+ require File.dirname(__FILE__) + "/../to_parsed_obj"
2
+
3
+ class Object
4
+ def to_parsed_obj
5
+ ToParsedObj.parse(self)
6
+ end
7
+ end
@@ -0,0 +1,73 @@
1
+ require 'mharris_ext'
2
+ module ToParsedObj
3
+ def self.method_missing(sym,*args,&b)
4
+ Parsers.instance.send(sym,*args,&b)
5
+ end
6
+ class SingleParser
7
+ include FromHash
8
+ attr_accessor :matcher, :convertor, :match_obj
9
+ def match?(obj,m=matcher)
10
+ self.match_obj = if m.kind_of?(Regexp)
11
+ m.match(obj.to_s.strip)
12
+ elsif m.respond_to?(:call)
13
+ m.call(obj)
14
+ elsif m.kind_of?(Array)
15
+ m.all? { |x| match?(obj,x) }
16
+ else
17
+ m == obj
18
+ end
19
+ !!match_obj
20
+ end
21
+ def parse(obj)
22
+ if convertor.kind_of?(Symbol) || convertor.kind_of?(String)
23
+ obj.send(convertor)
24
+ elsif convertor.kind_of?(Class)
25
+ convertor.new(obj)
26
+ elsif convertor.respond_to?(:call)
27
+ if convertor.arity == 1
28
+ convertor.call(obj)
29
+ else
30
+ convertor.call(obj,self)
31
+ end
32
+ else
33
+ "dunno how to convert"
34
+ end
35
+ end
36
+ end
37
+ class Parsers
38
+ include FromHash
39
+ fattr(:parsers) { [] }
40
+ def parse(obj)
41
+ parsers.each do |parser|
42
+ return parser.parse(obj) if parser.match?(obj)
43
+ end
44
+ obj
45
+ end
46
+ def add(ops,&b)
47
+ ops[:convertor] ||= b if block_given?
48
+ self.parsers << ToParsedObj::SingleParser.new(ops)
49
+ end
50
+ def add_basic!
51
+ require 'date'
52
+ add(:matcher => /^[0-9]+$/, :convertor => :to_i)
53
+ add(:matcher => [/^[0-9\.]+$/,/^[^\.]*\.[^\.]*$/], :convertor => :to_f)
54
+ add(:matcher => /^\d+[\/\\]\d+[\/\\]\d+$/) { |str| Date.parse(str) }
55
+
56
+ add(:matcher => /^(\d+[\/\\]\d+[\/\\]\d+\s+\d+(?::\d+){1,99})/) do |str,single|
57
+ m = single.match_obj[1].scan(/\d+/).map { |x| x.to_i }
58
+ arr = [2,0,1].map { |i| m[i] }
59
+ arr += m[3..-1].map { |x| x }
60
+ Time.local(*arr)
61
+ end
62
+
63
+ add(:matcher => /^(\d+-\d+-\d+\s+\d+(?::\d+){1,99})\s+\-\d+$/) do |str,single|
64
+ m = single.match_obj[1].scan(/\d+/).map { |x| x.to_i }
65
+ Time.local(*m)
66
+ end
67
+ end
68
+ class << self
69
+ fattr(:instance) { new }
70
+ end
71
+ end
72
+ end
73
+
@@ -0,0 +1,28 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'to_parsed_obj'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
13
+
14
+
15
+ RSpec::Matchers.define :parse_to do |expected|
16
+ match do |base_str|
17
+ @base_str = base_str
18
+ @parsed = base_str.to_parsed_obj
19
+ @parsed == expected
20
+ end
21
+ failure_message_for_should do |arg|
22
+ "Expected #{@base_str} to parse to #{expected.inspect}, actually parsed to #{@parsed.inspect}"
23
+ end
24
+ end
25
+
26
+ def mit(name,&b)
27
+ it(name,&b) #if name == "should parse time inspect"
28
+ end
@@ -0,0 +1,45 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require 'to_parsed_obj/ext'
3
+
4
+ TP = ToParsedObj
5
+
6
+ describe "ToParsedObj" do
7
+ before do
8
+ TP::Parsers.instance!
9
+ TP.add_basic!
10
+ end
11
+
12
+ mit "should leave empty alone" do
13
+ "".to_parsed_obj.should == ""
14
+ end
15
+ mit "should leave random string alone" do
16
+ str = "fgdfgerger"
17
+ str.to_parsed_obj.should == str
18
+ end
19
+ mit 'should convert int' do
20
+ "123".should parse_to(123)
21
+ end
22
+ mit "should convert float" do
23
+ "2.3".should parse_to(2.3)
24
+ end
25
+ mit "should not parse multiple dots to float" do
26
+ "1.2.3".should parse_to("1.2.3")
27
+ end
28
+ mit "should parse date" do
29
+ "4/1/2010".should parse_to(Date.new(2010,1,4) )
30
+ end
31
+ mit "should parse time with hours and minutes" do
32
+ "4/1/2010 12:30".should parse_to(Time.local(2010,4,1,12,30) )
33
+ end
34
+ mit "should parse time with seconds" do
35
+ "4/1/2010 12:30:01".should parse_to(Time.local(2010,4,1,12,30,1) )
36
+ end
37
+ mit 'should parse time inspect with seconds' do
38
+ t = Time.local(2010,4,1,12,30,1)
39
+ t.inspect.should parse_to(t)
40
+ end
41
+ mit 'should parse time inspect with no hours' do
42
+ t = Time.local(2010,4,1)
43
+ t.inspect.should parse_to(t)
44
+ end
45
+ end
@@ -0,0 +1,75 @@
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{to_parsed_obj}
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 = ["mharris717"]
12
+ s.date = %q{2011-01-12}
13
+ s.description = %q{parse strings into the appropriate object}
14
+ s.email = %q{mharris717@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "lib/to_parsed_obj.rb",
29
+ "lib/to_parsed_obj/ext.rb",
30
+ "spec/spec_helper.rb",
31
+ "spec/to_parsed_obj_spec.rb",
32
+ "to_parsed_obj.gemspec"
33
+ ]
34
+ s.homepage = %q{http://github.com/mharris717/to_parsed_obj}
35
+ s.licenses = ["MIT"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.3.7}
38
+ s.summary = %q{parse strings into the appropriate object}
39
+ s.test_files = [
40
+ "spec/spec_helper.rb",
41
+ "spec/to_parsed_obj_spec.rb"
42
+ ]
43
+
44
+ if s.respond_to? :specification_version then
45
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
49
+ s.add_runtime_dependency(%q<mharris_ext>, [">= 0"])
50
+ s.add_runtime_dependency(%q<fattr>, [">= 0"])
51
+ s.add_development_dependency(%q<rspec>, ["~> 2.1.0"])
52
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
53
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
54
+ s.add_development_dependency(%q<rcov>, [">= 0"])
55
+ s.add_development_dependency(%q<win32console>, [">= 0"])
56
+ else
57
+ s.add_dependency(%q<mharris_ext>, [">= 0"])
58
+ s.add_dependency(%q<fattr>, [">= 0"])
59
+ s.add_dependency(%q<rspec>, ["~> 2.1.0"])
60
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
61
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
62
+ s.add_dependency(%q<rcov>, [">= 0"])
63
+ s.add_dependency(%q<win32console>, [">= 0"])
64
+ end
65
+ else
66
+ s.add_dependency(%q<mharris_ext>, [">= 0"])
67
+ s.add_dependency(%q<fattr>, [">= 0"])
68
+ s.add_dependency(%q<rspec>, ["~> 2.1.0"])
69
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
70
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
71
+ s.add_dependency(%q<rcov>, [">= 0"])
72
+ s.add_dependency(%q<win32console>, [">= 0"])
73
+ end
74
+ end
75
+
metadata ADDED
@@ -0,0 +1,184 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: to_parsed_obj
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - mharris717
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-12 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: mharris_ext
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ prerelease: false
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: fattr
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :runtime
47
+ prerelease: false
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: rspec
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ~>
55
+ - !ruby/object:Gem::Version
56
+ hash: 11
57
+ segments:
58
+ - 2
59
+ - 1
60
+ - 0
61
+ version: 2.1.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: *id003
65
+ - !ruby/object:Gem::Dependency
66
+ name: bundler
67
+ requirement: &id004 !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ~>
71
+ - !ruby/object:Gem::Version
72
+ hash: 23
73
+ segments:
74
+ - 1
75
+ - 0
76
+ - 0
77
+ version: 1.0.0
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: *id004
81
+ - !ruby/object:Gem::Dependency
82
+ name: jeweler
83
+ requirement: &id005 !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ~>
87
+ - !ruby/object:Gem::Version
88
+ hash: 7
89
+ segments:
90
+ - 1
91
+ - 5
92
+ - 2
93
+ version: 1.5.2
94
+ type: :development
95
+ prerelease: false
96
+ version_requirements: *id005
97
+ - !ruby/object:Gem::Dependency
98
+ name: rcov
99
+ requirement: &id006 !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ hash: 3
105
+ segments:
106
+ - 0
107
+ version: "0"
108
+ type: :development
109
+ prerelease: false
110
+ version_requirements: *id006
111
+ - !ruby/object:Gem::Dependency
112
+ name: win32console
113
+ requirement: &id007 !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ hash: 3
119
+ segments:
120
+ - 0
121
+ version: "0"
122
+ type: :development
123
+ prerelease: false
124
+ version_requirements: *id007
125
+ description: parse strings into the appropriate object
126
+ email: mharris717@gmail.com
127
+ executables: []
128
+
129
+ extensions: []
130
+
131
+ extra_rdoc_files:
132
+ - LICENSE.txt
133
+ - README.rdoc
134
+ files:
135
+ - .document
136
+ - .rspec
137
+ - Gemfile
138
+ - Gemfile.lock
139
+ - LICENSE.txt
140
+ - README.rdoc
141
+ - Rakefile
142
+ - VERSION
143
+ - lib/to_parsed_obj.rb
144
+ - lib/to_parsed_obj/ext.rb
145
+ - spec/spec_helper.rb
146
+ - spec/to_parsed_obj_spec.rb
147
+ - to_parsed_obj.gemspec
148
+ has_rdoc: true
149
+ homepage: http://github.com/mharris717/to_parsed_obj
150
+ licenses:
151
+ - MIT
152
+ post_install_message:
153
+ rdoc_options: []
154
+
155
+ require_paths:
156
+ - lib
157
+ required_ruby_version: !ruby/object:Gem::Requirement
158
+ none: false
159
+ requirements:
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ hash: 3
163
+ segments:
164
+ - 0
165
+ version: "0"
166
+ required_rubygems_version: !ruby/object:Gem::Requirement
167
+ none: false
168
+ requirements:
169
+ - - ">="
170
+ - !ruby/object:Gem::Version
171
+ hash: 3
172
+ segments:
173
+ - 0
174
+ version: "0"
175
+ requirements: []
176
+
177
+ rubyforge_project:
178
+ rubygems_version: 1.3.7
179
+ signing_key:
180
+ specification_version: 3
181
+ summary: parse strings into the appropriate object
182
+ test_files:
183
+ - spec/spec_helper.rb
184
+ - spec/to_parsed_obj_spec.rb