parsey 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,23 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
22
+ .yardoc/
23
+ doc/
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Joshua Hawxwell
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.
@@ -0,0 +1,25 @@
1
+ # parsey
2
+
3
+ Parsey is a very simple class to match a string with a pattern and retrieve data from it.
4
+ It takes a string, a pattern, and a hash of regexes. The pattern is filled with the regexes
5
+ and then that is matched to the string given.
6
+
7
+ The pattern uses {} to surround the name of the regex it should be replaced with. You can
8
+ also use <> to surround parts of the pattern that are optional, though these obviously
9
+ must be nested properly.
10
+
11
+ ## Example
12
+
13
+ partials = {'folder' => '([a-zA-Z0-9-]+)',
14
+ 'file-name' => '([a-zA-Z0-9_ -]+)',
15
+ 'ext' => '(txt|jpg|png)'}
16
+
17
+ Parsey.parse('my-folder/my file.txt', '{folder}/{file-name}.{ext}', partials)
18
+ #=> {"folder"=>"my-folder", "file-name"=>"my file", "ext"=>"txt"}
19
+
20
+ Parsey.parse('my file.txt', '<{folder}/>{file-name}.{ext}', partials)
21
+ #=> {"folder"=>nil, "file-name"=>"my file", "ext"=>"txt"}
22
+
23
+ ## Copyright
24
+
25
+ Copyright (c) 2010 Joshua Hawxwell. See LICENSE for details.
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "parsey"
8
+ gem.summary = %Q{Parsey matches strings to patterns}
9
+ gem.description = %Q{Parsey matches a string with a pattern to retrieve data from it.}
10
+ gem.email = "m@hawx.me"
11
+ gem.homepage = "http://github.com/hawx/parsey"
12
+ gem.authors = ["Joshua Hawxwell"]
13
+ gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
14
+ gem.add_development_dependency "yard", ">= 0"
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ require 'rake/testtask'
23
+ Rake::TestTask.new(:test) do |test|
24
+ test.libs << 'lib' << 'test'
25
+ test.pattern = 'test/**/test_*.rb'
26
+ test.verbose = true
27
+ end
28
+
29
+ begin
30
+ require 'rcov/rcovtask'
31
+ Rcov::RcovTask.new do |test|
32
+ test.libs << 'test'
33
+ test.pattern = 'test/**/test_*.rb'
34
+ test.verbose = true
35
+ end
36
+ rescue LoadError
37
+ task :rcov do
38
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
39
+ end
40
+ end
41
+
42
+ task :test => :check_dependencies
43
+
44
+ task :default => :test
45
+
46
+ begin
47
+ require 'yard'
48
+ YARD::Rake::YardocTask.new
49
+ rescue LoadError
50
+ task :yardoc do
51
+ abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
52
+ end
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,130 @@
1
+ # Parsey is a very simple class to match a string with a pattern and retrieve data from it.
2
+ # It takes a string, a pattern, and a hash of regexes. The pattern is filled with the regexes
3
+ # and then that is matched to the string given.
4
+ #
5
+ # The pattern uses {} to surround the name of the regex it should be replaced with. You can
6
+ # also use <> to surround parts of the pattern that are optional, though these obviously
7
+ # must be nested properly.
8
+ #
9
+ # @example
10
+ #
11
+ # partials = {'folder' => '([a-zA-Z0-9-]+)',
12
+ # 'file-name' => '([a-zA-Z0-9_ -]+)',
13
+ # 'ext' => '(txt|jpg|png)'}
14
+ #
15
+ # Parsey.parse('my-folder/my file.txt', '{folder}/{file-name}.{ext}', partials)
16
+ # #=> {"folder"=>"my-folder", "file-name"=>"my file", "ext"=>"txt"}
17
+ #
18
+ # Parsey.parse('my file.txt', '<{folder}/>{file-name}.{ext}', partials)
19
+ # #=> {"folder"=>nil, "file-name"=>"my file", "ext"=>"txt"}
20
+ #
21
+ class Parsey
22
+
23
+ attr_accessor :to_parse, :pattern, :partials, :data
24
+
25
+ # Creates a new Parsey instance.
26
+ #
27
+ # @param [String] to_parse
28
+ # the string which is to be parsed
29
+ # @param [String] pattern
30
+ # for the string to match
31
+ # @param [Hash{String => String}] partials
32
+ # the regex patterns (as strings) to use when matching
33
+ #
34
+ def initialize(to_parse, pattern, partials)
35
+ @to_parse = to_parse
36
+ @pattern = pattern
37
+ @partials = partials
38
+
39
+ @data = {}
40
+ end
41
+
42
+ # Runs through +pattern+ and replaces each of the keywords with the
43
+ # correct regex from +partials+. It then adds '()?' round any parts of
44
+ # the pattern marked optional. And turns the final string into a regex.
45
+ #
46
+ # @return [Regex]
47
+ # the regular expression to match against when parsing
48
+ #
49
+ def regex
50
+ m = @pattern.gsub(/\{([a-z-]+)\}/) do
51
+ @partials[$1]
52
+ end
53
+
54
+ # replace optional '<stuff>'
55
+ m.gsub!(/<(.+)>/) do
56
+ "(#{$1})?"
57
+ end
58
+
59
+ Regexp.new(m)
60
+ end
61
+
62
+ # Gets the order of the different tags within the pattern. It inserts nil
63
+ # when it encounters an optional section so that it can easily be skipped
64
+ # during parsing.
65
+ #
66
+ # @return [Array]
67
+ # the order in which the tags appear in the +pattern+
68
+ #
69
+ def order
70
+ if @pattern =~ /<(.+)>/
71
+ parts = @pattern.dup.split('<')
72
+ parts.insert(1, nil)
73
+ parts.collect! {|i|
74
+ i.split('>') unless i.nil?
75
+ }.flatten!
76
+
77
+ parts.collect! {|i|
78
+ i.split('}') unless i.nil?
79
+ }.flatten!
80
+
81
+ parts.collect! {|i|
82
+ i.gsub!(/[^a-zA-Z0-9]/, '') unless i.nil?
83
+ }
84
+
85
+ parts.delete_if {|i| i == ''}
86
+
87
+ return parts
88
+ else
89
+ parts = []
90
+ @pattern.gsub(/\{([a-z-]+)\}/) do
91
+ parts << $1
92
+ end
93
+ return parts
94
+ end
95
+ end
96
+
97
+ # This does the parsing of +to_parse+ using +regex+. It fills the hash
98
+ # +data+ using +order+ to match the data up with the correct name.
99
+ #
100
+ # @return [Hash{String => String}]
101
+ # the data retrieved from +to_parse+
102
+ #
103
+ def parse
104
+ @to_parse.match( self.regex ).captures.each_with_index do |item, i|
105
+ unless self.order[i].nil?
106
+ @data[ self.order[i] ] = item
107
+ end
108
+ end
109
+ @data
110
+ end
111
+
112
+ # This is a convenience method to allow you to easily parse something
113
+ # in just one go!
114
+ #
115
+ # @param [String] to_parse
116
+ # the string which is to be parsed
117
+ # @param [String] pattern
118
+ # for the string to match
119
+ # @param [Hash{String => String}] partials
120
+ # the regex patterns (as strings) to use when matching
121
+ #
122
+ # @return [Hash{String => String}]
123
+ # the data retrieved from +to_parse+
124
+ #
125
+ def self.parse(to_parse, pattern, partials)
126
+ a = Parsey.new(to_parse, pattern, partials)
127
+ a.parse
128
+ end
129
+
130
+ end
@@ -0,0 +1,57 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{parsey}
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 = ["Joshua Hawxwell"]
12
+ s.date = %q{2010-07-15}
13
+ s.description = %q{Parsey matches a string with a pattern to retrieve data from it.}
14
+ s.email = %q{m@hawx.me}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.markdown"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.markdown",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/parsey.rb",
27
+ "parsey.gemspec",
28
+ "test/helper.rb",
29
+ "test/test_parsey.rb"
30
+ ]
31
+ s.homepage = %q{http://github.com/hawx/parsey}
32
+ s.rdoc_options = ["--charset=UTF-8"]
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = %q{1.3.7}
35
+ s.summary = %q{Parsey matches strings to patterns}
36
+ s.test_files = [
37
+ "test/helper.rb",
38
+ "test/test_parsey.rb"
39
+ ]
40
+
41
+ if s.respond_to? :specification_version then
42
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
43
+ s.specification_version = 3
44
+
45
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
46
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
47
+ s.add_development_dependency(%q<yard>, [">= 0"])
48
+ else
49
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
50
+ s.add_dependency(%q<yard>, [">= 0"])
51
+ end
52
+ else
53
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
54
+ s.add_dependency(%q<yard>, [">= 0"])
55
+ end
56
+ end
57
+
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'parsey'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,36 @@
1
+ require 'helper'
2
+
3
+ class TestParsey < Test::Unit::TestCase
4
+
5
+ should "construct correct regular expression" do
6
+ partials = {'test' => 'hello', 'something' => 'world'}
7
+ t = Parsey.new('', '{test}|{something}', partials)
8
+ assert_equal %r{hello|world}, t.regex
9
+ end
10
+
11
+ should "allow optional part of pattern" do
12
+ partials = {'folder' => 'f', 'name' => 'n', 'ext' => 'e'}
13
+ t = Parsey.new('', '<{folder}>/{name}.{ext}', partials)
14
+ assert_equal Regexp.new("(f)?\/n.e"), t.regex
15
+ end
16
+
17
+ should "create correct order" do
18
+ partials = {'folder' => 'f', 'name' => 'n', 'ext' => 'e'}
19
+ t = Parsey.new('', '<{folder}/>{name}.{ext}', partials)
20
+ assert_equal [nil, 'folder', 'name', 'ext'], t.order
21
+ end
22
+
23
+ should "create correct order when optional is in the middle" do
24
+ partials = {'folder' => 'folder', 'name' => 'name', 'ext' => 'ext'}
25
+ t = Parsey.new('', '{folder}/<{name}>.{ext}', partials)
26
+ assert_equal ['folder', nil, 'name', 'ext'], t.order
27
+ end
28
+
29
+ should "parse properly" do
30
+ partials = {'test' => '([a-z]+)', 'something' => '([a-z]+)'}
31
+ t = Parsey.new('something:what', '{test}:{something}', partials)
32
+ hash = {'test' => 'something', 'something' => 'what'}
33
+ assert_equal hash, t.parse
34
+ end
35
+
36
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: parsey
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
+ - Joshua Hawxwell
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-07-15 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: thoughtbot-shoulda
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
35
+ - !ruby/object:Gem::Dependency
36
+ name: yard
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id002
49
+ description: Parsey matches a string with a pattern to retrieve data from it.
50
+ email: m@hawx.me
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ extra_rdoc_files:
56
+ - LICENSE
57
+ - README.markdown
58
+ files:
59
+ - .document
60
+ - .gitignore
61
+ - LICENSE
62
+ - README.markdown
63
+ - Rakefile
64
+ - VERSION
65
+ - lib/parsey.rb
66
+ - parsey.gemspec
67
+ - test/helper.rb
68
+ - test/test_parsey.rb
69
+ has_rdoc: true
70
+ homepage: http://github.com/hawx/parsey
71
+ licenses: []
72
+
73
+ post_install_message:
74
+ rdoc_options:
75
+ - --charset=UTF-8
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ hash: 3
84
+ segments:
85
+ - 0
86
+ version: "0"
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ hash: 3
93
+ segments:
94
+ - 0
95
+ version: "0"
96
+ requirements: []
97
+
98
+ rubyforge_project:
99
+ rubygems_version: 1.3.7
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: Parsey matches strings to patterns
103
+ test_files:
104
+ - test/helper.rb
105
+ - test/test_parsey.rb