class AWS::Core::XML::Grammar
config.
a compatability layer between the old and new formats of the api
A class that simplifies building XML {Parser} rules. This is also
def self.customize customizations = nil, rules = {}, opts = {}, &block
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
def self.deep_copy rules
Performs a deep copy of the rules hash so that it can be
def self.deep_copy rules rules.inject({}) do |copy,(key,value)| copy[key] = value.is_a?(Hash) ? deep_copy(value) : value copy end end
def self.parse xml
def self.parse xml self.new.parse(xml) end
def apply_customizations customizations
def apply_customizations customizations customizations.each do |item| (type, identifier, args) = parse_customization_item(item) case type when :method validate_config_method(identifier) validate_args(identifier, args) send(identifier, *args) when :element element(identifier) do apply_customizations(args) end end end end
def blob_value
def blob_value @context[:type] = :blob end
def boolean_value
def boolean_value @context[:type] = :boolean end
def collect_values
def collect_values @context[:list] = true end
def construct_value &block
def construct_value &block raise 'remove the need for this' end
def context_for_child child_element_name
def context_for_child child_element_name @context[:children] ||= {} @context[:children][child_element_name] ||= {} @context[:children][child_element_name] end
def customize customizations = nil, &block
-
(Grammar)
- Returns a grammar with the given customizations
Other tags:
- Example: Hash-form customizations -
Example: Block-form customizations -
def customize customizations = nil, &block opts = { :inflect_rename => @inflect_rename } self.class.customize(customizations, @rules, opts, &block) end
def customize! customizations = nil, &block
Applies customizations to the current grammar, not returning
def customize! customizations = nil, &block apply_customizations(customizations) if customizations instance_eval(&block) if block_given? self end
def datetime_value
def datetime_value @context[:type] = :datetime end
def default_value name, value
def default_value name, value @context[:defaults] ||= {} @context[:defaults][name] = value end
def element element_name, &block
def element element_name, &block parent_context = @context parent_element_name = @element_name @context = context_for_child(element_name) @element_name = element_name begin if block_given? block.arity == 1 ? yield(parent_element_name) : yield end ensure @context = parent_context @element_name = parent_element_name end end
def eql? other
def eql? other other.is_a?(Grammar) and self.rules == other.rules end
def float_value
def float_value @context[:type] = :float end
def force
def force @context[:force] = true end
def ignore
def ignore @context[:ignore] = true end
def index index_name, options = {}
def index index_name, options = {} @context[:index] = options.merge(:name => index_name) end
def inflect value
def inflect value Inflection.ruby_name(value.to_s).to_sym end
def initialize rules = {}, options = {}
def initialize rules = {}, options = {} @rules = rules @context = @rules @element_name = 'xml' @inflect_rename = options.key?(:inflect_rename) ? options[:inflect_rename] : true end
def integer_value
def integer_value @context[:type] = :integer end
def list child_element_name = nil, &block
def list child_element_name = nil, &block if child_element_name ignore element(child_element_name) do |parent_element_name| rename(parent_element_name) collect_values yield if block_given? end else collect_values end end
def map map_element_name, key_element_name, value_element_name
def map map_element_name, key_element_name, value_element_name ignore element(map_element_name) do |parent_element_name| rename(parent_element_name) map_entry(key_element_name, value_element_name) end end
def map_entry key_element_name, value_element_name
def map_entry key_element_name, value_element_name @context[:map] = [key_element_name, value_element_name] end
def parse xml
-
(Data)
- Returns a hash-like parsed response.
Parameters:
-
xml
(String
) --
def parse xml Data.new(Parser.parse(xml, rules)) end
def parse_customization_item item
def parse_customization_item item case item when Symbol [:method, item, []] when Hash (method, arg) = item.to_a.first if method.kind_of?(Symbol) [:method, method, [arg].flatten] else [:element, method, arg] end end end
def rename new_name
def rename new_name if @inflect_rename @context[:rename] = inflect(new_name) else @context[:rename] = new_name end end
def string_value
def string_value @context[:type] = :string end
def symbol_value
def symbol_value @context[:type] = :symbol end
def time_value
def time_value @context[:type] = :time end
def validate_args(identifier, args)
def validate_args(identifier, args) arity = method(identifier).arity if args.length > 0 raise "#{identifier} does not accept an argument" if arity == 0 else raise "#{identifier} requires an argument" unless arity == 0 || arity == -1 end end
def validate_config_method(method)
def validate_config_method(method) allow_methods = %w( rename attribute_name boolean integer long float list force ignore collect_values symbol_value timestamp map_entry map blob string ) unless allow_methods.include?(method.to_s) raise "#{method} cannot be used in configuration" end end
def wrapper method_name, options = {}, &block
def wrapper method_name, options = {}, &block options[:for].each do |child| context_for_child(child)[:wrap] = method_name end end