Class: Sprout::Generator::NamedBase

Inherits:
RubiGen::Base show all
Defined in:
lib/sprout/generator/named_base.rb

Overview

The NamedBase is a good default base class for ActionScript class Generators. This class will accept the first command line argument and create many helpful properties for concrete generators to use.

Can accept class names in following formats:

* src/package/package/ClassName.as
* test/package/package/ClassName.as
* package/package/ClassName
* package.package.ClassName

Regardless of which format the name was sent in, the helper methods should provide valid results

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(runtime_args, runtime_options = {}) ⇒ NamedBase

Returns a new instance of NamedBase.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sprout/generator/named_base.rb', line 34

def initialize(runtime_args, runtime_options = {})
  super

  rakefile = Sprout.project_rakefile
  if(rakefile && File.exists?(rakefile))
     load rakefile
  end
  @model = ProjectModel.instance

  # Had to stop throwing on no args because the suite generator does
  # not require the user to send in a class name....
  #usage("Final argument must be a name parameter") if runtime_args.empty?
  @args = runtime_args.dup
  assign_names! @args.shift.dup
end

Instance Attribute Details

#class_dirObject (readonly)

The directory that contains the file, relative to your src_dir like ‘flash/diplay’



28
29
30
# File 'lib/sprout/generator/named_base.rb', line 28

def class_dir
  @class_dir
end

#class_fileObject (readonly)

Path to the class file relative to your src_dir like ‘flash/display/Sprite.as’



26
27
28
# File 'lib/sprout/generator/named_base.rb', line 26

def class_file
  @class_file
end

#class_nameObject (readonly)

The unqualified name of the class like ‘Sprite’



30
31
32
# File 'lib/sprout/generator/named_base.rb', line 30

def class_name
  @class_name
end

#full_class_nameObject (readonly)

Fully qualified class named including package name like ‘flash.display.Sprite’



24
25
26
# File 'lib/sprout/generator/named_base.rb', line 24

def full_class_name
  @full_class_name
end

#package_nameObject (readonly)

The package name of the class like ‘flash.display’



32
33
34
# File 'lib/sprout/generator/named_base.rb', line 32

def package_name
  @package_name
end

Instance Method Details

#actionscript_file_to_class_name(file) ⇒ Object

Transform a file name in the source or test path to a fully-qualified class name



176
177
178
179
180
181
182
183
184
# File 'lib/sprout/generator/named_base.rb', line 176

def actionscript_file_to_class_name(file)
  name = file.dup
  name.gsub!(/^#{Dir.pwd}\//, '')
  name.gsub!(/^#{test_dir}\//, '')
  name.gsub!(/^#{src_dir}\//, '')
  name.gsub!(/.as$/, '')
  name.gsub!(/#{File::SEPARATOR}/, '.')
  return name
end

#full_class_dirObject

Full path to the parent directory that contains the class like ‘src/flash/display’ for flash.display.Sprite class.



95
96
97
98
99
100
# File 'lib/sprout/generator/named_base.rb', line 95

def full_class_dir
  @full_class_dir ||= File.join(src_dir, class_dir)
  # pull trailing slash for classes in the root package
  @full_class_dir.gsub!(/\/$/, '')
  @full_class_dir
end

#full_class_pathObject

Full path to the class file from your project_path like ‘src/flash/display/Sprite.as’



103
104
105
# File 'lib/sprout/generator/named_base.rb', line 103

def full_class_path
  @full_class_path ||= File.join(src_dir, class_file)
end

#full_test_case_nameObject

Full name of the possible test case for this class_name



89
90
91
# File 'lib/sprout/generator/named_base.rb', line 89

def full_test_case_name
  full_class_name + 'Test'
end

#full_test_case_pathObject

Filesystem path to the TestCase file



113
114
115
# File 'lib/sprout/generator/named_base.rb', line 113

def full_test_case_path
  @full_test_case_path ||= File.join(full_test_dir, test_case_name + '.as')
end

#full_test_dirObject

Filesystem path to the folder that contains the TestCase file



108
109
110
# File 'lib/sprout/generator/named_base.rb', line 108

def full_test_dir
  @full_test_dir ||= full_class_dir.gsub(src_dir, test_dir)
end

#generate_script_pathObject

Path to the in-project generate script pulled out for DOS branching



119
120
121
122
123
124
125
126
# File 'lib/sprout/generator/named_base.rb', line 119

def generate_script_path
  usr = User.new
  if(usr.is_a?(WinUser) && !usr.is_a?(CygwinUser))
    return File.join(class_name, 'script', "generate.rb")
  else
    return File.join(class_name, 'script', "generate")
  end
end

#instance_nameObject



128
129
130
131
132
133
134
135
136
# File 'lib/sprout/generator/named_base.rb', line 128

def instance_name
  name = class_name.dup;
  char = name[0, 1]
  name[0, 1] = char.downcase
  if(name.size > 10)
    name = 'instance'
  end
  return name
end

#languageObject

The technology language that is stored in your project model usually (as2 or as3)



79
80
81
# File 'lib/sprout/generator/named_base.rb', line 79

def language
  return model.language
end

#lib_dirObject

Quick access to the library directory identified by your project model



61
62
63
# File 'lib/sprout/generator/named_base.rb', line 61

def lib_dir
  return model.lib_dir
end

#project_nameObject

The project_name that was either sent to the sprout gem or defined in your rakefile project model



74
75
76
# File 'lib/sprout/generator/named_base.rb', line 74

def project_name
  @project_name ||= (Sprout.project_name || model.project_name)
end

#project_pathObject

The path to your project. This will either be the directory from which the sprout gem was executed, or the nearest ancestor directory that contains a properly named rakefile.



68
69
70
# File 'lib/sprout/generator/named_base.rb', line 68

def project_path
  return model.project_path
end

#src_dirObject

Quick access to the source directory identified by your project model



51
52
53
# File 'lib/sprout/generator/named_base.rb', line 51

def src_dir
  return model.src_dir
end

#test_case_classesObject

Get the list of test_cases (which are files) as a list of fully qualified class names



166
167
168
169
170
171
172
# File 'lib/sprout/generator/named_base.rb', line 166

def test_case_classes
  @test_case_classes = self.test_cases.dup
  @test_case_classes.collect! do |file|
    actionscript_file_to_class_name(file)
  end
  @test_case_classes
end

#test_case_nameObject

Name of possible test case for this class_name



84
85
86
# File 'lib/sprout/generator/named_base.rb', line 84

def test_case_name
  @test_case_name ||= class_name + 'Test'
end

#test_casesObject

Collection of all test case files either assigned or found using the test_glob as provided.



156
157
158
# File 'lib/sprout/generator/named_base.rb', line 156

def test_cases
  @test_cases ||= Dir.glob(test_dir + test_glob)
end

#test_cases=(collection) ⇒ Object



160
161
162
# File 'lib/sprout/generator/named_base.rb', line 160

def test_cases=(collection)
  @test_cases = collection
end

#test_dirObject

Quick access to the test directory identified by your project model



56
57
58
# File 'lib/sprout/generator/named_base.rb', line 56

def test_dir
  return model.test_dir
end

#test_globObject

Glob that is used to search for test cases and build up the test suites



146
147
148
# File 'lib/sprout/generator/named_base.rb', line 146

def test_glob
  return @test_glob ||= '**/**/?*Test.as'
end

#test_glob=(glob) ⇒ Object



150
151
152
# File 'lib/sprout/generator/named_base.rb', line 150

def test_glob=(glob)
  @test_glob = glob
end

#user_requested_testObject

Will return whether the user originally requested a class name that looks like a test case (e.g., name.match(/Test$/) )



140
141
142
# File 'lib/sprout/generator/named_base.rb', line 140

def user_requested_test
  @user_requested_test ||= false
end