Skip to content

Commit

Permalink
Get default value for "form" property from schema
Browse files Browse the repository at this point in the history
  • Loading branch information
ekzobrain committed Apr 12, 2024
1 parent f308b98 commit 1683804
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 19 deletions.
32 changes: 18 additions & 14 deletions lib/xsd/base_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -304,20 +304,24 @@ def method_missing(method, *args)
# check for property first
if (property = self.class.properties[method])
value = property[:resolve] ? property[:resolve].call(self) : node[property[:name].to_s]
result = if value.nil?
# if object has reference - search property in referenced object
node['ref'] ? reference.send(method, *args) : property[:default]
else
case property[:type]
when :integer
property[:name] == :maxOccurs && value == 'unbounded' ? :unbounded : value.to_i
when :boolean
value == 'true'
else
value
end
end
return @cache[method] = result
r = if value.nil?
if node['ref']
# if object has reference - search property in referenced object
reference.send(method, *args)
else
property[:default].is_a?(Proc) ? instance_eval(&property[:default]) : property[:default]
end
else
case property[:type]
when :integer
property[:name] == :maxOccurs && value == 'unbounded' ? :unbounded : value.to_i
when :boolean
value == 'true'
else
value
end
end
return @cache[method] = r
end

# if object has ref it cannot contain any type and children, so proxy call to target object
Expand Down
7 changes: 3 additions & 4 deletions lib/xsd/objects/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,14 @@ class Attribute < BaseObject
property :fixed, :string

# Optional. Specifies the form for the attribute. The default value is the value of the attributeFormDefault
# attribute of the element containing the attribute. Can be set to one of the following:
# attribute of the schema containing the attribute. Can be set to one of the following:
# qualified - indicates that this attribute must be qualified with the namespace prefix and the no-colon-name
# (NCName) of the attribute
# unqualified - indicates that this attribute is not required to be qualified with the namespace prefix and is
# matched against the (NCName) of the attribute
# @!attribute form
# @return String, nil
# TODO: support default value from parent
property :form, :string
# @return String
property :form, :string, default: proc { schema.attribute_form_default }

# Optional. Specifies a built-in data type or a simple type. The type attribute can only be present when the
# content does not contain a simpleType element
Expand Down
2 changes: 1 addition & 1 deletion lib/xsd/objects/element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Element < BaseObject
# cannot be used if the parent element is the schema element
# @!attribute form
# @return String
property :form, :string
property :form, :string, default: proc { schema.element_form_default }

# Optional. Specifies whether an explicit null value can be assigned to the element. True enables an instance of
# the element to have the null attribute set to true. The null attribute is defined as part of the XML Schema
Expand Down
8 changes: 8 additions & 0 deletions spec/xsd/element_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,5 +165,13 @@
expect(result2).to eq('http://dom.gosuslugi.ru/schema/integration/base/')
end
end

describe '#form' do
it 'calculates default form value' do
result = reader['importWorkingListRequest'].form

expect(result).to eq('qualified')
end
end
end
end

0 comments on commit 1683804

Please sign in to comment.