Class: ActionText::Fragment
- Inherits:
-
Object
- Object
- ActionText::Fragment
- Defined in:
- lib/action_text/fragment.rb
Instance Attribute Summary collapse
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Class Method Summary collapse
Instance Method Summary collapse
- #find_all(selector) ⇒ Object
-
#initialize(source) ⇒ Fragment
constructor
A new instance of Fragment.
- #replace(selector) ⇒ Object
- #to_html ⇒ Object
- #to_plain_text ⇒ Object
- #to_s ⇒ Object
- #update {|source = self.source.dup| ... } ⇒ Object
Constructor Details
#initialize(source) ⇒ Fragment
Returns a new instance of Fragment.
28 29 30 |
# File 'lib/action_text/fragment.rb', line 28 def initialize(source) @source = source end |
Instance Attribute Details
#source ⇒ Object (readonly)
Returns the value of attribute source.
24 25 26 |
# File 'lib/action_text/fragment.rb', line 24 def source @source end |
Class Method Details
.from_html(html) ⇒ Object
19 20 21 |
# File 'lib/action_text/fragment.rb', line 19 def from_html(html) new(ActionText::HtmlConversion.fragment_for_html(html.to_s.strip)) end |
.wrap(fragment_or_html) ⇒ Object
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/action_text/fragment.rb', line 8 def wrap(fragment_or_html) case fragment_or_html when self fragment_or_html when Nokogiri::XML::DocumentFragment # base class for all fragments new(fragment_or_html) else from_html(fragment_or_html) end end |
Instance Method Details
#find_all(selector) ⇒ Object
32 33 34 |
# File 'lib/action_text/fragment.rb', line 32 def find_all(selector) source.css(selector) end |
#replace(selector) ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/action_text/fragment.rb', line 41 def replace(selector) update do |source| source.css(selector).each do |node| replacement_node = yield(node) node.replace(replacement_node.to_s) if node != replacement_node end end end |
#to_html ⇒ Object
54 55 56 |
# File 'lib/action_text/fragment.rb', line 54 def to_html @html ||= HtmlConversion.node_to_html(source) end |
#to_plain_text ⇒ Object
50 51 52 |
# File 'lib/action_text/fragment.rb', line 50 def to_plain_text @plain_text ||= PlainTextConversion.node_to_plain_text(source) end |
#to_s ⇒ Object
58 59 60 |
# File 'lib/action_text/fragment.rb', line 58 def to_s to_html end |
#update {|source = self.source.dup| ... } ⇒ Object
36 37 38 39 |
# File 'lib/action_text/fragment.rb', line 36 def update yield source = self.source.dup self.class.new(source) end |