Class: Excalibur::TruncateableContent

Inherits:
Object
  • Object
show all
Includes:
Duplicator
Defined in:
lib/excalibur/truncatable_content.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Duplicator

#dup_instance

Constructor Details

#initialize(content = {}, options = {}, combinator = nil) ⇒ TruncateableContent

Returns a new instance of TruncateableContent.



15
16
17
18
19
# 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

Instance Attribute Details

#combinatorObject

Returns the value of attribute combinator.



13
14
15
# File 'lib/excalibur/truncatable_content.rb', line 13

def combinator
  @combinator
end

#contentObject

Returns the value of attribute content.



11
12
13
# File 'lib/excalibur/truncatable_content.rb', line 11

def content
  @content
end

#optionsObject

Returns the value of attribute options.



12
13
14
# File 'lib/excalibur/truncatable_content.rb', line 12

def options
  @options
end

Instance Method Details

#can_merge?(obj) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/excalibur/truncatable_content.rb', line 21

def can_merge?(obj)
  obj.is_a? TruncateableContent
end

#dupObject



38
39
40
41
42
43
44
# 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) ⇒ Object



46
47
48
49
50
51
52
53
54
# 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) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# 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) ⇒ Object



68
69
70
# 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) ⇒ Object Also known as: to_s



72
73
74
75
76
77
78
# 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

#update_combinator(value = nil) ⇒ Object



64
65
66
# File 'lib/excalibur/truncatable_content.rb', line 64

def update_combinator(value = nil)
  @combinator = value
end

#update_content(key, value = nil) ⇒ Object



56
57
58
# File 'lib/excalibur/truncatable_content.rb', line 56

def update_content(key, value = nil)
  @content[key] = value
end

#update_option(key, value = nil) ⇒ Object



60
61
62
# File 'lib/excalibur/truncatable_content.rb', line 60

def update_option(key, value = nil)
  @options[key] = value
end