rasantiago-tiny_xpath_helper 0.1.4 → 0.1.5

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.
data/README.rdoc CHANGED
@@ -34,12 +34,6 @@ and :find can be :first or :all
34
34
  >> xpath.find_xpath( 'node/@style', :format => :rexml, :find => :all )
35
35
  => [style='first']
36
36
 
37
- If you only need to set :format, you may pass just the symbol instead of an options hash
38
- This is especially convenient using the [] syntax since a bug in ruby1.8
39
- prevents using => inside of []
40
- >> xpath[ 'node/@style', :rexml ]
41
- => [style='first']
42
-
43
37
  If you want to filter the output, you can pass something that can be to_proc'd as :format
44
38
  (Note that this acts on the string value of the element, not the REXML node)
45
39
  >> xpath[ 'node/@style', :to_i ]
@@ -51,10 +45,31 @@ If you want to filter the output, you can pass something that can be to_proc'd a
51
45
  >> xpath[ 'node', lambda{|x| x.strip.split(//) } ]
52
46
  => [["o", "n", "e"], ["t", "w", "o"], ["t", "h", "r", "e", "e"]]
53
47
 
48
+ If you only need to set :format, you may pass just the symbol instead of an options hash
49
+ This is especially convenient using the [] syntax since a bug in ruby1.8
50
+ prevents using => inside of []
51
+ >> xpath[ 'node/@style', :rexml ]
52
+ => [style='first']
53
+
54
+ Actually, formats :rexml and :text are aliases for the :auto_text parameter, which takes a bool,
55
+ and defaults to true.
56
+ >> xpath.all( 'node/@style', :auto_text => false )
57
+ => [style='first']
58
+
59
+ Option defaults can be passed to TinyXPathHelper#new
60
+ >> xpath_with_options = TinyXPathHelper.new( File.open( 'test/xml/sample.xml' ), :format => :to_i, :find => :all )
61
+ >> xpath_with_options.find_xpath( 'node' )
62
+ => [0,0,0]
63
+
64
+ You can access the default options for the TinyXMLHelper Class or any instance with #default_options
65
+ >> TinyXPathHelper.default_options
66
+ => {:format => nil, :auto_text => true, :find => :first}
67
+ >> xpath_with_options.default_options
68
+ => {:format => :to_i, :auto_text => true, :find => :all}
54
69
 
55
70
  You can also pass a block, which will get called on the entire return value, but
56
71
  only if at least one element is found
57
- >> xpath.all( 'node' ){ |nodes| nodes.count } # nodes is an array
72
+ >> xpath.all( 'node' ){ |nodes| nodes.count } # nodes is an array of strings
58
73
  => 3
59
74
 
60
75
  >> xpath.first( 'node' ){ |node| node.reverse } # node is a string
@@ -66,9 +81,10 @@ only if at least one element is found
66
81
  >> xpath.all( 'nothing' ){ |nodes| raise "this will not run" }
67
82
  => []
68
83
 
69
- There's no shortcut for the case where you want to run something over the array of matching REXML nodes.
70
- Just use Array#map for that.
71
- >> xpath[ 'node', :rexml ].map(&:name)
84
+ If you really want shortcut for the case where you want to run something
85
+ over the array of matching REXML nodes, set :auto_text to false when calling TinyXpathHelper#new
86
+ >> rexml_xpath = TinyXPathHelper.new( File.open( 'test/xml/sample.xml' ), :auto_text => false )
87
+ >> rexml_xpath[ 'node', :name ]
72
88
  => ['node', 'node', 'node']
73
89
 
74
90
  The output from :format => :rexml is suitable for passing to another TinyXPathHelper
@@ -10,11 +10,11 @@ class TinyXPathHelper
10
10
  end
11
11
 
12
12
  def default_options
13
- @options.dup
13
+ self.class.default_options.dup.update(@options)
14
14
  end
15
15
 
16
16
  def with_options(*options)
17
- r = default_options
17
+ r = default_options.dup
18
18
 
19
19
  options.each do |option|
20
20
  if not Hash === option
@@ -77,32 +77,33 @@ class TinyXPathHelper
77
77
  end
78
78
  end
79
79
 
80
+ def self.default_options
81
+ {:format => nil, :auto_text => true, :find => :first}.freeze
82
+ end
83
+
80
84
  def self.find_xpath_from(element, path, options = {}, &blk)
81
85
  if options[:with_indifferent_access]
82
86
  path = path.to_s
83
87
  end
84
88
 
85
- format = options[:format] || :text
86
- if format == :array
87
- options.update( :format => :text, :find => :all )
88
- format = :text
89
- end
90
-
91
- count = options[:find] || :first
92
-
93
- filter1 = self.method(:xml_node_to_text)
94
- filter2 = nil
89
+ options = self.default_options.dup.update(options)
90
+ format = options[:format]
91
+ auto_text = options[:auto_text]
92
+ count = options[:find]
95
93
 
96
- if format == :xml or format == :rexml
97
- filter1 = nil
94
+ if format == :array
95
+ count = :all
96
+ format = nil
98
97
  elsif format == :text
99
- filter2 = nil
100
- elsif format.respond_to? :to_proc
101
- filter2 = format.to_proc
102
- else
103
- raise "I don't know the format #{format.inspect}"
98
+ auto_text = true
99
+ format = nil
100
+ elsif format == :xml or format == :rexml
101
+ auto_text = false
102
+ format = nil
104
103
  end
105
104
 
105
+ filter1 = auto_text ? self.method(:xml_node_to_text) : nil
106
+ filter2 = format
106
107
 
107
108
  if count == :all
108
109
  elements = REXML::XPath.match(element, path)
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{tiny_xpath_helper}
5
- s.version = "0.1.4"
5
+ s.version = "0.1.5"
6
6
 
7
7
  s.authors = ["Jesse Wolfe"]
8
8
  s.date = %q{2009-06-01}
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rasantiago-tiny_xpath_helper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jesse Wolfe