Class: ContentfulConverter::Nodes::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/contentful_converter/nodes/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nokogiri_node = nil, parent = nil) ⇒ Base

Returns a new instance of Base.



8
9
10
11
12
13
# File 'lib/contentful_converter/nodes/base.rb', line 8

def initialize(nokogiri_node = nil, parent = nil)
  @nokogiri_node = nokogiri_node
  @parent = parent
  @node_type = type
  @content = []
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



6
7
8
# File 'lib/contentful_converter/nodes/base.rb', line 6

def content
  @content
end

#node_typeObject (readonly)

Returns the value of attribute node_type.



6
7
8
# File 'lib/contentful_converter/nodes/base.rb', line 6

def node_type
  @node_type
end

#nokogiri_nodeObject (readonly)

Returns the value of attribute nokogiri_node.



6
7
8
# File 'lib/contentful_converter/nodes/base.rb', line 6

def nokogiri_node
  @nokogiri_node
end

#parentObject (readonly)

Returns the value of attribute parent.



6
7
8
# File 'lib/contentful_converter/nodes/base.rb', line 6

def parent
  @parent
end

Instance Method Details

#add_content(node) ⇒ Object



15
16
17
# File 'lib/contentful_converter/nodes/base.rb', line 15

def add_content(node)
  @content << (node.needs_p_wrapping? ? wrap_in_p(node) : node)
end

#needs_p_wrapping?Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
34
35
36
# File 'lib/contentful_converter/nodes/base.rb', line 26

def needs_p_wrapping?
  if parent.nil? ||
     parent&.class == Nodes::Header ||
     parent&.class == Nodes::Paragraph ||
     parent&.class == Nodes::Hyperlink

    return false
  end

  true
end

#to_h(params = options) ⇒ Object



19
20
21
22
23
24
# File 'lib/contentful_converter/nodes/base.rb', line 19

def to_h(params = options)
  params[:nodeType] = node_type
  params[:data]     = params[:data] || {}
  params[:content]  = content.map(&:to_h).compact
  params
end