Class: Generable::Base

Inherits:
Object
  • Object
show all
Extended by:
Generable
Defined in:
lib/aina/generable/base.rb

Overview

Generable class

Direct Known Subclasses

PostType, Template

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Generable

accepts?, class_name_for, generable_types, get_after_generate, get_dir, get_replacements, get_template

Constructor Details

#initialize(name, options = nil) ⇒ Base

Returns a new instance of Base.



6
7
8
9
10
11
12
13
14
15
# File 'lib/aina/generable/base.rb', line 6

def initialize(name, options=nil)
  @original_name = format_name(name)
  @options = options
  @aina_version = Aina::VERSION
  @template = self.template
  @dir = self.dir
  @file = generate_file_name

  set_custom_vars
end

Instance Attribute Details

#aina_versionObject (readonly)

Returns the value of attribute aina_version.



4
5
6
# File 'lib/aina/generable/base.rb', line 4

def aina_version
  @aina_version
end

#fileObject (readonly)

Returns the value of attribute file.



4
5
6
# File 'lib/aina/generable/base.rb', line 4

def file
  @file
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/aina/generable/base.rb', line 4

def name
  @name
end

#supportsObject (readonly)

Returns the value of attribute supports.



4
5
6
# File 'lib/aina/generable/base.rb', line 4

def supports
  @supports
end

#templateObject (readonly)

Returns the value of attribute template.



4
5
6
# File 'lib/aina/generable/base.rb', line 4

def template
  @template
end

Instance Method Details

#after_generateObject



56
57
58
59
60
61
62
# File 'lib/aina/generable/base.rb', line 56

def after_generate
  if self.class.get_after_generate
    self.class.get_after_generate.each do |callback|
      self.send(callback)
    end
  end
end

#destroyObject



64
65
66
67
68
69
70
# File 'lib/aina/generable/base.rb', line 64

def destroy
  if File.exists?(@file)
    File.delete(@file)
  else
    raise "No #{self.class} with name #{@name}"
  end
end

#dirObject



33
34
35
# File 'lib/aina/generable/base.rb', line 33

def dir
  self.class.get_dir
end

#display_nameObject



17
18
19
20
21
22
# File 'lib/aina/generable/base.rb', line 17

def display_name
  name
    .split('_')
    .each {|w| w.capitalize! }
    .join(' ')
end

#generateObject



45
46
47
48
49
50
51
52
53
54
# File 'lib/aina/generable/base.rb', line 45

def generate
  text = File.read(@template)
  replacements.each do |replace|
    attribute = replace.gsub(/[{}]/, '')
    text.gsub!(/#{replace}/, self.send(attribute))
  end
  File.open(@file, "w") {|file| file.puts text}

  after_generate
end

#replacementsObject



41
42
43
# File 'lib/aina/generable/base.rb', line 41

def replacements
  self.class.get_replacements
end

#set_custom_varsObject



28
29
30
31
# File 'lib/aina/generable/base.rb', line 28

def set_custom_vars
  # Nothing here
  # This can be used in types to parse required instance variables
end