Class: XsdPopulator

Inherits:
Object
  • 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

#optionsObject (readonly)

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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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)

  # we got an Informer object that tells us explicitly to skip this node? Yes sir.
  return false if content.is_a?(Informer) && content.skip?

  # For comlex nodes we need either;
  # - a data provider or
  # - explicit confirmation to build without providers or
  # - providers available for offspring elements
  if element.child_elements? 
    return content.respond_to?(:try_take) || build_node_without_provider? || provider.has_providers_with_scope?(stack + [element.name])
  end

  # we got a non-nil value for a simple node? Go ahead
  return true if content || add_simple_nodes_without_data?

  return false

  # !build_node_without_provider? || 
  # return true if provider
end

#build_node_without_provider?Boolean

Returns:

  • (Boolean)


288
289
290
# File 'lib/xsd_populator.rb', line 288

def build_node_without_provider?
  strategy == :complete
end

#configure(_opts = {}) ⇒ Object



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 : {})
  # remove some cached values
  @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_providerObject



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

Returns:

  • (Boolean)


284
285
286
# File 'lib/xsd_populator.rb', line 284

def explain_xml?
  provider == default_provider
end

#loggerObject



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_recursionObject



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_xmlObject



75
76
77
# File 'lib/xsd_populator.rb', line 75

def populated_xml
  @populated_xml ||= populate_xml
end

#providerObject



67
68
69
# File 'lib/xsd_populator.rb', line 67

def provider
  options[:provider] || options[:data_provider] || default_provider
end

#strategyObject



280
281
282
# File 'lib/xsd_populator.rb', line 280

def strategy
  options[:strategy] || (explain_xml? ? :complete : :smart)
end

#uncacheObject



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)
  # logger.debug "XsdPopulator#write_file to: #{path}"
  File.write(path, populated_xml)
end

#xsd_fileObject



57
58
59
# File 'lib/xsd_populator.rb', line 57

def xsd_file
  options[:xsd_file] || options[:xsd]
end

#xsd_readerObject 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