swissparser 0.9.0 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -4,5 +4,6 @@
4
4
  *.gemspec
5
5
  announcement.txt
6
6
 
7
+ coverage/
7
8
  doc/
8
9
  pkg/
@@ -1,3 +1,14 @@
1
+ == 0.10.0 / 2009-11-14
2
+
3
+ * 1 new feature:
4
+ - SwissParser can now parse an input string or whatever input responding to the +each_line+ method
5
+
6
+ * 1 bug fix:
7
+ - +with_text_after+ and +set_separator+ can now be properly replaced when extended.
8
+
9
+ * Misc:
10
+ - First release with tests using cucumber
11
+
1
12
  == 0.9.0 / 2009-11-14
2
13
 
3
14
  * 1 new feature:
data/Rakefile CHANGED
@@ -7,8 +7,16 @@ end
7
7
  ensure_in_path 'lib'
8
8
  require 'swissparser'
9
9
 
10
- #task :default => 'test:run'
11
- #task 'gem:release' => 'test:run'
10
+ require 'cucumber/rake/task'
11
+
12
+ task :default => :features
13
+
14
+ Cucumber::Rake::Task.new(:features) do |t|
15
+ t.cucumber_opts = "--format pretty"
16
+ t.rcov = true
17
+ end
18
+
19
+ CLOBBER << "coverage/"
12
20
 
13
21
  Bones {
14
22
  name 'swissparser'
@@ -16,6 +24,7 @@ Bones {
16
24
  email 'paradigmatic@streum.org'
17
25
  url 'http://github.com/paradigmatic/SwissParser'
18
26
  version Swiss::VERSION
27
+ gem.development_dependencies = [["cucumber", ">= 0.4"]]
19
28
  readme_file 'README.rdoc'
20
29
  history_file 'CHANGELOG.rdoc'
21
30
  ignore_file '.gitignore'
@@ -20,7 +20,7 @@ along with SwissParser. If not, see <http://www.gnu.org/licenses/>.
20
20
  #!/usr/bin/ruby -w
21
21
 
22
22
  require 'yaml'
23
- require 'swissparser.rb'
23
+ require 'swissparser'
24
24
 
25
25
  class Protein
26
26
 
@@ -0,0 +1,65 @@
1
+ =begin
2
+ Copyright (C) 2009 Paradigmatic
3
+
4
+ This file is part of SwissParser.
5
+
6
+ SwissParser is free software: you can redistribute it and/or modify
7
+ it under the terms of the GNU General Public License as published by
8
+ the Free Software Foundation, either version 3 of the License, or
9
+ (at your option) any later version.
10
+
11
+ SwissParser is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ GNU General Public License for more details.
15
+
16
+ You should have received a copy of the GNU General Public License
17
+ along with SwissParser. If not, see <http://www.gnu.org/licenses/>.
18
+ =end
19
+
20
+ #!/usr/bin/ruby -w
21
+
22
+ require 'swissparser'
23
+ require 'examples/tutorial_1'
24
+
25
+ class Protein
26
+
27
+ attr_accessor :id, :size, :species, :taxonomy, :sequence
28
+
29
+ def initialize
30
+ @taxonomy = []
31
+ @sequence = ""
32
+ end
33
+
34
+ end
35
+
36
+ module Uniprot
37
+
38
+ SpeciesParser = Uniprot::Parser.extend do
39
+
40
+ before do
41
+ {}
42
+ end
43
+
44
+ finish_entry do |protein, container|
45
+ if container[protein.species].nil?
46
+ container[protein.species] = []
47
+ end
48
+ container[protein.species] << protein
49
+ end
50
+
51
+ end
52
+
53
+ end
54
+
55
+ if $0 == __FILE__
56
+
57
+ filename = ARGV.shift
58
+
59
+ result = Uniprot::SpeciesParser.parse_file( filename )
60
+
61
+ result.each do |species, ary|
62
+ puts "#{species} => #{ary.map{ |p| p.id }.join(', ')}"
63
+ end
64
+
65
+ end
@@ -0,0 +1,53 @@
1
+ Feature: Parser Extension
2
+ I can extend existing parser
3
+ And replace existing rules
4
+
5
+ Background:
6
+ Given input data
7
+ """
8
+ XX a1
9
+ YY b1
10
+ c1
11
+ //
12
+ XX a1
13
+ YY b2
14
+ c2
15
+ //
16
+ """
17
+
18
+ Scenario: Extension without redefinition
19
+ Given a simple parser
20
+ When I extend it
21
+ Then the extended parser should parse it as the original one
22
+
23
+ Scenario: With replacing separator
24
+ Given a simple parser
25
+ When I extend it
26
+ And I replace with("XX") to return always 'foo'
27
+ And I replace with("YY") to do nothing
28
+ Then the parser should return "[{ 'XX' => 'foo'}, { 'XX' => 'foo'}]"
29
+
30
+ Scenario: Text after replacing
31
+ Given a simple parser
32
+ When I extend it
33
+ And I replace with("XX") to do nothing
34
+ And I replace with("YY") to return always 'bar'
35
+ And I replace with_text_after("YY") to return always 'foo'
36
+ Then the parser should return "[{ 'YY' => 'bar', 'txt-YY' => 'foo'}, { 'YY' => 'bar', 'txt-YY' => 'foo'}]"
37
+
38
+ Scenario:
39
+ Given a simple parser
40
+ And input data
41
+ """
42
+ XX a1
43
+ YY b1
44
+ c1
45
+ %
46
+ XX a1
47
+ YY b2
48
+ c2
49
+ %
50
+ """
51
+ When I extend it
52
+ And I set the separator to '%'
53
+ Then the parser should return '2' entries
@@ -0,0 +1,103 @@
1
+ require 'lib/swissparser'
2
+ require 'spec/expectations'
3
+
4
+
5
+ Given /^a simple parser$/ do
6
+ @simple_parser = Swiss::Parser.define do
7
+ rules do
8
+ with("XX") {|c,e| e["XX"] = c}
9
+ with("YY") {|c,e| e["YY"] = c}
10
+ end
11
+ end
12
+ end
13
+
14
+ Given /^input data$/ do |string|
15
+ @data = string
16
+ end
17
+
18
+ When /^I extend it$/ do
19
+ @ext_parser = @simple_parser.extend {}
20
+ end
21
+
22
+ When /^I replace with\("([^\"]*)"\) to return always '([^\']*)'$/ do |key,out|
23
+ @ext_parser = @ext_parser.extend do
24
+ rules do
25
+ with( key ) {|c,e| e[key] = out }
26
+ end
27
+ end
28
+ end
29
+
30
+ When /^I replace with\("([^\"]*)"\) to do nothing$/ do |key|
31
+ @ext_parser = @ext_parser.extend do
32
+ rules do
33
+ with( key ) {|c,e| }
34
+ end
35
+ end
36
+ end
37
+
38
+ When /^I replace with_text_after\("([^\"]*)"\) to return always '([^\']*)'$/ do |key,out|
39
+ text_key = "txt-#{key}"
40
+ @ext_parser = @ext_parser.extend do
41
+ rules do
42
+ with_text_after( key ) {|c,e| e[text_key] = out }
43
+ end
44
+ end
45
+ end
46
+
47
+ When /^I set the separator to '([^\']*)'$/ do |sep|
48
+ @ext_parser = @ext_parser.extend do
49
+ rules do
50
+ set_separator( sep )
51
+ end
52
+ end
53
+ end
54
+
55
+ When /^I define '([^\']*)' helper$/ do |name|
56
+ @ext_parser = @ext_parser.extend do
57
+ helper(name.to_sym) do
58
+ name
59
+ end
60
+ end
61
+ end
62
+
63
+ When /^I call '([^\']*)' helper in after action$/ do |name|
64
+ l = eval("lambda { |x| #{name} }")
65
+ @ext_parser = @ext_parser.extend do
66
+ after(&l)
67
+ end
68
+ end
69
+
70
+ When /^I return param "([^\"]*)" in after action$/ do |name|
71
+ l = eval("lambda { |x| param(#{name}) }")
72
+ @ext_parser = @ext_parser.extend do
73
+ after(&l)
74
+ end
75
+
76
+ end
77
+
78
+ When /^I call parse with param "([^\"]*)" equal to "([^\"]*)"$/ do |key, val|
79
+ @result = @ext_parser.parse(@data, key => val)
80
+ end
81
+
82
+ Then /^the result should be "([^\"]*)"$/ do |val|
83
+ @result.should == val
84
+ end
85
+
86
+
87
+ Then /^the parser should return '([^\']*)'$/ do |val|
88
+ @ext_parser.parse( @data ).should == val
89
+ end
90
+
91
+ Then /^the parser should return '(\d*)' entries$/ do |num|
92
+ @ext_parser.parse( @data ).size.should == num.to_i
93
+ end
94
+
95
+ Then /^the extended parser should parse it as the original one$/ do
96
+ @simple_parser.parse( @data ).should == @ext_parser.parse( @data )
97
+ end
98
+
99
+
100
+ Then /^the parser should return "([^\"]*)"$/ do |ruby_exp|
101
+ result = eval(ruby_exp)
102
+ @ext_parser.parse( @data ).should == result
103
+ end
@@ -0,0 +1,29 @@
1
+ Feature: User Friendly
2
+ SwissParser is nice to use.
3
+
4
+ Background:
5
+ Given input data
6
+ """
7
+ XX a1
8
+ YY b1
9
+ c1
10
+ //
11
+ XX a1
12
+ YY b2
13
+ c2
14
+ //
15
+ """
16
+
17
+ Scenario: Helper Method
18
+ Given a simple parser
19
+ When I extend it
20
+ And I define 'foo' helper
21
+ And I call 'foo' helper in after action
22
+ Then the parser should return 'foo'
23
+
24
+ Scenario: Helper Method
25
+ Given a simple parser
26
+ When I extend it
27
+ And I return param "foo" in after action
28
+ And I call parse with param "foo" equal to "bar"
29
+ Then the result should be "bar"
@@ -21,7 +21,7 @@ require 'open-uri'
21
21
 
22
22
  module Swiss
23
23
 
24
- VERSION = "0.9.0"
24
+ VERSION = "0.10.0"
25
25
 
26
26
  # This class defines parsing rules. Its methods
27
27
  # are accessible within the +rules+ section of
@@ -186,14 +186,15 @@ module Swiss
186
186
  next
187
187
  end
188
188
  @actions[k] = v
189
- r.actions[:text].each do |k,v|
190
- @actions[:text][k] = v
191
- end
192
- if r.separator
193
- @separator = r.separator
194
- end
189
+ end
190
+ r.actions[:text].each do |k,v|
191
+ @actions[:text][k] = v
192
+ end
193
+ if r.separator
194
+ @separator = r.separator
195
195
  end
196
196
  end
197
+
197
198
 
198
199
 
199
200
 
@@ -234,10 +235,14 @@ module Swiss
234
235
  parse( file, params )
235
236
  end
236
237
  end
237
-
238
- private
239
238
 
240
- def parse( file, params )
239
+ # Parses any input that accepts the +each_line+ method. Works for
240
+ # string, open files, etc. An optional hash of arbitrary arguments
241
+ # (+params+) can be specified. It is passed to the workflow
242
+ # methods blocks (+before+, +new_entry+, ...) It returns the
243
+ # value specified in the +after+ block. By default, it returns an
244
+ # array containing _entry_ objects.
245
+ def parse( data, params={} )
241
246
  @ctx = ParsingContext.new( params )
242
247
  helperModule = Module.new
243
248
  @helpers.each do |name, proc|
@@ -246,7 +251,7 @@ module Swiss
246
251
  @ctx.extend( helperModule )
247
252
  container = @ctx.instance_exec( &@before )
248
253
  entry = @ctx.instance_exec( &@begin )
249
- file.each_line do |line|
254
+ data.each_line do |line|
250
255
  state = parse_line( line, entry )
251
256
  if state == :end
252
257
  @ctx.instance_exec( entry, container, &@end )
@@ -256,6 +261,8 @@ module Swiss
256
261
  @ctx.instance_exec( container, &@after )
257
262
  end
258
263
 
264
+ private
265
+
259
266
  PROTOTYPE = Parser.new
260
267
  PROTOTYPE.instance_eval do
261
268
  before { || [] }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swissparser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - paradigmatic
@@ -9,9 +9,19 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-14 00:00:00 +01:00
12
+ date: 2009-11-15 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: cucumber
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0.4"
24
+ version:
15
25
  - !ruby/object:Gem::Dependency
16
26
  name: bones
17
27
  type: :development
@@ -44,7 +54,11 @@ files:
44
54
  - examples/parse_from_uri.rb
45
55
  - examples/signal_demo.rb
46
56
  - examples/tutorial_1.rb
57
+ - examples/tutorial_2.rb
47
58
  - examples/uniprot_param_demo.rb
59
+ - features/parser_extension.feature
60
+ - features/step_definitions/steps.rb
61
+ - features/user_friendly.feature
48
62
  - lib/swiss_parser.rb
49
63
  - lib/swissparser.rb
50
64
  has_rdoc: true