Class: Docx::Elements::Style::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/docx/elements/style.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, selectors, required: false, converter:, validator:) ⇒ Attribute

Returns a new instance of Attribute.



14
15
16
17
18
19
20
# File 'lib/docx/elements/style.rb', line 14

def initialize(name, selectors, required: false, converter:, validator:)
  @name = name
  @selectors = selectors
  @required = required
  @converter = converter || Converters::DefaultValueConverter
  @validator = validator || Validators::DefaultValidator
end

Instance Attribute Details

#converterObject (readonly)

Returns the value of attribute converter.



12
13
14
# File 'lib/docx/elements/style.rb', line 12

def converter
  @converter
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/docx/elements/style.rb', line 12

def name
  @name
end

#requiredObject (readonly)

Returns the value of attribute required.



12
13
14
# File 'lib/docx/elements/style.rb', line 12

def required
  @required
end

#selectorsObject (readonly)

Returns the value of attribute selectors.



12
13
14
# File 'lib/docx/elements/style.rb', line 12

def selectors
  @selectors
end

#validatorObject (readonly)

Returns the value of attribute validator.



12
13
14
# File 'lib/docx/elements/style.rb', line 12

def validator
  @validator
end

Instance Method Details

#assign_to(style, value) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/docx/elements/style.rb', line 34

def assign_to(style, value)
  (required && value.nil?) &&
    raise(Errors::StyleRequiredPropertyValue, "Required value #{name}")

  validator.validate(value) ||
    raise(Errors::StyleInvalidPropertyValue, "Invalid value for #{name}: '#{value.nil? ? "nil" : value}'")

  encoded_value = converter.encode(value)

  selectors.map do |attribute_xpath|
    if (existing_attribute = style.node.at_xpath(attribute_xpath))
      if encoded_value.nil?
        existing_attribute.remove
      else
        existing_attribute.value = encoded_value.to_s
      end

      next encoded_value
    end

    next encoded_value if encoded_value.nil?

    node_xpath, attribute = attribute_xpath.split("/@")

    created_node =
      node_xpath
        .split("/")
        .reduce(style.node) do |parent_node, child_xpath|
          # find the child node
          parent_node.at_xpath(child_xpath) ||
            # or create the child node
            Nokogiri::XML::Node.new(child_xpath, parent_node)
              .tap { |created_child_node| parent_node << created_child_node }
        end

    created_node.set_attribute(attribute, encoded_value)
  end
    .first
end

#required?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/docx/elements/style.rb', line 22

def required?
  required
end

#retrieve_from(style) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/docx/elements/style.rb', line 26

def retrieve_from(style)
  selectors
    .lazy
    .filter_map { |node_xpath| style.node.at_xpath(node_xpath)&.value }
    .map { |value| converter.decode(value) }
    .first
end