Class: AsProject::AsClass

Inherits:
AsProjectBase show all
Defined in:
lib/asclass.rb

Constant Summary collapse

@@TEMPLATE_TYPE =
'asclass'
@@CLASS_TEMPLATE =
'Class.as'
@@TEST_CASE_TEMPLATE =
'TestCase.as'
@@TEST_SUITE_TEMPLATE =
'TestSuite.as'
@@INTERFACE_TEMPLATE =
'Interface.as'
@@COMPONENT_TEMPLATE =
'Component.as'
@@MXML_TEMPLATE =
'Component.mxml'

Instance Method Summary collapse

Methods inherited from AsProjectBase

#capitalize, #finish, #out, #put_created, #uncapitalize

Constructor Details

#initialize(args = nil) ⇒ AsClass

Returns a new instance of AsClass.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/asclass.rb', line 13

def initialize(args=nil)
#      begin
    @created_files = []
    @arguments = parse_args(args)
    @project = get_project(@arguments.execution_dir)
    if(!@arguments.name.nil?)
      configure_project(@arguments.name)
    end
    if(@arguments.force)
      @project.replace_all = true
    end
    execute
    finish
#      rescue Exception => e
#        puts e.to_s
#        exit
#      end
end

Instance Method Details

#class_templateObject



172
173
174
# File 'lib/asclass.rb', line 172

def class_template
  return @@CLASS_TEMPLATE
end

#component_templateObject



184
185
186
# File 'lib/asclass.rb', line 184

def component_template
  return @@COMPONENT_TEMPLATE
end

#configure_project(name) ⇒ Object

package_name class_name full_class_name test_case_name full_test_case_name instance_name add_child_line remove_child_line



130
131
132
133
134
135
136
137
138
# File 'lib/asclass.rb', line 130

def configure_project(name)
  @project['display_object'] = false
  @project['package_name'] = get_package_name(name)
  @project['class_name'] = get_class_name(name)
  @project['full_class_name'] = @project['package_name'] + '.' + @project['class_name']
  @project['test_case_name'] = get_test_case_name(name)
  @project['full_test_case_name'] = @project['package_name'] + '.' + @project['test_case_name']
  @project['instance_name'] = get_instance_name(name)
end

#create_class(template_file) ⇒ Object



81
82
83
84
85
# File 'lib/asclass.rb', line 81

def create_class(template_file)
  suffix = '.' + template_file.split(File::SEPARATOR).pop.split('.').pop
  target_name = capitalize(@project['class_name'] + suffix)
  create_file(template_file, @project.source_dir, target_name)
end

#create_file(template_file, target_dir, target_name) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/asclass.rb', line 93

def create_file(template_file, target_dir, target_name)
  template_base = select_template_base
  @arguments.verify_template(template_base)
  source_location = @project.get_template(template_type, File.join(template_base, template_file))
  package_dirs = @project['package_name'].split('.').join(File::SEPARATOR)
  target_location = File.join(target_dir, package_dirs, target_name)
  @created_files << @project.copy_file(source_location, target_location, true)
end

#create_test_case(template_file) ⇒ Object



87
88
89
90
91
# File 'lib/asclass.rb', line 87

def create_test_case(template_file)
  suffix = '.' + template_file.split(File::SEPARATOR).pop.split('.').pop
  target_name = capitalize(@project['class_name'] + 'Test' + suffix)
  create_file(template_file, @project.test_dir, target_name)
end

#create_test_suitesObject



110
111
112
113
114
115
116
117
118
119
# File 'lib/asclass.rb', line 110

def create_test_suites
  template_base = select_template_base
  template = @project.get_template(template_type, File.join(template_base, 'TestSuite.as'))
  content = IO.read(template)
  name = 'AllTests.as'
  suite_generator = TestSuiteGenerator.new(@project.test_dir, name, content)
  suite_generator.created_files.each do |suite|
    @created_files << suite
  end
end

#executeObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/asclass.rb', line 41

def execute
  name = @arguments.name
  
  # Only build class and test case if a name
  # was provided...
  if(!name.nil?)
    is_test_case = false
    is_interface = false

    template = ''
    if(@arguments.mxml)
      @project['display_object'] = true
      template = mxml_template
    elsif(@arguments.component)
      template = component_template
    elsif(name.match(/Test$/))
      is_test_case = true
      template = test_case_template
    elsif(name.match(/^I[A-Z]/) || name.match(/able$/))
      is_interface = true
      template = interface_template
    else
      template = class_template
    end
    
    if(!is_test_case)
      create_class(template)
    end
    
    if(!is_interface && @arguments.build_test_case || is_test_case)
      template = test_case_template
      create_test_case(template)
    end
  end
  
  if(@arguments.build_test_suites)
    create_test_suites
  end
end

#get_class_location(name) ⇒ Object



152
153
154
155
# File 'lib/asclass.rb', line 152

def get_class_location(name)
  pkg = get_package_name(name)
  return pkg.split('.').join(File::SEPARATOR)
end

#get_class_name(name) ⇒ Object



146
147
148
149
150
# File 'lib/asclass.rb', line 146

def get_class_name(name)
  name = name.sub(/Test$/, '')
  parts = name.split('.')
  return parts.pop
end

#get_instance_name(name) ⇒ Object



161
162
163
164
165
166
# File 'lib/asclass.rb', line 161

def get_instance_name(name)
  name = get_class_name(name)
  chars = name.split('')
  chars[0] = chars[0].downcase
  return chars.join('')
end

#get_package_name(name) ⇒ Object



140
141
142
143
144
# File 'lib/asclass.rb', line 140

def get_package_name(name)
  parts = name.split('.')
  parts.pop
  return parts.join('.')
end

#get_project(dir) ⇒ Object



192
193
194
195
196
197
# File 'lib/asclass.rb', line 192

def get_project(dir)
#      if(File.exists?(File.join(dir, '.project') || File.directory?(File.join(dir, 'config'))))
    return AsClassProject.new(dir)
#      end
#      raise ProjectError.new('AsClass could not find a .project file in the current directory: ' + dir)
end

#get_test_case_name(name) ⇒ Object



157
158
159
# File 'lib/asclass.rb', line 157

def get_test_case_name(name)
  return get_class_name(name) + 'Test'
end

#interface_templateObject



176
177
178
# File 'lib/asclass.rb', line 176

def interface_template
  return @@INTERFACE_TEMPLATE
end

#mxml_templateObject



188
189
190
# File 'lib/asclass.rb', line 188

def mxml_template
  return @@MXML_TEMPLATE
end

#parse_args(args = nil) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/asclass.rb', line 32

def parse_args(args=nil)
  if(args.nil?)
    args = AsClassArguments.new
    args.execution_dir = Dir.pwd
    args.parse!(ARGV)
  end
  return args
end

#select_template_baseObject



102
103
104
105
106
107
108
# File 'lib/asclass.rb', line 102

def select_template_base
  if(@arguments.template.nil?)
    return @project.default_templates[0]
  else
    return @arguments.template
  end
end

#template_typeObject



168
169
170
# File 'lib/asclass.rb', line 168

def template_type
  return @@TEMPLATE_TYPE
end

#test_case_templateObject



180
181
182
# File 'lib/asclass.rb', line 180

def test_case_template
  return @@TEST_CASE_TEMPLATE
end