Class: Spectifly::Xsd::Field
Instance Attribute Summary
#attributes, #description, #example, #inherits_from, #restrictions, #validations
Instance Method Summary
collapse
Methods inherited from Base::Field
#extract_attributes, #multiple?, #type
#extract_attributes, #initialize, #required?, #type, #unique?
Instance Method Details
#display_type ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/spectifly/xsd/field.rb', line 20
def display_type
prefix = if @validations.include?('Must be positive')
'positive_'
elsif @validations.include?('Must be non-negative')
'non_negative_'
end
prefixed_type = "#{prefix}#{type}"
camel_type = Spectifly::Support.lower_camelize(prefixed_type)
if Spectifly::Xsd::Types::Native.include?(prefixed_type)
"xs:#{camel_type}"
else
"#{camel_type}Type"
end
end
|
#embedded_block ⇒ Object
77
78
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/spectifly/xsd/field.rb', line 77
def embedded_block
if description || example || !restrictions.empty?
Proc.new { |el|
if description || example
el.xs :annotation do |ann|
ann.xs :documentation, description if description
ann.xs :documentation, "Example: #{example}" if example
end
end
type_block.call(el) unless restrictions.empty?
}
end
end
|
6
7
8
9
10
|
# File 'lib/spectifly/xsd/field.rb', line 6
def
super
@restrictions.delete('unique')
@restrictions
end
|
#name ⇒ Object
12
13
14
|
# File 'lib/spectifly/xsd/field.rb', line 12
def name
Spectifly::Support.camelize(@field_name).gsub(/\W/, '')
end
|
#regex ⇒ Object
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/spectifly/xsd/field.rb', line 44
def regex
return nil unless restrictions['regex']
regex = restrictions['regex'].source
if regex =~ /^(\^)?([^\$]*)(\$)?$/
prefix = '[\s\S]*' unless $1
suffix = '[\s\S]*' unless $3
regex = "#{prefix}#{$2}#{suffix}"
end
regex
end
|
#restrictions_block ⇒ Object
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/spectifly/xsd/field.rb', line 64
def restrictions_block
if !restrictions.empty?
Proc.new { |el|
el.xs :minInclusive, :value => restrictions['minimum_value'] if restrictions['minimum_value']
el.xs :maxInclusive, :value => restrictions['maximum_value'] if restrictions['maximum_value']
el.xs :pattern, :value => regex if regex
(restrictions['valid_values'] || []).each do |vv|
el.xs :enumeration, :value => vv
end
}
end
end
|
#to_xsd(builder = nil) ⇒ Object
35
36
37
38
39
40
41
42
|
# File 'lib/spectifly/xsd/field.rb', line 35
def to_xsd(builder = nil)
builder ||= ::Builder::XmlMarkup.new(:indent => 2)
attributes['type'] = display_type if restrictions.empty?
attributes['minOccurs'] = '0' unless required?
attributes['maxOccurs'] = 'unbounded' if multiple?
block = embedded_block
builder.xs :element, { :name => name }.merge(attributes), &block
end
|
#type_block(use_name = false) ⇒ Object
55
56
57
58
59
60
61
62
|
# File 'lib/spectifly/xsd/field.rb', line 55
def type_block(use_name = false)
type_attributes = { :name => name_as_type } if use_name
Proc.new { |el|
el.xs :simpleType, type_attributes do |st|
st.xs :restriction, :base => display_type, &restrictions_block
end
}
end
|