Class: InsertInto

Inherits:
Object
  • Object
show all
Defined in:
lib/insert_into.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInsertInto

Returns a new instance of InsertInto.



8
9
10
11
12
13
# File 'lib/insert_into.rb', line 8

def initialize
  @insert_text = ""
  @into_text = Hpricot("")
  @target_tag = ""
  @type = :between
end

Instance Attribute Details

#insert_textObject (readonly)

Returns the value of attribute insert_text.



6
7
8
# File 'lib/insert_into.rb', line 6

def insert_text
  @insert_text
end

#into_textObject (readonly)

Returns the value of attribute into_text.



6
7
8
# File 'lib/insert_into.rb', line 6

def into_text
  @into_text
end

#target_tagObject (readonly)

Returns the value of attribute target_tag.



6
7
8
# File 'lib/insert_into.rb', line 6

def target_tag
  @target_tag
end

#typeObject (readonly)

Returns the value of attribute type.



6
7
8
# File 'lib/insert_into.rb', line 6

def type
  @type
end

Instance Method Details

#after_tag(target_tag) ⇒ Object



53
54
55
56
57
# File 'lib/insert_into.rb', line 53

def after_tag target_tag
  @target_tag = target_tag
  @type = :after
  self
end

#append_to_tag(target_tag) ⇒ Object



41
42
43
44
45
# File 'lib/insert_into.rb', line 41

def append_to_tag target_tag
  @target_tag = target_tag
  @type = :append_to_tag
  self
end

#before_tag(target_tag) ⇒ Object



47
48
49
50
51
# File 'lib/insert_into.rb', line 47

def before_tag target_tag
  @target_tag = target_tag
  @type = :before
  self
end

#between_tag(target_tag) ⇒ Object



29
30
31
32
33
# File 'lib/insert_into.rb', line 29

def between_tag target_tag
  @target_tag = target_tag
  @type = :between
  self
end

#insert(insert_text) ⇒ Object



15
16
17
18
# File 'lib/insert_into.rb', line 15

def insert insert_text
  @insert_text = insert_text
  self
end

#into(into_text) ⇒ Object



20
21
22
23
# File 'lib/insert_into.rb', line 20

def into into_text
  @into_text = Hpricot(into_text)
  self
end

#prepend_to_tag(target_tag) ⇒ Object



35
36
37
38
39
# File 'lib/insert_into.rb', line 35

def prepend_to_tag target_tag
  @target_tag = target_tag
  @type = :prepend_to_tag
  self
end

#processObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/insert_into.rb', line 59

def process
  return @insert_text << into_text if @target_tag.empty?
  @into_text.search(@target_tag) do |node|
    case @type
      when :after
        node.after insert_text
      when :before
        node.before insert_text
      when :prepend_to_tag
        node.inner_html(insert_text + node.inner_html)
      when :append_to_tag
        node.inner_html(node.inner_html + insert_text)
      when :between
        node.inner_html insert_text
    end
  end
  into_text
end