class Codestrap::Stub::Abstract

@abstract

Template renderer class

Methods (aliased to abstract) that require overriding

#pre

Pre execution

#execute

Render execution

Methods that may be overridden

#post

Attributes

dst[R]

Path to destination project @!attribute [r] dst @return [String]

file[W]

Set file object @!attribute [w] file @return [File]

objects[RW]

Objects utilized in templates @!attribute [rw] objects @return [Array<Codestrap::Object::Factory>]

overwrite[RW]

Allow overwriting of files @!attribute [rw] overwrite @return [true|false]

src[R]

Path to template @!attribute [r] src @return [String]

Public Class Methods

abstract_methods() click to toggle source

List methods aliased Codestrap::Template::Abstract#abstract @return [Array]

# File lib/codestrap/stub/abstract.rb, line 37
def self.abstract_methods
  instance_methods.group_by { |m| instance_method(m) }.map(&:last).keep_if { |sym| sym.length > 1 }.map { |methods|
    if methods.include?('abstract'.to_sym)
      return methods.map { |method| method.to_s }.select { |method| method != 'abstract' }
    end
  }
  []
end
new() click to toggle source

Abstract class. Raise error on instantiation

# File lib/codestrap/stub/abstract.rb, line 98
def initialize()
  raise RendererAbstractOnly, 'Abstract Class Requires implementation'
end

Public Instance Methods

abstract() click to toggle source

@abstract Method(s) aliased to this method are considered abstract @raise [RendererRequiredMethod]

Raise an error if a method remains aliased to this method
# File lib/codestrap/stub/abstract.rb, line 106
def abstract
  raise RendererRequiredMethod, "Method #{__method__.to_s} not implemented"
end
Also aliased as: pre, execute
dst=(dst) click to toggle source

Set destination file

@param [String] dst

Path to destination file
# File lib/codestrap/stub/abstract.rb, line 85
def dst=(dst)
  if !overwrite? and File.exist? dst
    Codestrap::Core.logger.error(:FILEEXISTS, dst)
    exit 255
  end
  unless File.exist?(File.dirname(dst))
    Codestrap::Core.logger.error(:NOPATH, dst)
    exit 255
  end
  @dst = dst
end
execute()

Execution method

Alias for: abstract
file() click to toggle source

Creates and returns Tempfile to working file

@return [Tempfile]

Path to temporary file
# File lib/codestrap/stub/abstract.rb, line 124
def file
  @file ||= Tempfile.new('codestrap')
end
overwrite?() click to toggle source

Check overwrite attribute @return [true|false]

# File lib/codestrap/stub/abstract.rb, line 68
def overwrite?
  !!@overwite
end
post() click to toggle source

Post execution method

# File lib/codestrap/stub/abstract.rb, line 117
def post
end
pre()

Pre execution method

Alias for: abstract
src=(src) click to toggle source

Set source template

@param [String] src

Path to source file
# File lib/codestrap/stub/abstract.rb, line 76
def src=(src)
  raise Exception, %q[File doesn't exist] unless File.exist?(src)
  @src = src
end
to_disk() click to toggle source

Moves file contents to dst

@return [Integer]

Returns 0 on success
# File lib/codestrap/stub/abstract.rb, line 132
def to_disk
  @file.close
  tmp = open(@file.path)
  final = Tempfile.new('codestrap')
  strip_modeline(tmp, final)

  tmp.close
  File.unlink(@file.path)
  final.close(unlink_now = false)

  FileUtils.mv final.path, self.dst
end

Private Instance Methods

strip_modeline(src, dst, line_count=10) click to toggle source
# File lib/codestrap/stub/abstract.rb, line 147
def strip_modeline(src, dst, line_count=10)
  i = 0
  src.each_line do |line|
    i += 1
    dst.write(line) unless line =~ /(?:\b|^)(?:strap|stub):(?:erb|)(?:\b|$)/ and i <= line_count
  end
end