Class: AWS::Core::XML::Frame
- Inherits:
-
Object
- Object
- AWS::Core::XML::Frame
- Defined in:
- lib/aws/core/xml/frame.rb
Direct Known Subclasses
Constant Summary collapse
- TRANSLATE_DIGITS =
['0123456789'.freeze, ('X'*10).freeze]
- EASY_FORMAT =
"XXXX-XX-XXTXX:XX:XX.XXXZ".freeze
- DATE_PUNCTUATION =
['-:.TZ'.freeze, (' '*5).freeze]
Instance Attribute Summary collapse
-
#element_name ⇒ Object
readonly
Returns the value of attribute element_name.
-
#parent_frame ⇒ Object
readonly
Returns the value of attribute parent_frame.
-
#root_frame ⇒ Object
readonly
Returns the value of attribute root_frame.
-
#rules ⇒ Object
readonly
Returns the value of attribute rules.
Instance Method Summary collapse
- #add_text(chars) ⇒ Object
- #build_child_frame(element_name) ⇒ Object
- #close ⇒ Object
- #consume_child_frame(child_frame) ⇒ Object
- #data ⇒ Object
- #forced? ⇒ Boolean
- #ignored? ⇒ Boolean
- #index_keys_for(index_opts, &block) ⇒ Object
-
#initialize(root_frame, parent_frame, element_name, rules) ⇒ Frame
constructor
A new instance of Frame.
- #keys_from_path(data, path, &block) ⇒ Object
-
#known_child_frames ⇒ Object
The list of child frames that have customizations (rules), all other children will be parsed using standard rules.
- #list? ⇒ Boolean
- #map? ⇒ Boolean
- #ruby_name ⇒ Object
- #rules_for(child_element_name) ⇒ Object
- #value ⇒ Object
- #wrapped? ⇒ Boolean (also: #wrapper)
Constructor Details
#initialize(root_frame, parent_frame, element_name, rules) ⇒ Frame
Returns a new instance of Frame.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/aws/core/xml/frame.rb', line 27 def initialize root_frame, parent_frame, element_name, rules @root_frame = root_frame @parent_frame = parent_frame @element_name = element_name @rules = rules @rules[:children] ||= {} @data = {}.merge(rules[:defaults] || {}) @text = nil # initialize values for child frames of special types (e.g. # lists, maps, and forced elements) known_child_frames.each do |child_frame| context = data_context_for(child_frame) if child_frame.list? context[child_frame.ruby_name] = [] elsif child_frame.map? context[child_frame.ruby_name] = {} elsif child_frame.forced? context[child_frame.ruby_name] = child_frame.value end end end |
Instance Attribute Details
#element_name ⇒ Object (readonly)
Returns the value of attribute element_name.
55 56 57 |
# File 'lib/aws/core/xml/frame.rb', line 55 def element_name @element_name end |
#parent_frame ⇒ Object (readonly)
Returns the value of attribute parent_frame.
54 55 56 |
# File 'lib/aws/core/xml/frame.rb', line 54 def parent_frame @parent_frame end |
#root_frame ⇒ Object (readonly)
Returns the value of attribute root_frame.
53 54 55 |
# File 'lib/aws/core/xml/frame.rb', line 53 def root_frame @root_frame end |
#rules ⇒ Object (readonly)
Returns the value of attribute rules.
56 57 58 |
# File 'lib/aws/core/xml/frame.rb', line 56 def rules @rules end |
Instance Method Details
#add_text(chars) ⇒ Object
161 162 163 164 |
# File 'lib/aws/core/xml/frame.rb', line 161 def add_text chars @text ||= '' @text << chars end |
#build_child_frame(element_name) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/aws/core/xml/frame.rb', line 76 def build_child_frame element_name # if element_name should be wrapped # build a frame for the wrapper # build a child frame from the wrapper # else Frame.new(root_frame, self, element_name, rules_for(element_name)) end |
#close ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/aws/core/xml/frame.rb', line 103 def close # some xml elements should be indexed at the root level # The :index rule determines the name of this index # and what keys the data should be indexed as (one element # can be indexed under multiple keys). The index value # is always the element itself. if index = @rules[:index] index_keys_for(index) do |key| root_frame.add_to_index(index[:name], key, data) end end end |
#consume_child_frame(child_frame) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/aws/core/xml/frame.rb', line 84 def consume_child_frame child_frame child_frame.close return if child_frame.ignored? ruby_name = child_frame.ruby_name value = child_frame.value context = data_context_for(child_frame) if child_frame.list? context[ruby_name] << value elsif map = child_frame.map? context[ruby_name][child_frame.map_key] = child_frame.map_value else context[ruby_name] = value end end |
#data ⇒ Object
58 59 60 |
# File 'lib/aws/core/xml/frame.rb', line 58 def data ignored? ? parent_frame.data : @data end |
#forced? ⇒ Boolean
190 191 192 |
# File 'lib/aws/core/xml/frame.rb', line 190 def forced? @rules[:force] end |
#ignored? ⇒ Boolean
186 187 188 |
# File 'lib/aws/core/xml/frame.rb', line 186 def ignored? @rules[:ignore] end |
#index_keys_for(index_opts, &block) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/aws/core/xml/frame.rb', line 116 def index_keys_for index_opts, &block # simple (single) key if key = index_opts[:key] yield(data[key]) return end # composite key, joined by ":" if parts = index_opts[:keys] composite_key = parts.map{|part| data[part] }.join(":") yield(composite_key) return end # multiple keys, collected from the given path if path = index_opts[:key_path] keys_from_path(data, path.dup, &block) return end raise "missing require index rule option, :key, :keys or :key_path" end |
#keys_from_path(data, path, &block) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/aws/core/xml/frame.rb', line 141 def keys_from_path data, path, &block step = path.shift value = data[step] if path.empty? yield(value) return end if value.is_a?(Array) value.each do |v| keys_from_path(v, path.dup, &block) end else keys_from_path(value, path.dup, &block) end end |
#known_child_frames ⇒ Object
The list of child frames that have customizations (rules), all other children will be parsed using standard rules
72 73 74 |
# File 'lib/aws/core/xml/frame.rb', line 72 def known_child_frames rules[:children].keys.map {|name| build_child_frame(name) } end |
#list? ⇒ Boolean
194 195 196 |
# File 'lib/aws/core/xml/frame.rb', line 194 def list? @rules[:list] end |
#map? ⇒ Boolean
198 199 200 |
# File 'lib/aws/core/xml/frame.rb', line 198 def map? @rules[:map] end |
#ruby_name ⇒ Object
62 63 64 |
# File 'lib/aws/core/xml/frame.rb', line 62 def ruby_name rules[:rename] || root_frame.inflect(element_name) end |
#rules_for(child_element_name) ⇒ Object
66 67 68 |
# File 'lib/aws/core/xml/frame.rb', line 66 def rules_for child_element_name rules[:children][child_element_name] || {} end |
#value ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/aws/core/xml/frame.rb', line 166 def value if !data.empty? data[:encoding] == 'base64' ? Base64.decode64(@text.strip) : data elsif @text.nil? rules[:type] == :boolean ? false : nil else case rules[:type] when nil then @text when :datetime then datetime_like_value(DateTime, :civil) when :time then datetime_like_value(Time, :utc) when :integer then @text.to_i when :float then @text.to_f when :boolean then @text == 'true' when :blob then Base64.decode64(@text) when :symbol then Core::Inflection.ruby_name(@text).to_sym else raise "unhandled type" end end end |
#wrapped? ⇒ Boolean Also known as: wrapper
202 203 204 |
# File 'lib/aws/core/xml/frame.rb', line 202 def wrapped? @rules[:wrap] end |