class AWS::Core::XmlGrammar::Frame
@private
def add_mutators(variable_name,
def add_mutators(variable_name, setter = nil, getter = nil) return if context.kind_of?(Context) variable_name = variable_name.to_s.gsub(/\?$/, '') 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
def add_mutators_for child_frame
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
def add_text text
def add_text text @text ||= '' @text << text end
def add_to_collection(collection, value)
def add_to_collection(collection, value) @customizations[:add_to_collection].call(collection, value) end
def build_child_frame(child_element_name)
def build_child_frame(child_element_name) Frame.new(child_element_name, :parent_frame => self, :root_frame => root_frame) end
def close
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
def collected?
def collected? @customizations[:collected] end
def construct_context
def construct_context if @customizations[:construct_value] instance_eval(&@customizations[:construct_value]) else Context.new end end
def consume_child_frame child_frame
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
def consume_in_wrapper method_name, child_frame
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
def consume_initial_frame(child_frame)
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
def context
def context @context ||= (self.ignored? ? parent_frame.context : construct_context) end
def customizations_for_child child_element_name
def customizations_for_child child_element_name @customizations[:children][child_element_name] || CustomizationContext.new(child_element_name) end
def default_value
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
def forced?
def forced? @customizations[:forced] end
def getter
def getter @customizations[:getter] end
def ignored?
def ignored? @customizations[:ignored] end
def index(name)
def index(name) return root_frame.index(name) unless root_frame == self context.send(name) end
def initial_collection
def initial_collection @customizations[:initial_collection].call end
def initial_customizations(element_name = nil)
def initial_customizations(element_name = nil) end
def initialize element_name, options = {}
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
def invoke_setter(child_frame, value)
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
def ruby_name
def ruby_name Inflection.ruby_name(@customizations[:renamed] || element_name) end
def setter
def setter @customizations[:setter] end
def value
def value @customizations[:value_formatter] ? @customizations[:value_formatter].format_value(default_value) : default_value end
def wrapped?
def wrapped? @customizations[:wrapper] end
def wrapper_customizations(method_name)
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
def wrapper_frame_for(method_name)
def wrapper_frame_for(method_name) @wrapper_frames[method_name] ||= Frame.new(method_name.to_s, :customizations => wrapper_customizations(method_name)) end
def wrapper_methods
def wrapper_methods @customizations[:wrapper] end