class BrainDamage::Templateable::ClassTemplateable

Attributes

current_file_name[R]

Public Class Methods

new(resource, options = {}) click to toggle source
Calls superclass method BrainDamage::Templateable::Base::new
# File lib/generators/brain_damage/lib/templateable/class_templateable.rb, line 8
def initialize(resource, options = {})
  super
  @public_methods_templates = "#{dir}/templates/public_methods/*"
  @private_methods_templates = "#{dir}/templates/private_methods/*"

  @removed_methods = []
end

Public Instance Methods

always_overwrite_methods() click to toggle source
# File lib/generators/brain_damage/lib/templateable/class_templateable.rb, line 83
def always_overwrite_methods
  []
end
class_definition() click to toggle source
# File lib/generators/brain_damage/lib/templateable/class_templateable.rb, line 75
def class_definition
  @parser.class_definition.definition if @parser
end
extract_definitions() click to toggle source
# File lib/generators/brain_damage/lib/templateable/class_templateable.rb, line 67
def extract_definitions
  if @current_code
    @parser = RubySimpleParser::Parser.new @current_code
  else
    @parser = RubySimpleParser::Parser.new
  end
end
generate() click to toggle source
# File lib/generators/brain_damage/lib/templateable/class_templateable.rb, line 34
def generate
  render
end
leading_class_method_calls() click to toggle source
# File lib/generators/brain_damage/lib/templateable/class_templateable.rb, line 38
def leading_class_method_calls
  if @parser
    @parser.class_method_calls[:after_class_definition].map(&:print).uniq.join("\n")

  else
    ''
  end
end
methods(visibility) click to toggle source
# File lib/generators/brain_damage/lib/templateable/class_templateable.rb, line 47
def methods(visibility)
  definitions = {}

  identation = if visibility == :public then 1 else 2 end

  Dir[instance_variable_get("@#{visibility}_methods_templates")].map { |template_file|
    method_code = render_erb_file(template_file).indent identation
    method_name = RubySimpleParser::Method.extract_method_name method_code
    definitions[method_name] = method_code unless @removed_methods.include? method_name
  }

  if @parser
    @parser.send("#{visibility}_methods").each do |method_name, method|
      definitions[method_name] = method.print unless overwrite_method? method_name
    end
  end

  definitions.values.join("\n\n")
end
overwrite_method?(method_name) click to toggle source
# File lib/generators/brain_damage/lib/templateable/class_templateable.rb, line 79
def overwrite_method?(method_name)
  always_overwrite_methods.include? method_name
end
private_methods() click to toggle source
# File lib/generators/brain_damage/lib/templateable/class_templateable.rb, line 24
def private_methods
  methods :private
end
public_methods() click to toggle source
# File lib/generators/brain_damage/lib/templateable/class_templateable.rb, line 20
def public_methods
  methods :public
end
remove_methods(*methods) click to toggle source
# File lib/generators/brain_damage/lib/templateable/class_templateable.rb, line 16
def remove_methods(*methods)
  @removed_methods += methods
end
setup(file_name) click to toggle source
# File lib/generators/brain_damage/lib/templateable/class_templateable.rb, line 28
def setup(file_name)
  @current_file_name = file_name
  @current_code = File.read @current_file_name if File.exists? @current_file_name
  extract_definitions
end