Class: WSDL::Contract::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/wsdl/contract/template.rb

Overview

Request template scaffold for request/header/body part contracts.

Constant Summary collapse

MODES =

Supported template rendering modes.

  • :minimal includes only required elements/attributes
  • :full includes all known elements/attributes

Returns:

  • (Array<Symbol>)
%i[minimal full].freeze

Instance Method Summary collapse

Constructor Details

#initialize(section:, elements:, mode:) ⇒ Template

Returns a new instance of Template.

Parameters:

  • section (Symbol)

    :header or :body

  • elements (Array<WSDL::XML::Element>)
  • mode (Symbol)

    :minimal or :full



18
19
20
21
22
23
24
# File 'lib/wsdl/contract/template.rb', line 18

def initialize(section:, elements:, mode:)
  validate_mode!(mode)

  @section = section
  @elements = elements
  @mode = mode
end

Instance Method Details

#to_dslString

Returns a copy-pastable request DSL scaffold.

Returns:

  • (String)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/wsdl/contract/template.rb', line 40

def to_dsl
  lines = ['operation.prepare do']

  if @section == :header
    lines << '  header do'
    append_element_lines(@elements, lines, 4)
    lines << '  end'
  else
    # Body content defaults to body section, no explicit wrapper needed
    append_element_lines(@elements, lines, 2)
  end

  lines << 'end'
  lines.join("\n")
end

#to_hHash{Symbol => Object}

Returns inspection-oriented structure of the selected part.

Returns:

  • (Hash{Symbol => Object})


29
30
31
32
33
34
35
# File 'lib/wsdl/contract/template.rb', line 29

def to_h
  @elements.each_with_object({}) do |element, memo|
    next unless include_element?(element)

    memo[element.name.to_sym] = template_value_for(element)
  end
end