Class: XsdPopulator
- Inherits:
-
Object
show all
- Defined in:
- lib/xsd_populator.rb
Defined Under Namespace
Classes: ElementNotFoundException, Informer
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(_opts = {}) ⇒ XsdPopulator
Returns a new instance of XsdPopulator.
34
35
36
|
# File 'lib/xsd_populator.rb', line 34
def initialize(_opts = {})
configure _opts
end
|
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
32
33
34
|
# File 'lib/xsd_populator.rb', line 32
def options
@options
end
|
Instance Method Details
#add_attribute?(attribute, provider, stack = [], opts = {}) ⇒ Boolean
323
324
325
326
327
|
# File 'lib/xsd_populator.rb', line 323
def add_attribute?(attribute, provider, stack = [], opts = {})
return true if attribute.required?
content = opts[:content] || provider.try_take(stack + ["@#{attribute.name}"])
return (!content.nil?) || add_empty_attributes?
end
|
#add_empty_attributes? ⇒ Boolean
319
320
321
|
# File 'lib/xsd_populator.rb', line 319
def add_empty_attributes?
strategy == :nil_to_empty || strategy == :complete
end
|
#add_simple_nodes_without_data? ⇒ Boolean
292
293
294
|
# File 'lib/xsd_populator.rb', line 292
def add_simple_nodes_without_data?
strategy == :nil_to_empty || strategy == :complete
end
|
#build?(element, provider, stack, opts = {}) ⇒ Boolean
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
# File 'lib/xsd_populator.rb', line 296
def build?(element, provider, stack, opts = {})
content = opts[:content] || provider.try_take([stack, element.name].flatten.compact)
return false if content.is_a?(Informer) && content.skip?
if element.child_elements?
return content.respond_to?(:try_take) || build_node_without_provider? || provider.has_providers_with_scope?(stack + [element.name])
end
return true if content || add_simple_nodes_without_data?
return false
end
|
#build_node_without_provider? ⇒ Boolean
288
289
290
|
# File 'lib/xsd_populator.rb', line 288
def build_node_without_provider?
strategy == :complete
end
|
38
39
40
41
42
43
44
|
# File 'lib/xsd_populator.rb', line 38
def configure _opts = {}
@options = (@options || {}).merge(_opts.is_a?(Hash) ? _opts : {})
@logger = nil if _opts[:logger]
@xsd_reader = nil if _opts[:xsd_reader] || _opts[:reader]
uncache if (_opts.keys & [:strategy,:element,:xsd_file,:xsd,:xsd_reader,:reader,:provider,:data_provider]).length > 0
end
|
#default_provider ⇒ Object
71
72
73
|
# File 'lib/xsd_populator.rb', line 71
def default_provider
@default_provider ||= XsdExplanationProvider.new(:data => {:xsd_reader => xsd_reader}, :logger => logger)
end
|
#explain_xml? ⇒ Boolean
284
285
286
|
# File 'lib/xsd_populator.rb', line 284
def explain_xml?
provider == default_provider
end
|
#logger ⇒ Object
50
51
52
53
54
55
|
# File 'lib/xsd_populator.rb', line 50
def logger
return @logger || options[:logger] if @logger || options[:logger]
@logger = Logger.new(STDOUT)
@logger.level = Logger::WARN
return @logger
end
|
#max_recursion ⇒ Object
88
89
90
|
# File 'lib/xsd_populator.rb', line 88
def max_recursion
options[:max_recursion] || 3
end
|
#populate_element(element_specifier = nil) ⇒ Object
79
80
81
|
# File 'lib/xsd_populator.rb', line 79
def populate_element(element_specifier = nil)
element_specifier.nil? ? populated_xml : populate_xml(element_specifier)
end
|
#populated_xml ⇒ Object
75
76
77
|
# File 'lib/xsd_populator.rb', line 75
def populated_xml
@populated_xml ||= populate_xml
end
|
#provider ⇒ Object
67
68
69
|
# File 'lib/xsd_populator.rb', line 67
def provider
options[:provider] || options[:data_provider] || default_provider
end
|
#strategy ⇒ Object
280
281
282
|
# File 'lib/xsd_populator.rb', line 280
def strategy
options[:strategy] || (explain_xml? ? :complete : :smart)
end
|
#uncache ⇒ Object
46
47
48
|
# File 'lib/xsd_populator.rb', line 46
def uncache
@populated_xml = nil
end
|
#write_file(path) ⇒ Object
83
84
85
86
|
# File 'lib/xsd_populator.rb', line 83
def write_file(path)
File.write(path, populated_xml)
end
|
#xsd_file ⇒ Object
57
58
59
|
# File 'lib/xsd_populator.rb', line 57
def xsd_file
options[:xsd_file] || options[:xsd]
end
|
#xsd_reader ⇒ Object
Also known as:
reader
61
62
63
|
# File 'lib/xsd_populator.rb', line 61
def xsd_reader
@xsd_reader ||= options[:xsd_reader] || options[:reader] || (xsd_file.nil? ? nil : XsdReader::XML.new(:xsd_file => xsd_file, :logger => logger))
end
|