fluentxpath 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ === 0.0.1 2010-04-04
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
@@ -0,0 +1,11 @@
1
+ History.txt
2
+ Manifest.txt
3
+ PostInstall.txt
4
+ README.rdoc
5
+ Rakefile
6
+ lib/fluentxpath.rb
7
+ script/console
8
+ script/destroy
9
+ script/generate
10
+ test/test_fluentxpath.rb
11
+ test/test_helper.rb
@@ -0,0 +1,7 @@
1
+
2
+ For more information on fluentxpath, see http://fluentxpath.rubyforge.org
3
+
4
+ NOTE: Change this information in PostInstall.txt
5
+ You can also delete it if you don't want it.
6
+
7
+
@@ -0,0 +1,63 @@
1
+ = fluentxpath
2
+
3
+ * http://github.com/douglassquirrel/fluentxpath
4
+
5
+ == DESCRIPTION:
6
+
7
+ With the fluentxpath Ruby gem, you build your xpath query in a language that is nearly English and very readable.
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * Navigate to root element or arbitrary element
12
+ * Navigate to children and descendants
13
+ * Specify tag name and attribute values
14
+ * Everything else is missing!
15
+
16
+ == SYNOPSIS:
17
+
18
+ require 'fluentxpath'
19
+ xpath = Fluentxpath::XPath
20
+ xpath.that_navigates { to_root_element.with_name("library") } # "/library"
21
+ xpath.that_navigates { to_any_element.with_name("book") } # "//book"
22
+ xpath.that_navigates { to_root_element.with_name "library"
23
+ and_then.to_children.with_name "subject"
24
+ and_then.to_children.with_name "shelf" } # "/library/subject/shelf"
25
+ xpath.that_navigates { to_root_element.with_name "library"
26
+ and_then.to_descendants.with_name "book" } # "/library//book"
27
+ xpath.that_navigates { to_any_element.with_name("book")
28
+ and_attributes("title" => "The Conquest of Bread",
29
+ "author" => "Kropotkin") } # "//book[@author='Kropotkin' and @title='The Conquest of Bread']"
30
+ # See /test for many more
31
+
32
+ == REQUIREMENTS:
33
+
34
+ * None, though you will want something that can use your xpath expression (like Nokogiri)
35
+
36
+ == INSTALL:
37
+
38
+ * sudo gem install fluentxpath
39
+
40
+ == LICENSE:
41
+
42
+ (The MIT License)
43
+
44
+ Copyright (c) 2010 Douglas Squirrel
45
+
46
+ Permission is hereby granted, free of charge, to any person obtaining
47
+ a copy of this software and associated documentation files (the
48
+ 'Software'), to deal in the Software without restriction, including
49
+ without limitation the rights to use, copy, modify, merge, publish,
50
+ distribute, sublicense, and/or sell copies of the Software, and to
51
+ permit persons to whom the Software is furnished to do so, subject to
52
+ the following conditions:
53
+
54
+ The above copyright notice and this permission notice shall be
55
+ included in all copies or substantial portions of the Software.
56
+
57
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
58
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
59
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
60
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
61
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
62
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
63
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,16 @@
1
+ require 'rubygems'
2
+ gem 'hoe', '>= 2.1.0'
3
+ require 'hoe'
4
+ require 'fileutils'
5
+ require './lib/fluentxpath'
6
+
7
+ Hoe.plugin :newgem
8
+
9
+ # Generate all the Rake tasks
10
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
11
+ $hoe = Hoe.spec 'fluentxpath' do
12
+ self.developer 'Douglas Squirrel', 'ds@douglassquirrel.com'
13
+ self.rubyforge_name = self.name
14
+ end
15
+
16
+ require 'newgem/tasks'
@@ -0,0 +1,76 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ module Fluentxpath
5
+ VERSION = '0.1.0'
6
+
7
+ class XPath
8
+ attr_accessor :expression
9
+
10
+ def self.that_navigates(&block)
11
+ xpath = self.new
12
+ xpath.instance_eval(&block)
13
+ return xpath.expression
14
+ end
15
+
16
+ def initialize
17
+ @expression = ""
18
+ end
19
+
20
+ def to_root_node
21
+ add "/"
22
+ end
23
+
24
+ def to_root_element
25
+ add "/"
26
+ end
27
+
28
+ def to_any_element
29
+ add "//"
30
+ end
31
+
32
+ def with_name(name)
33
+ add name
34
+ end
35
+
36
+ def and_name(name)
37
+ with_name(name)
38
+ end
39
+
40
+ def and_then
41
+ return self
42
+ end
43
+
44
+ def to_children
45
+ add "/"
46
+ end
47
+
48
+ def to_descendants
49
+ add "//"
50
+ end
51
+
52
+ def with_attributes(attributes)
53
+ predicates = []
54
+ attributes.each_pair { |attribute, value| predicates << "@#{attribute}='#{value}'" }
55
+ add "[#{predicates.sort.join(" and ")}]"
56
+ end
57
+
58
+ def and_attributes(attributes)
59
+ with_attributes(attributes)
60
+ end
61
+
62
+ def and_attribute(attributes)
63
+ with_attributes(attributes)
64
+ end
65
+
66
+ def with_attribute(attributes)
67
+ with_attributes(attributes)
68
+ end
69
+
70
+ private
71
+ def add(text)
72
+ @expression += text
73
+ return self
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/fluentxpath.rb'}"
9
+ puts "Loading fluentxpath gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,52 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestFluentxpath < Test::Unit::TestCase
4
+ def setup
5
+ @xpath = Fluentxpath::XPath
6
+ end
7
+
8
+ def test_single_expressions
9
+ assert_equal "/", @xpath.that_navigates { to_root_node }
10
+
11
+ assert_equal "/library", @xpath.that_navigates { to_root_element.with_name "library" }
12
+ assert_equal "/html", @xpath.that_navigates { to_root_element.with_name "html" }
13
+
14
+ assert_equal "//book", @xpath.that_navigates { to_any_element.with_name "book" }
15
+ end
16
+
17
+ def test_navigation
18
+ assert_equal "/library/subject", @xpath.that_navigates { to_root_element.with_name "library"
19
+ and_then.to_children.with_name "subject" }
20
+ assert_equal "/library/subject/shelf", @xpath.that_navigates { to_root_element.with_name "library"
21
+ and_then.to_children.with_name "subject"
22
+ and_then.to_children.with_name "shelf" }
23
+ assert_equal "/library/subject/shelf/book", @xpath.that_navigates { to_root_element.with_name "library"
24
+ and_then.to_children.with_name "subject"
25
+ and_then.to_children.with_name "shelf"
26
+ and_then.to_children.with_name "book" }
27
+ assert_equal "/library//book", @xpath.that_navigates { to_root_element.with_name "library"
28
+ and_then.to_descendants.with_name "book" }
29
+ assert_equal "/library//shelf/book", @xpath.that_navigates { to_root_element.with_name "library"
30
+ and_then.to_descendants.with_name "shelf"
31
+ and_then.to_children.with_name "book" }
32
+ end
33
+
34
+ def test_attributes
35
+ assert_equal "//book[@title='The Conquest of Bread']",
36
+ @xpath.that_navigates { to_any_element.with_name("book")
37
+ and_attribute("title" => "The Conquest of Bread") }
38
+ assert_equal "//book[@author='Kropotkin' and @title='The Conquest of Bread']",
39
+ @xpath.that_navigates { to_any_element.with_name("book")
40
+ and_attributes("title" => "The Conquest of Bread", "author" => "Kropotkin") }
41
+ end
42
+
43
+ def test_real_world_examples
44
+ assert_equal "//div[@id='homeMedals']//tr", @xpath.that_navigates { to_any_element.with_name("div").and_attribute("id" => "homeMedals")
45
+ and_then.to_descendants.with_name("tr") }
46
+ assert_equal "//ul[@id='nav']/li/ul/li/a", @xpath.that_navigates { to_any_element.with_name("ul").and_attribute("id" => "nav")
47
+ and_then.to_children.with_name("li")
48
+ and_then.to_children.with_name("ul")
49
+ and_then.to_children.with_name("li")
50
+ and_then.to_children.with_name("a") }
51
+ end
52
+ end
@@ -0,0 +1,3 @@
1
+ require 'stringio'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/../lib/fluentxpath'
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fluentxpath
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Douglas Squirrel
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-17 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rubyforge
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 2
29
+ - 0
30
+ - 4
31
+ version: 2.0.4
32
+ type: :development
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: hoe
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 2
43
+ - 6
44
+ - 0
45
+ version: 2.6.0
46
+ type: :development
47
+ version_requirements: *id002
48
+ description: With the fluentxpath Ruby gem, you build your xpath query in a language that is nearly English and very readable.
49
+ email:
50
+ - ds@douglassquirrel.com
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ extra_rdoc_files:
56
+ - History.txt
57
+ - Manifest.txt
58
+ - PostInstall.txt
59
+ files:
60
+ - History.txt
61
+ - Manifest.txt
62
+ - PostInstall.txt
63
+ - README.rdoc
64
+ - Rakefile
65
+ - lib/fluentxpath.rb
66
+ - script/console
67
+ - script/destroy
68
+ - script/generate
69
+ - test/test_fluentxpath.rb
70
+ - test/test_helper.rb
71
+ has_rdoc: true
72
+ homepage: http://github.com/douglassquirrel/fluentxpath
73
+ licenses: []
74
+
75
+ post_install_message:
76
+ rdoc_options:
77
+ - --main
78
+ - README.rdoc
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ segments:
86
+ - 0
87
+ version: "0"
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ segments:
93
+ - 0
94
+ version: "0"
95
+ requirements: []
96
+
97
+ rubyforge_project: fluentxpath
98
+ rubygems_version: 1.3.6
99
+ signing_key:
100
+ specification_version: 3
101
+ summary: With the fluentxpath Ruby gem, you build your xpath query in a language that is nearly English and very readable.
102
+ test_files:
103
+ - test/test_fluentxpath.rb
104
+ - test/test_helper.rb