Class: PageBuilder::Elements::Basic

Inherits:
Nokogiri::XML::Element
  • Object
show all
Defined in:
lib/pagebuilder/elements/basic.rb

Overview

A wrapper for Nokogiri::XML::Element so that we can add extra helpers

Direct Known Subclasses

Anchor, Input

Instance Method Summary collapse

Instance Method Details

#configure(content = nil, **attributes) ⇒ self

Helper to easily set the content and attributes for this element

Parameters:

  • content (String) (defaults to: nil)

    text for the content of the element

  • attributes

    keyword arguments for the attributes that should be set

  • data (Hash)

    a customizable set of options

Returns:

  • (self)


18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/pagebuilder/elements/basic.rb', line 18

def configure(content = nil, **attributes)
  self.content = content if content

  # Deal with helper attributes
  data_attrs = attributes.delete(:data)
  self.data_attributes = data_attrs if data_attrs

  # Set normal attributes
  attributes.each { |k, v| self[k] = v }

  self
end

#data_attributes=(attributes) ⇒ Object

Helper to set data attributes as a single call instead of an individual line for each attribute

Parameters:

  • attributes (Hash)

    data attributes that should be set (minus the “data-” prefix)

Returns:

  • void



35
36
37
38
39
# File 'lib/pagebuilder/elements/basic.rb', line 35

def data_attributes=(attributes)
  attributes.each do |k, v|
    self["data-#{k}"] = v
  end
end