class AndroidXml::Tag
Attributes
is_root[RW]
Public Class Methods
flatten_attrs(attrs, prefix='')
click to toggle source
# File lib/android-xml/tag.rb, line 6 def self.flatten_attrs(attrs, prefix='') flattened = {} attrs.each do |key, value| key = "#{prefix}#{key}" if value.is_a?(Hash) flattened.merge!(flatten_attrs(value, "#{key}_")) else flattened[key] = value end end flattened end
format_attrs(tag, attrs, whitespace)
click to toggle source
# File lib/android-xml/tag.rb, line 20 def self.format_attrs(tag, attrs, whitespace) output = '' is_first = true attrs.each do |key, value| if value.nil? next end key = key.to_s if Setup.tags[tag][:attrs].key?(key) xml_key = Setup.tags[tag][:attrs][key] elsif Setup.all_tag[:attrs].key?(key) xml_key = Setup.all_tag[:attrs][key] elsif key.to_s.include?(':') xml_key = key.to_s else xml_key = "android:#{key}" end if value =~ /^@string\/(\w+)$/ Tag.found_strings << $1 end quoted_value = Tag.quote_attr_value(value) if is_first output << " #{xml_key}=\"#{quoted_value}\"" is_first = false else output << "\n#{whitespace}#{xml_key}=\"#{quoted_value}\"" end end output end
format_string(text)
click to toggle source
isn’t there a method that does this for me!?
# File lib/android-xml/string.rb, line 38 def self.format_string(text) text = text.dup { '\\' => '\\\\', "\n" => '\n', "\t" => '\t', "\b" => '\b', '"' => '\"', "'" => "\\\\'", }.each do |find, replace| text.gsub!(find, replace) end text end
found_strings()
click to toggle source
# File lib/android-xml/string.rb, line 10 def found_strings @found_strings ||= [] end
new(tag, attrs={}, &block)
click to toggle source
# File lib/android-xml/tag.rb, line 70 def initialize(tag, attrs={}, &block) @buffer = [] @attrs = {}.merge(Tag.flatten_attrs(attrs)) @raw_tag = tag.to_s if rename = Setup.tags[tag.to_s][:rename] @tag = rename else @tag = tag.to_s.gsub('_', '-') end @generate = block end
quote_attr_value(value)
click to toggle source
# File lib/android-xml/tag.rb, line 56 def self.quote_attr_value(value) value = value.to_s.dup { '&' => '&', '\'' => ''', '"' => '"', '<' => '<', '>' => '>', }.each do |find, replace| value.gsub!(find, replace) end value end
registered_strings()
click to toggle source
# File lib/android-xml/string.rb, line 14 def registered_strings @registered_strings ||= [] end
Public Instance Methods
clone(attrs={}, &block)
click to toggle source
# File lib/android-xml/tag.rb, line 90 def clone(attrs={}, &block) block ||= @generate Tag.new(@tag, @attrs.merge(Tag.flatten_attrs(attrs)), &block) end
generate(tab='')
click to toggle source
# File lib/android-xml/tag.rb, line 120 def generate(tab='') whitespace = "#{tab} #{' ' * @tag.length}" output = "#{tab}<#{@tag}#{generate_attrs(whitespace)}" if @generate inside = generate_block(tab + Setup.tab) if !inside || inside.strip.empty? output << " />\n" else output << ">" if inside.lstrip.start_with?('<') output << "\n" << inside << "#{tab}</#{@tag}>\n" else output << inside << "</#{@tag}>\n" end end else output << " />\n" end output end
generate_attrs(whitespace)
click to toggle source
# File lib/android-xml/tag.rb, line 106 def generate_attrs(whitespace) attrs = {} attrs.merge!(Setup.all_tag[:defaults]) if is_root attrs.merge!(Setup.root_tag[:defaults]) end attrs.merge!(Setup.tags[@raw_tag][:defaults]) attrs.merge!(@attrs) Tag.format_attrs(@tag, attrs, whitespace) end
generate_block(tab='')
click to toggle source
# File lib/android-xml/tag.rb, line 142 def generate_block(tab='') return @block_output if @block_output output = '' if @generate text = run_block(&@generate) @buffer.each do |tag| output << tag.generate(tab) end if text.is_a?(String) if output.empty? output << text else output << tab << text << "\n" end end end @block_output = output end
include(tag, &block)
click to toggle source
Calls superclass method
# File lib/android-xml/tag.rb, line 95 def include(tag, &block) if tag.is_a?(Tag) if block_given? tag = tag.clone(&block) end @buffer << tag else super end end
method_missing(method_name, attrs={}, &block)
click to toggle source
# File lib/android-xml/tag.rb, line 84 def method_missing(method_name, attrs={}, &block) tag = Tag.new(method_name, attrs, &block) include tag tag end
out()
click to toggle source
# File lib/android-xml/tag.rb, line 175 def out puts to_s end
run_block(&block)
click to toggle source
# File lib/android-xml/string.rb, line 29 def run_block(&block) text = instance_exec(&block) if text.is_a?(String) text = Tag.format_string(text) end text end
string(attrs={}, &block)
click to toggle source
# File lib/android-xml/string.rb, line 20 def string(attrs={}, &block) tag = Tag.new('string', attrs, &block) string = attrs['name'] || attrs[:name] raise "name is required in a <string> tag" unless string && ! string.empty? Tag.registered_strings << (string) include tag tag end
to_ary()
click to toggle source
# File lib/android-xml/tag.rb, line 171 def to_ary [to_s] end
to_s()
click to toggle source
# File lib/android-xml/tag.rb, line 167 def to_s generate end