ddollar-simple-parser 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,41 @@
1
+ module SimpleParser::String
2
+
3
+ def self.included(base)
4
+ base.send(:define_method, :parse) do |format|
5
+ SimpleParser::String::parse(self, format)
6
+ end
7
+ end
8
+
9
+ @parser_cache = {}
10
+
11
+ def self.parse(string, format)
12
+ parser = @parser_cache[format] || SimpleParser::String::build_parser(format)
13
+
14
+ matches = string.match(parser[:regexp]).to_a
15
+ matches.shift
16
+
17
+ parser[:tokens].inject({}) do |memo, token|
18
+ memo[token.intern] = matches.shift
19
+ memo
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def self.build_parser(format)
26
+ tokens = format.scan(/\:\w+/).map { |token| token[1..-1] }
27
+
28
+ regexp_builder = "^#{Regexp.escape(format)}$"
29
+
30
+ tokens.each do |token|
31
+ regexp_builder.gsub!(":#{token}", '(.+?)')
32
+ end
33
+
34
+ regexp_builder.gsub!('::', '.+?')
35
+
36
+ regexp = Regexp.new(regexp_builder)
37
+
38
+ @parser_cache[format] = { :tokens => tokens, :regexp => regexp }
39
+ end
40
+
41
+ end
@@ -0,0 +1,43 @@
1
+ module SimpleParser
2
+
3
+ # :stopdoc:
4
+ VERSION = '0.1.0'
5
+ LIBPATH = File.expand_path(File.dirname(__FILE__)) + File::SEPARATOR
6
+ PATH = File.dirname(LIBPATH) + File::SEPARATOR
7
+ # :startdoc:
8
+
9
+ # Returns the version string for the library.
10
+ #
11
+ def self.version
12
+ VERSION
13
+ end
14
+
15
+ # Returns the library path for the module. If any arguments are given,
16
+ # they will be joined to the end of the libray path using
17
+ # <tt>File.join</tt>.
18
+ #
19
+ def self.libpath( *args )
20
+ args.empty? ? LIBPATH : ::File.join(LIBPATH, *args)
21
+ end
22
+
23
+ # Returns the lpath for the module. If any arguments are given,
24
+ # they will be joined to the end of the path using
25
+ # <tt>File.join</tt>.
26
+ #
27
+ def self.path( *args )
28
+ args.empty? ? PATH : ::File.join(PATH, *args)
29
+ end
30
+
31
+ # Utility method used to rquire all files ending in .rb that lie in the
32
+ # directory below this file that has the same name as the filename passed
33
+ # in. Optionally, a specific _directory_ name can be passed in such that
34
+ # the _filename_ does not have to be equivalent to the directory.
35
+ #
36
+ def self.require_all_libs_relative_to(fname)
37
+ search_me = File.expand_path(File.join(File.dirname(fname), '**', '*.rb'))
38
+ Dir.glob(search_me).sort.each { |rb| require rb }
39
+ end
40
+
41
+ SimpleParser.require_all_libs_relative_to __FILE__
42
+
43
+ end unless defined?(SimpleParser)
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ddollar-simple-parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - David Dollar
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-07-08 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: ddollar@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ files:
25
+ - lib/simple-parser
26
+ - lib/simple-parser/string.rb
27
+ - lib/simple-parser.rb
28
+ - README
29
+ has_rdoc: true
30
+ homepage: http://peervoice.com/software/simple-parser
31
+ post_install_message:
32
+ rdoc_options: []
33
+
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: "0"
41
+ version:
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ requirements: []
49
+
50
+ rubyforge_project: simple-parser
51
+ rubygems_version: 1.2.0
52
+ signing_key:
53
+ specification_version: 2
54
+ summary: Simple parser
55
+ test_files: []
56
+