class Excalibur::TruncateableContent
The TruncateableContent
class is responsible for text content that is constrained in length due to SEO specifications. Mainly used for title and meta description tags this class facilitates the creation and proper rendering of the content. A prefix/body/suffix data structure is used so that only the body is truncated without having an effect on the branding that is put before and/or after the body.
Attributes
combinator[RW]
content[RW]
options[RW]
Public Class Methods
new(content = {}, options = {}, combinator = nil)
click to toggle source
# File lib/excalibur/truncatable_content.rb, line 15 def initialize(content = {}, options = {}, combinator = nil) @content = ::HashWithIndifferentAccess.new(content) @options = ::HashWithIndifferentAccess.new(options) @combinator = combinator end
Public Instance Methods
can_merge?(obj)
click to toggle source
# File lib/excalibur/truncatable_content.rb, line 21 def can_merge?(obj) obj.is_a? TruncateableContent end
dup()
click to toggle source
# File lib/excalibur/truncatable_content.rb, line 38 def dup self.class.new( dup_instance(@content), dup_instance(@options), dup_instance(@combinator) ) end
get_content(key, obj = nil)
click to toggle source
# File lib/excalibur/truncatable_content.rb, line 46 def get_content(key, obj = nil) if @content[key].instance_of?(Proc) @content[key].call(obj) elsif @content[key].nil? '' else @content[key] end end
merge!(obj)
click to toggle source
# File lib/excalibur/truncatable_content.rb, line 25 def merge!(obj) if can_merge?(obj) @content.merge!(obj.content) @options.merge!(obj.options) @combinator = obj.combinator unless obj.combinator.nil? self else fail(TypeError.new(true), 'can only merge two Excalibur::TruncateableContent objects') end end
render_long(obj = nil)
click to toggle source
# File lib/excalibur/truncatable_content.rb, line 68 def render_long(obj = nil) @content.map { |key, _value| get_content(key, obj).to_s }.inject(:+).to_s end
render_short(obj = nil)
click to toggle source
# File lib/excalibur/truncatable_content.rb, line 72 def render_short(obj = nil) if @combinator.instance_of? Proc @combinator.call(obj) else render_long(obj) end end
Also aliased as: to_s
update_combinator(value = nil)
click to toggle source
# File lib/excalibur/truncatable_content.rb, line 64 def update_combinator(value = nil) @combinator = value end
update_content(key, value = nil)
click to toggle source
# File lib/excalibur/truncatable_content.rb, line 56 def update_content(key, value = nil) @content[key] = value end
update_option(key, value = nil)
click to toggle source
# File lib/excalibur/truncatable_content.rb, line 60 def update_option(key, value = nil) @options[key] = value end