Class Sprout::Generator::NamedBase
In: sprout/lib/sprout/generator/named_base.rb
Parent: RubiGen::Base

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

Methods

Attributes

class_dir  [R]  The directory that contains the file, relative to your src_dir like ‘flash/diplay‘
class_file  [R]  Path to the class file relative to your src_dir like ‘flash/display/Sprite.as‘
class_name  [R]  The unqualified name of the class like ‘Sprite‘
full_class_name  [R]  Fully qualified class named including package name like ‘flash.display.Sprite‘
package_name  [R]  The package name of the class like ‘flash.display‘

Public Class methods

[Source]

# File sprout/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
      end

Public Instance methods

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

[Source]

# File sprout/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 path to the parent directory that contains the class like ‘src/flash/display’ for flash.display.Sprite class.

[Source]

# File sprout/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 path to the class file from your project_path like ‘src/flash/display/Sprite.as‘

[Source]

# File sprout/lib/sprout/generator/named_base.rb, line 103
      def full_class_path
        @full_class_path ||= File.join(src_dir, class_file)
      end

Full name of the possible test case for this class_name

[Source]

# File sprout/lib/sprout/generator/named_base.rb, line 89
      def full_test_case_name
        full_class_name + 'Test'
      end

Filesystem path to the TestCase file

[Source]

# File sprout/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

Filesystem path to the folder that contains the TestCase file

[Source]

# File sprout/lib/sprout/generator/named_base.rb, line 108
      def full_test_dir
        @full_test_dir ||= full_class_dir.gsub(src_dir, test_dir)
      end

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

[Source]

# File sprout/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

[Source]

# File sprout/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

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

[Source]

# File sprout/lib/sprout/generator/named_base.rb, line 79
      def language
        return model.language
      end

Quick access to the library directory identified by your project model

[Source]

# File sprout/lib/sprout/generator/named_base.rb, line 61
      def lib_dir
        return model.lib_dir
      end

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

[Source]

# File sprout/lib/sprout/generator/named_base.rb, line 74
      def project_name
        @project_name ||= (Sprout.project_name || model.project_name)
      end

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.

[Source]

# File sprout/lib/sprout/generator/named_base.rb, line 68
      def project_path
        return model.project_path
      end

Quick access to the source directory identified by your project model

[Source]

# File sprout/lib/sprout/generator/named_base.rb, line 51
      def src_dir
        return model.src_dir
      end

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

[Source]

# File sprout/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

Name of possible test case for this class_name

[Source]

# File sprout/lib/sprout/generator/named_base.rb, line 84
      def test_case_name
        @test_case_name ||= class_name + 'Test'
      end

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

[Source]

# File sprout/lib/sprout/generator/named_base.rb, line 156
      def test_cases
        @test_cases ||= Dir.glob(test_dir + test_glob)
      end

[Source]

# File sprout/lib/sprout/generator/named_base.rb, line 160
      def test_cases=(collection)
        @test_cases = collection
      end

Quick access to the test directory identified by your project model

[Source]

# File sprout/lib/sprout/generator/named_base.rb, line 56
      def test_dir
        return model.test_dir
      end

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

[Source]

# File sprout/lib/sprout/generator/named_base.rb, line 146
      def test_glob
        return @test_glob ||= '**/**/?*Test.as'
      end

[Source]

# File sprout/lib/sprout/generator/named_base.rb, line 150
      def test_glob=(glob)
        @test_glob = glob
      end

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

[Source]

# File sprout/lib/sprout/generator/named_base.rb, line 140
      def user_requested_test
        @user_requested_test ||= false
      end

Protected Instance methods

[Source]

# File sprout/lib/sprout/generator/named_base.rb, line 196
      def assign_names!(name)
        # trim file name suffix in case it was submitted
        name.gsub!(/\//, '.')
        name.gsub!(/\.as$/, '')
        name.gsub!(/\.mxml$/, '')
        if(model)
          # Pull leading src_dir from class name if submitted
          name.gsub!(/^#{src_dir}\./, '')
          name.gsub!(/^#{test_dir}\./, '')
        end

        if(name.match(/Test$/))
          @user_requested_test = true
          name = name.gsub(/Test$/, '')
        end
        if(name.match(/^I/) || name.match(/able$/))
          @user_requested_interface = true
        end

        @full_class_name = name
        parts = name.split('.')
        @class_name = parts.pop
        @package_name = parts.join('.')
        @class_file = @full_class_name.split('.').join(File::SEPARATOR) + '.as'
        @class_dir = File.dirname(@class_file)
        if(@class_dir == '.')
          @class_dir = ''
        end
      end

[Source]

# File sprout/lib/sprout/generator/named_base.rb, line 188
      def banner
        "Usage: #{$0} [options] packagename.ClassName"
      end

[Source]

# File sprout/lib/sprout/generator/named_base.rb, line 192
      def model
        @model ||= ProjectModel.instance
      end

[Validate]