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

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)
	@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



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

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

#destroyObject



53
54
55
56
57
58
59
# File 'lib/aina/generable/base.rb', line 53

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

#dirObject



22
23
24
# File 'lib/aina/generable/base.rb', line 22

def dir
	self.class.get_dir
end

#generateObject



34
35
36
37
38
39
40
41
42
43
# File 'lib/aina/generable/base.rb', line 34

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

 	after_generate
end

#replacementsObject



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

def replacements
	self.class.get_replacements
end

#set_custom_varsObject



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

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