Class: HeyDan::ScriptFile

Inherits:
Object
  • Object
show all
Defined in:
lib/heydan/script_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(folder, source, variable) ⇒ ScriptFile

Returns a new instance of ScriptFile.



11
12
13
14
15
16
17
# File 'lib/heydan/script_file.rb', line 11

def initialize(folder, source, variable)
  @folder, @source, @variable = folder, source, variable
  @name = @folder + "_" + @source + "_" + @variable
  create_class_name
  @script_folder_path = File.join(HeyDan.folders[:sources], @folder, 'scripts')
  @script_file_path = File.join(@script_folder_path, "#{@name}.rb")
end

Instance Attribute Details

#class_nameObject

Returns the value of attribute class_name.



7
8
9
# File 'lib/heydan/script_file.rb', line 7

def class_name
  @class_name
end

#folderObject

Returns the value of attribute folder.



2
3
4
# File 'lib/heydan/script_file.rb', line 2

def folder
  @folder
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/heydan/script_file.rb', line 8

def name
  @name
end

#script_file_pathObject

Returns the value of attribute script_file_path.



6
7
8
# File 'lib/heydan/script_file.rb', line 6

def script_file_path
  @script_file_path
end

#script_folder_pathObject

Returns the value of attribute script_folder_path.



5
6
7
# File 'lib/heydan/script_file.rb', line 5

def script_folder_path
  @script_folder_path
end

#sourceObject

Returns the value of attribute source.



3
4
5
# File 'lib/heydan/script_file.rb', line 3

def source
  @source
end

#variableObject

Returns the value of attribute variable.



4
5
6
# File 'lib/heydan/script_file.rb', line 4

def variable
  @variable
end

Instance Method Details

#create_class_nameObject



23
24
25
# File 'lib/heydan/script_file.rb', line 23

def create_class_name
  @class_name = @name.split('_').collect(&:capitalize).join
end

#create_script_folderObject



19
20
21
# File 'lib/heydan/script_file.rb', line 19

def create_script_folder
  FileUtils.mkdir_p @script_folder_path if !Dir.exist?(@script_folder_path)
end

#eval_classObject



41
42
43
44
# File 'lib/heydan/script_file.rb', line 41

def eval_class
  load script_file_path
  return eval("#{@class_name}")
end

#saveObject



34
35
36
37
38
39
# File 'lib/heydan/script_file.rb', line 34

def save
  create_script_folder
  File.open(@script_file_path, 'w') do |f|
    f.write(template)
  end
end

#templateObject



27
28
29
30
31
32
# File 'lib/heydan/script_file.rb', line 27

def template

  template_path = File.join(File.dirname(__FILE__), 'templates', 'script.rb.erb')
  require 'erb'
  ERB.new(File.read(template_path)).result binding
end