Class: Maven::Tools::Visitor
- Inherits:
-
Object
- Object
- Maven::Tools::Visitor
- Defined in:
- lib/maven/tools/visitor.rb
Instance Method Summary collapse
- #accept(name, model) ⇒ Object
- #accept_array(name, array) ⇒ Object
- #accept_hash(name, hash) ⇒ Object
- #accept_project(project) ⇒ Object
- #accept_raw_hash(name, hash) ⇒ Object
- #camel_case_lower(str) ⇒ Object
- #dec ⇒ Object
- #end_raw_tag(name) ⇒ Object
- #end_tag(name) ⇒ Object
- #escape_value(value) ⇒ Object
- #inc ⇒ Object
- #indent ⇒ Object
-
#initialize(io = STDOUT) ⇒ Visitor
constructor
A new instance of Visitor.
- #raw_tag(name, value) ⇒ Object
- #start_raw_tag(name, attr = {}) ⇒ Object
- #start_tag(name, attr = {}) ⇒ Object
- #tag(name, value) ⇒ Object
- #visit(model) ⇒ Object
Constructor Details
#initialize(io = STDOUT) ⇒ Visitor
Returns a new instance of Visitor.
5 6 7 |
# File 'lib/maven/tools/visitor.rb', line 5 def initialize( io = STDOUT ) @io = io end |
Instance Method Details
#accept(name, model) ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/maven/tools/visitor.rb', line 75 def accept( name, model ) if model start_tag( name ) visit( model ) end_tag( name ) end end |
#accept_array(name, array) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/maven/tools/visitor.rb', line 83 def accept_array( name, array ) unless array.empty? start_tag( name ) n = name.to_s.sub( /ies$/, 'y' ).sub( /s$/, '' ) case array.first when Maven::Tools::Base array.each do |i| start_tag( n ) visit( i ) end_tag( n ) end when Hash array.each do |i| accept_hash( n, i ) end else array.each do |i| tag( n, i ) end end end_tag( name ) end end |
#accept_hash(name, hash) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/maven/tools/visitor.rb', line 125 def accept_hash( name, hash ) unless hash.empty? attr = hash.select do |k, v| [ k, v ] if k.to_s.match( /^@/ ) end start_tag( name, attr ) hash.each do |k, v| case v when Array accept_array( k, v ) when Hash accept_hash( k, v ) else tag( k, v ) unless k.to_s.match( /^@/ ) end end end_tag( name ) end end |
#accept_project(project) ⇒ Object
69 70 71 72 73 |
# File 'lib/maven/tools/visitor.rb', line 69 def accept_project( project ) accept( 'project', project ) @io.close if @io.respond_to? :close nil end |
#accept_raw_hash(name, hash) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/maven/tools/visitor.rb', line 107 def accept_raw_hash( name, hash ) unless hash.empty? attr = hash.select do |k, v| [ k, v ] if k.to_s.match( /^@/ ) end start_tag( name, attr ) hash.each do |k, v| case v when Array accept_array( k, v ) else raw_tag( k, v ) unless k.to_s.match( /^@/ ) end end end_tag( name ) end end |
#camel_case_lower(str) ⇒ Object
62 63 64 65 66 67 |
# File 'lib/maven/tools/visitor.rb', line 62 def camel_case_lower( str ) str = str.to_s str.split( '_' ).inject([]) do |buffer, e| buffer.push( buffer.empty? ? e : e.capitalize ) end.join end |
#dec ⇒ Object
17 18 19 |
# File 'lib/maven/tools/visitor.rb', line 17 def dec @indent = @indent[ 0..-3 ] end |
#end_raw_tag(name) ⇒ Object
32 33 34 35 |
# File 'lib/maven/tools/visitor.rb', line 32 def end_raw_tag( name ) dec @io << "#{indent}</#{name}>\n" end |
#end_tag(name) ⇒ Object
41 42 43 |
# File 'lib/maven/tools/visitor.rb', line 41 def end_tag( name ) end_raw_tag( camel_case_lower( name ) ) end |
#escape_value(value) ⇒ Object
145 146 147 148 149 150 151 152 153 |
# File 'lib/maven/tools/visitor.rb', line 145 def escape_value( value ) value = value.to_s value.gsub!( /&/, '&' ) # undo double quote, somehow xyz.gemspec.rz have encoded values value.gsub!( /&(amp|lt|gt);/, '&\1;' ) value.gsub!( /</, '<' ) value.gsub!( />/, '>' ) value end |
#inc ⇒ Object
13 14 15 |
# File 'lib/maven/tools/visitor.rb', line 13 def inc @indent = @indent + ' ' end |
#indent ⇒ Object
9 10 11 |
# File 'lib/maven/tools/visitor.rb', line 9 def indent @indent ||= '' end |
#raw_tag(name, value) ⇒ Object
56 57 58 59 60 |
# File 'lib/maven/tools/visitor.rb', line 56 def raw_tag( name, value ) unless value.nil? @io << "#{indent}<#{name}>#{escape_value( value )}</#{name}>\n" end end |
#start_raw_tag(name, attr = {}) ⇒ Object
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/maven/tools/visitor.rb', line 21 def start_raw_tag( name, attr = {} ) @io << "#{indent}<#{name}" attr.each do |k,v| @io << "\n" vv = v.gsub( /"/, '"' ) @io << "#{indent} #{k.to_s[1..-1]}=\"#{vv}\"" end @io << ">\n" inc end |
#start_tag(name, attr = {}) ⇒ Object
37 38 39 |
# File 'lib/maven/tools/visitor.rb', line 37 def start_tag( name, attr = {} ) start_raw_tag( camel_case_lower( name ), attr ) end |
#tag(name, value) ⇒ Object
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/maven/tools/visitor.rb', line 45 def tag( name, value ) if value != nil if value.respond_to? :to_xml @io << "#{indent}#{value.to_xml}\n" else name = camel_case_lower( name ) @io << "#{indent}<#{name}>#{escape_value( value )}</#{name}>\n" end end end |
#visit(model) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/maven/tools/visitor.rb', line 155 def visit( model ) model.attributes.each do |k, v| if k == :properties accept_raw_hash( k, v ) else case v when Base accept( k, v ) when Array accept_array( k, v ) when Hash accept_hash( k, v ) else tag( k, v ) end end end end |