class AWS::Core::XmlGrammar::Frame

@private

Attributes

customizations[RW]
element_name[R]
parent_frame[R]
root_frame[R]

Public Class Methods

new(element_name, options = {}) click to toggle source
# 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

Public Instance Methods

add_text(text) click to toggle source
# File lib/aws/core/xml_grammar.rb, line 626
def add_text text
  @text ||= ''
  @text << text
end
add_to_collection(collection, value) click to toggle source
# File lib/aws/core/xml_grammar.rb, line 653
def add_to_collection(collection, value)
  @customizations[:add_to_collection].call(collection, value)
end
build_child_frame(child_element_name) click to toggle source
# 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
close() click to toggle source
# 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
consume_child_frame(child_frame) click to toggle source
# 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
context() click to toggle source
# File lib/aws/core/xml_grammar.rb, line 637
def context
  @context ||= (self.ignored? ? parent_frame.context : construct_context)
end
getter() click to toggle source
# File lib/aws/core/xml_grammar.rb, line 645
def getter
  @customizations[:getter]
end
index(name) click to toggle source
# 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
initial_collection() click to toggle source
# File lib/aws/core/xml_grammar.rb, line 649
def initial_collection
  @customizations[:initial_collection].call
end
setter() click to toggle source
# File lib/aws/core/xml_grammar.rb, line 641
def setter
  @customizations[:setter]
end
value() click to toggle source
# File lib/aws/core/xml_grammar.rb, line 631
def value
  @customizations[:value_formatter] ?
    @customizations[:value_formatter].format_value(default_value) :
    default_value
end

Protected Instance Methods

add_mutators(variable_name, setter = nil, getter = nil) click to toggle source
# 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
add_mutators_for(child_frame) click to toggle source
# 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
collected?() click to toggle source
# File lib/aws/core/xml_grammar.rb, line 763
def collected?
  @customizations[:collected]
end
construct_context() click to toggle source
# 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
consume_in_wrapper(method_name, child_frame) click to toggle source
# 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
consume_initial_frame(child_frame) click to toggle source
# 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
customizations_for_child(child_element_name) click to toggle source
# 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
default_value() click to toggle source
# 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
forced?() click to toggle source
# File lib/aws/core/xml_grammar.rb, line 753
def forced?
  @customizations[:forced]
end
ignored?() click to toggle source
# File lib/aws/core/xml_grammar.rb, line 758
def ignored?
  @customizations[:ignored]
end
initial_customizations(element_name = nil) click to toggle source
# File lib/aws/core/xml_grammar.rb, line 803
def initial_customizations(element_name = nil)
end
invoke_setter(child_frame, value) click to toggle source
# 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
ruby_name() click to toggle source
# File lib/aws/core/xml_grammar.rb, line 792
def ruby_name
  Inflection.ruby_name(@customizations[:renamed] || element_name)
end
wrapped?() click to toggle source
# File lib/aws/core/xml_grammar.rb, line 768
def wrapped?
  @customizations[:wrapper]
end
wrapper_customizations(method_name) click to toggle source
# 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
wrapper_frame_for(method_name) click to toggle source
# 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
wrapper_methods() click to toggle source
# File lib/aws/core/xml_grammar.rb, line 773
def wrapper_methods
  @customizations[:wrapper]
end