Class: Uniword::Ooxml::Schema::AttributeDefinition
- Inherits:
-
Object
- Object
- Uniword::Ooxml::Schema::AttributeDefinition
- 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.
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
-
#property ⇒ Object
readonly
Returns the value of attribute property.
-
#required ⇒ Object
readonly
Returns the value of attribute required.
-
#tag ⇒ Object
readonly
Returns the value of attribute tag.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#values ⇒ Object
readonly
Returns the value of attribute values.
Instance Method Summary collapse
-
#attribute_name ⇒ String
Get XML attribute name with namespace.
-
#enum? ⇒ Boolean
Check if attribute is enum type.
-
#format_value(value) ⇒ String
Format value for XML output.
-
#initialize(definition) ⇒ AttributeDefinition
constructor
Initialize attribute definition from configuration.
-
#optional? ⇒ Boolean
Check if attribute is optional.
-
#property_name ⇒ Symbol
Get property name for accessing in model.
-
#required? ⇒ Boolean
Check if attribute is required.
-
#valid_value?(value) ⇒ Boolean
Validate value against schema.
Constructor Details
#initialize(definition) ⇒ AttributeDefinition
Initialize attribute definition from configuration
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
#name ⇒ Object (readonly)
Returns the value of attribute name.
21 22 23 |
# File 'lib/uniword/ooxml/schema/attribute_definition.rb', line 21 def name @name end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
21 22 23 |
# File 'lib/uniword/ooxml/schema/attribute_definition.rb', line 21 def namespace @namespace end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
21 22 23 |
# File 'lib/uniword/ooxml/schema/attribute_definition.rb', line 21 def prefix @prefix end |
#property ⇒ Object (readonly)
Returns the value of attribute property.
21 22 23 |
# File 'lib/uniword/ooxml/schema/attribute_definition.rb', line 21 def property @property end |
#required ⇒ Object (readonly)
Returns the value of attribute required.
21 22 23 |
# File 'lib/uniword/ooxml/schema/attribute_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/attribute_definition.rb', line 21 def tag @tag end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
21 22 23 |
# File 'lib/uniword/ooxml/schema/attribute_definition.rb', line 21 def type @type end |
#values ⇒ Object (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_name ⇒ String
Get XML attribute name with namespace
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
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
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
48 49 50 |
# File 'lib/uniword/ooxml/schema/attribute_definition.rb', line 48 def optional? !@required end |
#property_name ⇒ Symbol
Get property name for accessing in model
Converts XML attribute name to Ruby property name. Uses custom property mapping if specified in schema.
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
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
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 |