Class: Maven::Tools::Visitor

Inherits:
Object
  • Object
show all
Defined in:
lib/maven/tools/visitor.rb

Instance Method Summary collapse

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



70
71
72
73
74
75
76
# File 'lib/maven/tools/visitor.rb', line 70

def accept( name, model )
  if model
    start_tag( name )
    visit( model )
    end_tag( name )
  end
end

#accept_array(name, array) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/maven/tools/visitor.rb', line 78

def accept_array( name, array )
  unless array.empty?
    start_tag( name )
    n = name.to_s.sub( /ies$/, 'y' ).sub( /s$/, '' )
    case array.first
    when Virtus
      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



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/maven/tools/visitor.rb', line 120

def accept_hash( name, hash )
  unless hash.empty?
    # TODO attributes
    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
        # TODO xml content
        tag( k, v ) unless k.to_s.match( /^@/ )
      end
    end
    end_tag( name )
  end
end

#accept_project(project) ⇒ Object



64
65
66
67
68
# File 'lib/maven/tools/visitor.rb', line 64

def accept_project( project )
  accept( 'project', project )
  @io.close if @io.respond_to? :close
  nil
end

#accept_raw_hash(name, hash) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/maven/tools/visitor.rb', line 102

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



57
58
59
60
61
62
# File 'lib/maven/tools/visitor.rb', line 57

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

#decObject



17
18
19
# File 'lib/maven/tools/visitor.rb', line 17

def dec
  @indent = @indent[ 0..-3 ]
end

#end_raw_tag(name) ⇒ Object



31
32
33
34
# File 'lib/maven/tools/visitor.rb', line 31

def end_raw_tag( name )
  dec
  @io.puts "#{indent}</#{name}>"
end

#end_tag(name) ⇒ Object



40
41
42
# File 'lib/maven/tools/visitor.rb', line 40

def end_tag( name )
  end_raw_tag( camel_case_lower( name ) )
end

#incObject



13
14
15
# File 'lib/maven/tools/visitor.rb', line 13

def inc
  @indent = @indent + '  '
end

#indentObject



9
10
11
# File 'lib/maven/tools/visitor.rb', line 9

def indent
  @indent ||= ''
end

#raw_tag(name, value) ⇒ Object



51
52
53
54
55
# File 'lib/maven/tools/visitor.rb', line 51

def raw_tag( name, value )
  unless value.nil?
    @io.puts "#{indent}<#{name}>#{value}</#{name}>"
  end
end

#start_raw_tag(name, attr = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/maven/tools/visitor.rb', line 21

def start_raw_tag( name, attr = {} )
  @io.print "#{indent}<#{name}"
  attr.each do |k,v|
    @io.puts
    @io.print "#{indent}  #{k.to_s[1..-1]}='#{v}'"
  end
  @io.puts ">"
  inc
end

#start_tag(name, attr = {}) ⇒ Object



36
37
38
# File 'lib/maven/tools/visitor.rb', line 36

def start_tag( name, attr = {} )
  start_raw_tag( camel_case_lower( name ), attr )
end

#tag(name, value) ⇒ Object



44
45
46
47
48
49
# File 'lib/maven/tools/visitor.rb', line 44

def tag( name, value )
  unless value.nil?
    name = camel_case_lower( name )
    @io.puts "#{indent}<#{name}>#{value}</#{name}>"
  end
end

#visit(model) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/maven/tools/visitor.rb', line 140

def visit( model )
  model.attributes.each do |k, v|
    if k == :properties
      accept_raw_hash( k, v )
    else
      case v
      when Virtus
        accept( k, v )
      when Array
        accept_array( k, v )
      when Hash
        accept_hash( k, v )
      else
        tag( k, v )
      end
    end
  end
end