Class: AWS::Core::XML::Grammar

Inherits:
Object
  • Object
show all
Defined in:
lib/aws/core/xml/grammar.rb

Overview

A class that simplifies building XML Parser rules. This is also a compatability layer between the old and new formats of the api config.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rules = {}, options = {}) ⇒ Grammar

Returns a new instance of Grammar.



23
24
25
26
27
28
29
# File 'lib/aws/core/xml/grammar.rb', line 23

def initialize rules = {}, options = {}
  @rules = rules
  @context = @rules
  @element_name = 'xml'
  @inflect_rename = options.key?(:inflect_rename) ?
    options[:inflect_rename] : true
end

Instance Attribute Details

#rulesHash (readonly)

Returns a hash of rules defined by this grammar.

Returns:

  • (Hash)

    Returns a hash of rules defined by this grammar.



41
42
43
# File 'lib/aws/core/xml/grammar.rb', line 41

def rules
  @rules
end

Class Method Details

.customize(customizations = nil, rules = {}, opts = {}, &block) ⇒ Object



76
77
78
79
80
81
# File 'lib/aws/core/xml/grammar.rb', line 76

def self.customize customizations = nil, rules = {}, opts = {}, &block
  grammar = self.new(deep_copy(rules), opts)
  grammar.send(:apply_customizations, customizations) if customizations
  grammar.instance_eval(&block) if block_given?
  grammar
end

.parse(xml) ⇒ Object



83
84
85
# File 'lib/aws/core/xml/grammar.rb', line 83

def self.parse xml
  self.new.parse(xml)
end

Instance Method Details

#customize(customizations = nil, &block) ⇒ Grammar

Returns a new grammar (leaving the current one un-modified) with the given customizations applied. Customizations can be given in a hash-form or in a block form.

Examples:

Block-form customizations


grammar.customize do
  element "EnumElement" do
    symbol_value
    list
  end
end

Hash-form customizations


grammar.customize "EnumElement" => [:symbol_value, :list]

Returns:

  • (Grammar)

    Returns a grammar with the given customizations applied.



63
64
65
66
# File 'lib/aws/core/xml/grammar.rb', line 63

def customize customizations = nil, &block
  opts = { :inflect_rename => @inflect_rename }
  self.class.customize(customizations, @rules, opts, &block)
end

#customize!(customizations = nil, &block) ⇒ Object

Applies customizations to the current grammar, not returning a new grammar.



70
71
72
73
74
# File 'lib/aws/core/xml/grammar.rb', line 70

def customize! customizations = nil, &block
  apply_customizations(customizations) if customizations
  instance_eval(&block) if block_given?
  self
end

#parse(xml) ⇒ Data

Parses the XML with the rules provided by the current grammar. This method is meant to provide backwards compatability with the old XmlGrammar class that handled rules and parsing.

Parameters:

  • xml (String)

Returns:

  • (Data)

    Returns a hash-like parsed response.



36
37
38
# File 'lib/aws/core/xml/grammar.rb', line 36

def parse xml
  Data.new(Parser.parse(xml, rules))
end