insert_into 0.0.2 → 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.
data/README.rdoc CHANGED
@@ -2,7 +2,17 @@
2
2
 
3
3
  Insert some random text into XML/HTML.
4
4
 
5
- InsertInto.insert("My Text").into("<tag></tag>").between_tag("tag").process
5
+ InsertInto.new.insert("text").into("<wrapper><tag/></wrapper>").between_tag("tag").process
6
+
7
+ Results in:
8
+
9
+ <wrapper><tag>text</tag></wrapper>
10
+
11
+ Supports:
12
+
13
+ between_tag
14
+ before_tag
15
+ after_tag
6
16
 
7
17
  == Note on Patches/Pull Requests
8
18
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.1.0
data/insert_into.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{insert_into}
8
- s.version = "0.0.2"
8
+ s.version = "0.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Richard Hart"]
12
- s.date = %q{2010-03-31}
12
+ s.date = %q{2010-04-02}
13
13
  s.description = %q{Insert text into XML/HTML.}
14
14
  s.email = %q{richard@ur-ban.com}
15
15
  s.extra_rdoc_files = [
data/lib/insert_into.rb CHANGED
@@ -1,14 +1,15 @@
1
1
  class InsertInto
2
2
 
3
3
  require 'rubygems'
4
- require 'nokogiri'
4
+ require 'hpricot'
5
5
 
6
- attr_reader :insert_text, :into_text, :target_tag
6
+ attr_reader :insert_text, :into_text, :target_tag, :type
7
7
 
8
8
  def initialize
9
9
  @insert_text = ""
10
- @into_text = Nokogiri::XML.parse("")
10
+ @into_text = Hpricot("")
11
11
  @target_tag = ""
12
+ @type = :between
12
13
  end
13
14
 
14
15
  def insert insert_text
@@ -17,23 +18,42 @@ class InsertInto
17
18
  end
18
19
 
19
20
  def into into_text
20
- @into_text = Nokogiri::XML.parse(into_text)
21
+ @into_text = Hpricot(into_text)
21
22
  self
22
23
  end
23
24
 
24
25
  def into_text
25
- @into_text.root.to_s.gsub(/[ \n]/,'')
26
+ @into_text.to_s.gsub(/[ \n]/,'')
26
27
  end
27
28
 
28
29
  def between_tag target_tag
29
30
  @target_tag = target_tag
31
+ @type = :between
32
+ self
33
+ end
34
+
35
+ def before_tag target_tag
36
+ @target_tag = target_tag
37
+ @type = :before
38
+ self
39
+ end
40
+
41
+ def after_tag target_tag
42
+ @target_tag = target_tag
43
+ @type = :after
30
44
  self
31
45
  end
32
46
 
33
47
  def process
34
48
  return @insert_text << into_text if @target_tag.empty?
35
- @into_text.search("//#{@target_tag}").each do |node|
36
- node.content = @insert_text
49
+ @into_text.search(@target_tag) do |node|
50
+ if @type == :after
51
+ node.after insert_text
52
+ elsif @type == :before
53
+ node.before insert_text
54
+ else
55
+ node.inner_html insert_text
56
+ end
37
57
  end
38
58
  into_text
39
59
  end
@@ -50,13 +50,23 @@ describe InsertInto do
50
50
  end
51
51
 
52
52
  it "should insert text into a string if target tag is supplied" do
53
- result = InsertInto.new.insert("insert").into("<into/>").between_tag("into").process
54
- result.should == "<into>insert</into>"
53
+ result = InsertInto.new.insert("<foo>insert</foo>").into("<into/>").between_tag("into").process
54
+ result.should == "<into><foo>insert</foo></into>"
55
55
  end
56
56
 
57
57
  it "should insert text multiple times into a string if target tag is supplied" do
58
- result = InsertInto.new.insert("insert").into("<wrapper><into/><into/></wrapper>").between_tag("into").process
59
- result.should == "<wrapper><into>insert</into><into>insert</into></wrapper>"
58
+ result = InsertInto.new.insert("<foo>insert</foo>").into("<bar><into/><into/></bar>").between_tag("into").process
59
+ result.should == "<bar><into><foo>insert</foo></into><into><foo>insert</foo></into></bar>"
60
+ end
61
+
62
+ it "should insert text before a tag" do
63
+ result = InsertInto.new.insert("<foo>insert</foo>").into("<bar><into/></bar>").before_tag("into").process
64
+ result.should == "<bar><foo>insert</foo><into/></bar>"
65
+ end
66
+
67
+ it "should insert text after a tag" do
68
+ result = InsertInto.new.insert("<foo>insert</foo>").into("<bar><into/></bar>").after_tag("into").process
69
+ result.should == "<bar><into/><foo>insert</foo></bar>"
60
70
  end
61
71
 
62
72
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
+ - 1
7
8
  - 0
8
- - 2
9
- version: 0.0.2
9
+ version: 0.1.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Richard Hart
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-31 00:00:00 +01:00
17
+ date: 2010-04-02 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency