Class: Uniword::Ooxml::Schema::ElementDefinition
- Inherits:
-
Object
- Object
- Uniword::Ooxml::Schema::ElementDefinition
- Defined in:
- lib/uniword/ooxml/schema/element_definition.rb
Overview
Represents the definition of ONE OOXML element type.
Responsibility: Hold schema definition for a single element type. Single Responsibility - represents one element’s structure only.
Loaded from external YAML schema configuration.
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
-
#tag ⇒ Object
readonly
Returns the value of attribute tag.
Instance Method Summary collapse
-
#attribute(attr_name) ⇒ AttributeDefinition?
Get attribute definition by name.
-
#child(element_type) ⇒ ChildDefinition?
Get child definition by element type.
-
#has_attributes? ⇒ Boolean
Check if element has attributes.
-
#has_children? ⇒ Boolean
Check if element has children.
-
#initialize(definition) ⇒ ElementDefinition
constructor
Initialize element definition from configuration.
-
#local_name ⇒ String
Get element tag without namespace prefix.
-
#namespace_declarations ⇒ Hash
Get namespace declarations for XML element.
-
#optional_attributes ⇒ Array<AttributeDefinition>
Get optional attributes.
-
#optional_children ⇒ Array<ChildDefinition>
Get optional children.
-
#prefix ⇒ String?
Get namespace prefix.
-
#required_attributes ⇒ Array<AttributeDefinition>
Get required attributes.
-
#required_children ⇒ Array<ChildDefinition>
Get required children.
Constructor Details
#initialize(definition) ⇒ ElementDefinition
Initialize element definition from configuration
26 27 28 29 30 31 32 |
# File 'lib/uniword/ooxml/schema/element_definition.rb', line 26 def initialize(definition) @tag = definition[:tag] @description = definition[:description] @namespace = definition[:namespace] @children = build_children(definition[:children] || []) @attributes = build_attributes(definition[:attributes] || []) end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
21 22 23 |
# File 'lib/uniword/ooxml/schema/element_definition.rb', line 21 def attributes @attributes end |
#children ⇒ Object (readonly)
Returns the value of attribute children.
21 22 23 |
# File 'lib/uniword/ooxml/schema/element_definition.rb', line 21 def children @children end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
21 22 23 |
# File 'lib/uniword/ooxml/schema/element_definition.rb', line 21 def description @description end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
21 22 23 |
# File 'lib/uniword/ooxml/schema/element_definition.rb', line 21 def namespace @namespace end |
#tag ⇒ Object (readonly)
Returns the value of attribute tag.
21 22 23 |
# File 'lib/uniword/ooxml/schema/element_definition.rb', line 21 def tag @tag end |
Instance Method Details
#attribute(attr_name) ⇒ AttributeDefinition?
Get attribute definition by name
67 68 69 70 |
# File 'lib/uniword/ooxml/schema/element_definition.rb', line 67 def attribute(attr_name) attr_name = attr_name.to_sym @attributes.find { |a| a.name == attr_name } end |
#child(element_type) ⇒ ChildDefinition?
Get child definition by element type
55 56 57 58 |
# File 'lib/uniword/ooxml/schema/element_definition.rb', line 55 def child(element_type) element_type = element_type.to_sym @children.find { |c| c.element_type == element_type } end |
#has_attributes? ⇒ Boolean
Check if element has attributes
82 83 84 |
# File 'lib/uniword/ooxml/schema/element_definition.rb', line 82 def has_attributes? !@attributes.empty? end |
#has_children? ⇒ Boolean
Check if element has children
75 76 77 |
# File 'lib/uniword/ooxml/schema/element_definition.rb', line 75 def has_children? !@children.empty? end |
#local_name ⇒ String
Get element tag without namespace prefix
120 121 122 |
# File 'lib/uniword/ooxml/schema/element_definition.rb', line 120 def local_name @tag.split(":").last end |
#namespace_declarations ⇒ Hash
Get namespace declarations for XML element
41 42 43 44 45 46 |
# File 'lib/uniword/ooxml/schema/element_definition.rb', line 41 def namespace_declarations return {} unless @namespace prefix = tag.split(":").first { "xmlns:#{prefix}" => @namespace } end |
#optional_attributes ⇒ Array<AttributeDefinition>
Get optional attributes
110 111 112 |
# File 'lib/uniword/ooxml/schema/element_definition.rb', line 110 def optional_attributes @attributes.reject(&:required?) end |
#optional_children ⇒ Array<ChildDefinition>
Get optional children
96 97 98 |
# File 'lib/uniword/ooxml/schema/element_definition.rb', line 96 def optional_children @children.reject(&:required?) end |
#prefix ⇒ String?
Get namespace prefix
130 131 132 133 |
# File 'lib/uniword/ooxml/schema/element_definition.rb', line 130 def prefix parts = @tag.split(":") parts.length > 1 ? parts.first : nil end |
#required_attributes ⇒ Array<AttributeDefinition>
Get required attributes
103 104 105 |
# File 'lib/uniword/ooxml/schema/element_definition.rb', line 103 def required_attributes @attributes.select(&:required?) end |
#required_children ⇒ Array<ChildDefinition>
Get required children
89 90 91 |
# File 'lib/uniword/ooxml/schema/element_definition.rb', line 89 def required_children @children.select(&:required?) end |