Class: Emjay::Component

Inherits:
Object
  • Object
show all
Defined in:
lib/emjay/component.rb

Overview

Base class for all MJML components. Port of JS Component class from createComponent.js. Takes parsed node data (attributes, children, content) + context hash.

Direct Known Subclasses

BodyComponent, HeadComponent

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(initial_data = {}) ⇒ Component

initialDatas in JS: { attributes, children, content, context, props, globalAttributes, rawAttrs }



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/emjay/component.rb', line 32

def initialize(initial_data = {})
  attrs = initial_data[:attributes] || {}
  @children = initial_data[:children] || []
  @content = initial_data[:content] || ""
  @context = initial_data[:context] || {}
  @props = initial_data[:props] || {}
  @props[:children] = @children
  @props[:content] = @content
  @props[:raw_attrs] = initial_data[:raw_attrs] || {}
  global_attributes = initial_data[:global_attributes] || {}

  # Attribute merging: defaults -> global -> element
  @attributes = self.class.default_attributes
    .merge(global_attributes)
    .merge(attrs)
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



7
8
9
# File 'lib/emjay/component.rb', line 7

def attributes
  @attributes
end

#contextObject (readonly)

Returns the value of attribute context.



7
8
9
# File 'lib/emjay/component.rb', line 7

def context
  @context
end

#propsObject (readonly)

Returns the value of attribute props.



7
8
9
# File 'lib/emjay/component.rb', line 7

def props
  @props
end

Class Method Details

.allowed_attributesObject



26
27
28
# File 'lib/emjay/component.rb', line 26

def allowed_attributes
  {}
end

.component_nameObject

Raises:

  • (NotImplementedError)


10
11
12
# File 'lib/emjay/component.rb', line 10

def component_name
  raise NotImplementedError, "#{name} must define self.component_name"
end

.default_attributesObject



22
23
24
# File 'lib/emjay/component.rb', line 22

def default_attributes
  {}
end

.ending_tag?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/emjay/component.rb', line 14

def ending_tag?
  false
end

.raw_element?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/emjay/component.rb', line 18

def raw_element?
  false
end

Instance Method Details

#get_attribute(name) ⇒ Object



53
54
55
# File 'lib/emjay/component.rb', line 53

def get_attribute(name)
  @attributes[name]
end

#get_child_contextObject



49
50
51
# File 'lib/emjay/component.rb', line 49

def get_child_context
  @context
end

#get_contentObject



57
58
59
# File 'lib/emjay/component.rb', line 57

def get_content
  (@content || "").strip
end