Class: ActionMosaico::Fragment
- Inherits:
-
Object
- Object
- ActionMosaico::Fragment
- Defined in:
- lib/action_mosaico/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.clone| ... } ⇒ Object
Constructor Details
#initialize(source) ⇒ Fragment
Returns a new instance of Fragment.
24 25 26 |
# File 'lib/action_mosaico/fragment.rb', line 24 def initialize(source) @source = source end |
Instance Attribute Details
#source ⇒ Object (readonly)
Returns the value of attribute source.
22 23 24 |
# File 'lib/action_mosaico/fragment.rb', line 22 def source @source end |
Class Method Details
.from_html(html) ⇒ Object
17 18 19 |
# File 'lib/action_mosaico/fragment.rb', line 17 def from_html(html) new(ActionMosaico::HtmlConversion.fragment_for_html(html.to_s.strip)) end |
.wrap(fragment_or_html) ⇒ Object
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/action_mosaico/fragment.rb', line 6 def wrap(fragment_or_html) case fragment_or_html when self fragment_or_html when Nokogiri::HTML::DocumentFragment new(fragment_or_html) else from_html(fragment_or_html) end end |
Instance Method Details
#find_all(selector) ⇒ Object
28 29 30 |
# File 'lib/action_mosaico/fragment.rb', line 28 def find_all(selector) source.css(selector) end |
#replace(selector) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/action_mosaico/fragment.rb', line 37 def replace(selector) update do |source| source.css(selector).each do |node| node.replace(yield(node).to_s) end end end |
#to_html ⇒ Object
49 50 51 |
# File 'lib/action_mosaico/fragment.rb', line 49 def to_html @html ||= HtmlConversion.node_to_html(source) end |
#to_plain_text ⇒ Object
45 46 47 |
# File 'lib/action_mosaico/fragment.rb', line 45 def to_plain_text @plain_text ||= PlainTextConversion.node_to_plain_text(source) end |
#to_s ⇒ Object
53 54 55 |
# File 'lib/action_mosaico/fragment.rb', line 53 def to_s to_html end |
#update {|source = self.source.clone| ... } ⇒ Object
32 33 34 35 |
# File 'lib/action_mosaico/fragment.rb', line 32 def update yield source = self.source.clone self.class.new(source) end |