Class: Uniword::Ooxml::Schema::ChildDefinition

Inherits:
Object
  • Object
show all
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.

Examples:

Create from config

child_def = ChildDefinition.new({
  element: :run,
  tag: 'w:r',
  multiple: true,
  optional: true
})

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition) ⇒ ChildDefinition

Initialize child definition from configuration

Parameters:

  • definition (Hash)

    Child definition hash from YAML



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

#attributesArray<Hash> (readonly)

Get attributes for this child

Returns:

  • (Array<Hash>)

    Attribute definitions



81
82
83
# File 'lib/uniword/ooxml/schema/child_definition.rb', line 81

def attributes
  @attributes
end

#element_typeObject (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

#multipleObject (readonly)

Returns the value of attribute multiple.



21
22
23
# File 'lib/uniword/ooxml/schema/child_definition.rb', line 21

def multiple
  @multiple
end

#optionalObject (readonly)

Returns the value of attribute optional.



21
22
23
# File 'lib/uniword/ooxml/schema/child_definition.rb', line 21

def optional
  @optional
end

#requiredObject (readonly)

Returns the value of attribute required.



21
22
23
# File 'lib/uniword/ooxml/schema/child_definition.rb', line 21

def required
  @required
end

#tagObject (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

Returns:

  • (Boolean)

    true if 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

Returns:

  • (Boolean)

    true if multiple



46
47
48
# File 'lib/uniword/ooxml/schema/child_definition.rb', line 46

def multiple?
  @multiple
end

#optional?Boolean

Check if child is optional

Returns:

  • (Boolean)

    true if 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.

Examples:

Presence-only elements

<w:b/>  # Bold = true by presence
<w:i/>  # Italic = true by presence

Returns:

  • (Boolean)

    true if presence-only



67
68
69
# File 'lib/uniword/ooxml/schema/child_definition.rb', line 67

def presence_only?
  @presence_only
end

#property_nameSymbol

Get property name for accessing child in model

Converts element_type to Ruby property name.

Examples:

Property names

run → :runs (if multiple)
paragraph_properties → :properties

Returns:

  • (Symbol)

    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

Returns:

  • (Boolean)

    true if required



53
54
55
# File 'lib/uniword/ooxml/schema/child_definition.rb', line 53

def required?
  @required
end