Class: BrainDamage::Templateable::ClassTemplateable

Inherits:
Base
  • Object
show all
Defined in:
lib/generators/brain_damage/lib/templateable/class_templateable.rb

Direct Known Subclasses

ControllerGenerator, ModelGenerator

Instance Attribute Summary collapse

Attributes inherited from Base

#inner_html, #options, #template_file

Instance Method Summary collapse

Methods inherited from Base

#indent, #method_missing, #render, #render_erb_file, #render_erb_string, #render_template_file

Constructor Details

#initialize(resource, options = {}) ⇒ ClassTemplateable

Returns a new instance of ClassTemplateable.



8
9
10
11
12
13
14
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class BrainDamage::Templateable::Base

Instance Attribute Details

#current_file_nameObject (readonly)

Returns the value of attribute current_file_name.



6
7
8
# File 'lib/generators/brain_damage/lib/templateable/class_templateable.rb', line 6

def current_file_name
  @current_file_name
end

Instance Method Details

#always_overwrite_methodsObject



83
84
85
# File 'lib/generators/brain_damage/lib/templateable/class_templateable.rb', line 83

def always_overwrite_methods
  []
end

#class_definitionObject



75
76
77
# File 'lib/generators/brain_damage/lib/templateable/class_templateable.rb', line 75

def class_definition
  @parser.class_definition.definition if @parser
end

#extract_definitionsObject



67
68
69
70
71
72
73
# 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

#generateObject



34
35
36
# File 'lib/generators/brain_damage/lib/templateable/class_templateable.rb', line 34

def generate
  render
end

#leading_class_method_callsObject



38
39
40
41
42
43
44
45
# 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) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# 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_methodsObject



24
25
26
# File 'lib/generators/brain_damage/lib/templateable/class_templateable.rb', line 24

def private_methods
  methods :private
end

#public_methodsObject



20
21
22
# File 'lib/generators/brain_damage/lib/templateable/class_templateable.rb', line 20

def public_methods
  methods :public
end

#remove_methods(*methods) ⇒ Object



16
17
18
# File 'lib/generators/brain_damage/lib/templateable/class_templateable.rb', line 16

def remove_methods(*methods)
  @removed_methods += methods
end

#setup(file_name) ⇒ Object



28
29
30
31
32
# 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