Class: Uniword::Ooxml::Schema::AttributeDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/ooxml/schema/attribute_definition.rb

Overview

Represents the definition of ONE OOXML element attribute.

Responsibility: Define schema for a single attribute. Single Responsibility - attribute definition only.

Loaded from external YAML schema configuration.

Examples:

Create from config

attr_def = AttributeDefinition.new({
  name: :val,
  type: :enum,
  values: ['left', 'right', 'center'],
  required: true
})

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition) ⇒ AttributeDefinition

Initialize attribute definition from configuration

Parameters:

  • definition (Hash)

    Attribute definition hash from YAML



27
28
29
30
31
32
33
34
35
36
# File 'lib/uniword/ooxml/schema/attribute_definition.rb', line 27

def initialize(definition)
  @name = definition[:name]&.to_sym
  @tag = definition[:tag] || "w:#{definition[:name]}"
  @type = definition.fetch(:type, :string)&.to_sym
  @required = definition.fetch(:required, false)
  @values = definition[:values] # For enum types
  @namespace = definition[:namespace]
  @prefix = definition[:prefix] || "w"
  @property = definition[:property] # Custom property name mapping
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



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

def namespace
  @namespace
end

#prefixObject (readonly)

Returns the value of attribute prefix.



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

def prefix
  @prefix
end

#propertyObject (readonly)

Returns the value of attribute property.



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

def property
  @property
end

#requiredObject (readonly)

Returns the value of attribute required.



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

def required
  @required
end

#tagObject (readonly)

Returns the value of attribute tag.



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

def tag
  @tag
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

#valuesObject (readonly)

Returns the value of attribute values.



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

def values
  @values
end

Instance Method Details

#attribute_nameString

Get XML attribute name with namespace

Examples:

Attribute names

attribute_name  # => "w:val" or "r:id"

Returns:

  • (String)

    Full attribute name



136
137
138
139
140
141
142
# File 'lib/uniword/ooxml/schema/attribute_definition.rb', line 136

def attribute_name
  if @namespace && @prefix != "w"
    "#{@prefix}:#{@name}"
  else
    @tag
  end
end

#enum?Boolean

Check if attribute is enum type

Returns:

  • (Boolean)

    true if enum



55
56
57
# File 'lib/uniword/ooxml/schema/attribute_definition.rb', line 55

def enum?
  @type == :enum && !@values.nil?
end

#format_value(value) ⇒ String

Format value for XML output

Examples:

Format values

format_value(true)   # => "1"
format_value(false)  # => "0"
format_value(42)     # => "42"

Parameters:

  • value (Object)

    Value to format

Returns:

  • (String)

    Formatted value



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/uniword/ooxml/schema/attribute_definition.rb', line 116

def format_value(value)
  case @type
  when :boolean
    value ? "1" : "0"
  when :integer
    value.to_i.to_s
  when :enum
    validate_enum_value(value)
    value.to_s
  else
    value.to_s
  end
end

#optional?Boolean

Check if attribute is optional

Returns:

  • (Boolean)

    true if optional



48
49
50
# File 'lib/uniword/ooxml/schema/attribute_definition.rb', line 48

def optional?
  !@required
end

#property_nameSymbol

Get property name for accessing in model

Converts XML attribute name to Ruby property name. Uses custom property mapping if specified in schema.

Examples:

Property names

val → :val
lineRule → :line_rule
w:val → :val
id (with property: comment_id) → :comment_id

Returns:

  • (Symbol)

    Property name



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/uniword/ooxml/schema/attribute_definition.rb', line 93

def property_name
  # Use custom property name if specified
  return @property.to_sym if @property

  # Remove namespace prefix if present
  name_str = @tag.to_s.sub(/^w:/, "").sub(/^r:/, "")

  # Convert camelCase to snake_case
  name_str.gsub(/([A-Z])/, '_\1')
    .downcase
    .sub(/^_/, "")
    .to_sym
end

#required?Boolean

Check if attribute is required

Returns:

  • (Boolean)

    true if required



41
42
43
# File 'lib/uniword/ooxml/schema/attribute_definition.rb', line 41

def required?
  @required
end

#valid_value?(value) ⇒ Boolean

Validate value against schema

Parameters:

  • value (Object)

    Value to validate

Returns:

  • (Boolean)

    true if valid



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/uniword/ooxml/schema/attribute_definition.rb', line 63

def valid_value?(value)
  return false if value.nil? && @required

  # Type-specific validation
  case @type
  when :enum
    @values.include?(value.to_s)
  when :integer
    value.is_a?(Integer) || value.to_s.match?(/^\d+$/)
  when :boolean
    [true, false, "true", "false", "1", "0"].include?(value)
  when :string
    true # Any value acceptable
  else
    true
  end
end