@private
# File lib/aws/core/xml_grammar.rb, line 546 def initialize element_name, options = {} @element_name = element_name @context = options[:context] @parent_frame = options[:parent_frame] @root_frame = options[:root_frame] @wrapper_frames = {} if @parent_frame @customizations = @parent_frame.customizations_for_child(element_name) else @customizations = options[:customizations] @root_frame ||= self end if @root_frame == self and indexes = @customizations[:index_names] indexes.each do |name| if context.kind_of?(Context) context.__set_data__(name, {}) else add_mutators(name) context.send("#{name}=", {}) end end end # we build and discard child frames here so we can know # which children should always add a method to this # frame's context (forced elements, like collected arrays) @customizations[:children].keys.each do |child_element_name| consume_initial_frame(build_child_frame(child_element_name)) end if @customizations[:wrapper_frames] @customizations[:wrapper_frames].keys.each do |method_name| consume_initial_frame(wrapper_frame_for(method_name)) end end end
# File lib/aws/core/xml_grammar.rb, line 626 def add_text text @text ||= '' @text << text end
# File lib/aws/core/xml_grammar.rb, line 653 def add_to_collection(collection, value) @customizations[:add_to_collection].call(collection, value) end
# File lib/aws/core/xml_grammar.rb, line 588 def build_child_frame(child_element_name) Frame.new(child_element_name, :parent_frame => self, :root_frame => root_frame) end
# File lib/aws/core/xml_grammar.rb, line 616 def close if indexed = @customizations[:indexed] (name, block) = indexed key = block.call(context) [key].flatten.each do |k| index(name)[k] = context end end end
# File lib/aws/core/xml_grammar.rb, line 594 def consume_child_frame child_frame return if child_frame.ignored? if child_frame.wrapped? child_frame.wrapper_methods.each do |method_name| consume_in_wrapper(method_name, child_frame) end else # forced child frames have already added mutators to this context add_mutators_for(child_frame) unless child_frame.forced? if child_frame.collected? child_frame.add_to_collection(context.send(child_frame.getter), child_frame.value) else invoke_setter(child_frame, child_frame.value) end end end
# File lib/aws/core/xml_grammar.rb, line 637 def context @context ||= (self.ignored? ? parent_frame.context : construct_context) end
# File lib/aws/core/xml_grammar.rb, line 645 def getter @customizations[:getter] end
# File lib/aws/core/xml_grammar.rb, line 657 def index(name) return root_frame.index(name) unless root_frame == self context.send(name) end
# File lib/aws/core/xml_grammar.rb, line 649 def initial_collection @customizations[:initial_collection].call end
# File lib/aws/core/xml_grammar.rb, line 641 def setter @customizations[:setter] end
# File lib/aws/core/xml_grammar.rb, line 631 def value @customizations[:value_formatter] ? @customizations[:value_formatter].format_value(default_value) : default_value end
# File lib/aws/core/xml_grammar.rb, line 736 def add_mutators(variable_name, setter = nil, getter = nil) return if context.kind_of?(Context) variable_name = variable_name.to_s.gsub(%r\?$/, '') setter ||= "#{variable_name}=" getter ||= variable_name return if context.respond_to?(getter) && context.respond_to?(setter) MetaUtils.extend_method(context, setter) do |val| instance_variable_set("@#{variable_name}", val) end MetaUtils.extend_method(context, getter) do instance_variable_get("@#{variable_name}") end end
# File lib/aws/core/xml_grammar.rb, line 728 def add_mutators_for child_frame return if context.kind_of?(Context) add_mutators(child_frame.ruby_name, child_frame.setter, child_frame.getter) end
# File lib/aws/core/xml_grammar.rb, line 763 def collected? @customizations[:collected] end
# File lib/aws/core/xml_grammar.rb, line 676 def construct_context if @customizations[:construct_value] instance_eval(&@customizations[:construct_value]) else Context.new end end
# File lib/aws/core/xml_grammar.rb, line 685 def consume_in_wrapper method_name, child_frame wrapper_frame = wrapper_frame_for(method_name) add_mutators(method_name) # the wrapper consumes the unwrapped child customizations = child_frame.customizations.merge(:wrapper => nil) child_frame = child_frame.dup child_frame.customizations = customizations wrapper_frame.consume_child_frame(child_frame) consume_child_frame(wrapper_frame) end
# File lib/aws/core/xml_grammar.rb, line 663 def consume_initial_frame(child_frame) if child_frame.forced? add_mutators_for(child_frame) if child_frame.collected? invoke_setter(child_frame, child_frame.initial_collection) else # this allows nested forced elements to appear invoke_setter(child_frame, child_frame.value) end end end
# File lib/aws/core/xml_grammar.rb, line 797 def customizations_for_child child_element_name @customizations[:children][child_element_name] || CustomizationContext.new(child_element_name) end
# File lib/aws/core/xml_grammar.rb, line 778 def default_value if # TODO : move this out of the default value method @context and @context.respond_to?(:encoding) and @context.encoding == 'base64' then Base64.decode64(@text.strip) else @context || @text end end
# File lib/aws/core/xml_grammar.rb, line 753 def forced? @customizations[:forced] end
# File lib/aws/core/xml_grammar.rb, line 758 def ignored? @customizations[:ignored] end
# File lib/aws/core/xml_grammar.rb, line 803 def initial_customizations(element_name = nil) end
# File lib/aws/core/xml_grammar.rb, line 719 def invoke_setter(child_frame, value) if context.kind_of?(Context) context.__set_data__(child_frame.getter, value) else context.send(child_frame.setter, value) end end
# File lib/aws/core/xml_grammar.rb, line 792 def ruby_name Inflection.ruby_name(@customizations[:renamed] || element_name) end
# File lib/aws/core/xml_grammar.rb, line 768 def wrapped? @customizations[:wrapper] end
# File lib/aws/core/xml_grammar.rb, line 706 def wrapper_customizations(method_name) customizations = CustomizationContext.new(method_name) customizations[:children] = @customizations[:children] if wrapper_frames = @customizations[:wrapper_frames] and additional = wrapper_frames[method_name] additional[:children] = @customizations[:children].merge(additional[:children]) if additional[:children] customizations.merge!(additional) end customizations end
# File lib/aws/core/xml_grammar.rb, line 699 def wrapper_frame_for(method_name) @wrapper_frames[method_name] ||= Frame.new(method_name.to_s, :customizations => wrapper_customizations(method_name)) end
# File lib/aws/core/xml_grammar.rb, line 773 def wrapper_methods @customizations[:wrapper] end