Class: Uniword::Ooxml::Schema::ChildDefinition
- Inherits:
-
Object
- Object
- Uniword::Ooxml::Schema::ChildDefinition
- Defined in:
- lib/uniword/ooxml/schema/child_definition.rb
Overview
Represents the definition of a child element relationship.
Responsibility: Define how ONE child element relates to its parent. Single Responsibility - child relationship definition only.
Loaded from external YAML schema configuration.
Instance Attribute Summary collapse
-
#attributes ⇒ Array<Hash>
readonly
Get attributes for this child.
-
#element_type ⇒ Object
readonly
Returns the value of attribute element_type.
-
#multiple ⇒ Object
readonly
Returns the value of attribute multiple.
-
#optional ⇒ Object
readonly
Returns the value of attribute optional.
-
#required ⇒ Object
readonly
Returns the value of attribute required.
-
#tag ⇒ Object
readonly
Returns the value of attribute tag.
Instance Method Summary collapse
-
#has_attributes? ⇒ Boolean
Check if child has attributes.
-
#initialize(definition) ⇒ ChildDefinition
constructor
Initialize child definition from configuration.
-
#multiple? ⇒ Boolean
Check if multiple children allowed.
-
#optional? ⇒ Boolean
Check if child is optional.
-
#presence_only? ⇒ Boolean
Check if element is presence-only (no value).
-
#property_name ⇒ Symbol
Get property name for accessing child in model.
-
#required? ⇒ Boolean
Check if child is required.
Constructor Details
#initialize(definition) ⇒ ChildDefinition
Initialize child definition from configuration
26 27 28 29 30 31 32 33 34 |
# File 'lib/uniword/ooxml/schema/child_definition.rb', line 26 def initialize(definition) @element_type = definition[:element]&.to_sym @tag = definition[:tag] @optional = definition.fetch(:optional, true) @multiple = definition.fetch(:multiple, false) @required = definition.fetch(:required, false) @presence_only = definition.fetch(:presence_only, false) @attributes = definition[:attributes] || [] end |
Instance Attribute Details
#attributes ⇒ Array<Hash> (readonly)
Get attributes for this child
81 82 83 |
# File 'lib/uniword/ooxml/schema/child_definition.rb', line 81 def attributes @attributes end |
#element_type ⇒ Object (readonly)
Returns the value of attribute element_type.
21 22 23 |
# File 'lib/uniword/ooxml/schema/child_definition.rb', line 21 def element_type @element_type end |
#multiple ⇒ Object (readonly)
Returns the value of attribute multiple.
21 22 23 |
# File 'lib/uniword/ooxml/schema/child_definition.rb', line 21 def multiple @multiple end |
#optional ⇒ Object (readonly)
Returns the value of attribute optional.
21 22 23 |
# File 'lib/uniword/ooxml/schema/child_definition.rb', line 21 def optional @optional end |
#required ⇒ Object (readonly)
Returns the value of attribute required.
21 22 23 |
# File 'lib/uniword/ooxml/schema/child_definition.rb', line 21 def required @required end |
#tag ⇒ Object (readonly)
Returns the value of attribute tag.
21 22 23 |
# File 'lib/uniword/ooxml/schema/child_definition.rb', line 21 def tag @tag end |
Instance Method Details
#has_attributes? ⇒ Boolean
Check if child has attributes
74 75 76 |
# File 'lib/uniword/ooxml/schema/child_definition.rb', line 74 def has_attributes? !@attributes.empty? end |
#multiple? ⇒ Boolean
Check if multiple children allowed
46 47 48 |
# File 'lib/uniword/ooxml/schema/child_definition.rb', line 46 def multiple? @multiple end |
#optional? ⇒ Boolean
Check if child is optional
39 40 41 |
# File 'lib/uniword/ooxml/schema/child_definition.rb', line 39 def optional? @optional end |
#presence_only? ⇒ Boolean
Check if element is presence-only (no value)
Some OOXML elements like <w:b/> have no value, their presence alone indicates the property is true.
67 68 69 |
# File 'lib/uniword/ooxml/schema/child_definition.rb', line 67 def presence_only? @presence_only end |
#property_name ⇒ Symbol
Get property name for accessing child in model
Converts element_type to Ruby property name.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/uniword/ooxml/schema/child_definition.rb', line 92 def property_name name = @element_type.to_s # Handle special cases name = case name when "paragraph_properties" "properties" when "run_properties" "properties" when "table_properties" "properties" when "table_cell_properties" "properties" when "table_row_properties" "properties" when "text" "text_element" when "table_row" "row" when "table_cell" "cell" else name end # Pluralize if multiple name = "#{name}s" if @multiple && !name.end_with?("s") name.to_sym end |
#required? ⇒ Boolean
Check if child is required
53 54 55 |
# File 'lib/uniword/ooxml/schema/child_definition.rb', line 53 def required? @required end |