Class: Docx::Elements::Style

Inherits:
Object
  • Object
show all
Includes:
SimpleInspect
Defined in:
lib/docx/elements/style.rb,
lib/docx/elements/style/converters.rb,
lib/docx/elements/style/validators.rb

Defined Under Namespace

Modules: Converters, Validators Classes: Attribute

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SimpleInspect

#inspect

Constructor Details

#initialize(configuration, node, **attributes) ⇒ Style

Returns a new instance of Style.



105
106
107
108
109
110
111
112
# File 'lib/docx/elements/style.rb', line 105

def initialize(configuration, node, **attributes)
  @configuration = configuration
  @node = node

  attributes.each do |name, value|
    self.send("#{name}=", value)
  end
end

Class Attribute Details

.attributesObject

Returns the value of attribute attributes.



78
79
80
# File 'lib/docx/elements/style.rb', line 78

def attributes
  @attributes
end

Instance Attribute Details

#nodeObject

Returns the value of attribute node.



114
115
116
# File 'lib/docx/elements/style.rb', line 114

def node
  @node
end

Class Method Details

.attribute(name, *selectors, required: false, converter: nil, validator: nil) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/docx/elements/style.rb', line 84

def attribute(name, *selectors, required: false, converter: nil, validator: nil)
  new_attribute = Attribute.new(name, selectors, required: required, converter: converter, validator: validator)
  attributes << new_attribute

  define_method(name) do
    new_attribute.retrieve_from(self)
  end

  define_method("#{name}=") do |value|
    new_attribute.assign_to(self, value)
  end
end

.create(configuration, attributes = {}) ⇒ Object



97
98
99
100
101
102
# File 'lib/docx/elements/style.rb', line 97

def create(configuration, attributes = {})
  node = Nokogiri::XML::Node.new("w:style", configuration.styles_parent_node)
  configuration.styles_parent_node.add_child(node)

  Elements::Style.new(configuration, node, **attributes)
end

.required_attributesObject



80
81
82
# File 'lib/docx/elements/style.rb', line 80

def required_attributes
  attributes.select(&:required?)
end

Instance Method Details

#removeObject



173
174
175
176
# File 'lib/docx/elements/style.rb', line 173

def remove
  node.remove
  @configuration.styles.delete(self)
end

#to_xmlObject



169
170
171
# File 'lib/docx/elements/style.rb', line 169

def to_xml
  node.to_xml
end

#valid?Boolean

Returns:

  • (Boolean)


161
162
163
164
165
166
167
# File 'lib/docx/elements/style.rb', line 161

def valid?
  self.class.required_attributes.all? do |a|
    attribute_value = a.retrieve_from(self)

    a.validator&.validate(attribute_value)
  end
end