Class: Migrate::Conf

Inherits:
Object
  • Object
show all
Defined in:
lib/migrate/conf.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, file) ⇒ Conf

Returns a new instance of Conf.



7
8
9
10
11
12
# File 'lib/migrate/conf.rb', line 7

def initialize(root, file)
  @root = root
  @file=file
  @file_path = "#{root}/#{file}"
  @loaded = false
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



5
6
7
# File 'lib/migrate/conf.rb', line 5

def root
  @root
end

Instance Method Details

#deleteObject



54
55
56
57
58
59
60
61
# File 'lib/migrate/conf.rb', line 54

def delete
  if File.exists? @file_path
    File.delete @file_path
  end
rescue Exception => e
  Log.error("Error while removing configuration file.", e)
  exit
end

#exists?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/migrate/conf.rb', line 14

def exists?
  File.exist? @file_path
end

#get_dbObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/migrate/conf.rb', line 63

def get_db
  case @storage
  when "pg"
    if @pg == nil
      require_relative "./storage/postgres"
      @pg = Storage::Postgres.new(self)
    end

    @pg
  when "mysql"
    if @mysql == nil
      require_relative "./storage/mysql"
      @mysql = Storage::Mysql.new(self)
    end

    @mysql
  end
end

#get_langObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/migrate/conf.rb', line 82

def get_lang
  case @lang
  when "sql"
    if @sql == nil
      @sql = Lang::Sql.new(get_db)
    end

    @sql
  when "javascript"
    if @javascript == nil
      @javascript = Lang::Javascript.new
    end

    @javascript
  when "ruby"
    if @ruby == nil
      @ruby = Lang::Ruby.new
    end

    @ruby
  when "go"
    if @go == nil
      @go = Lang::Go.new
    end

    @go
  when "python"
    if @python == nil
      @python = Lang::Python.new
    end

    @python
  end
end

#init(config) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/migrate/conf.rb', line 18

def init(config)
  if not Dir.exist? @root
    Dir.mkdir @root
  end

  File.open(@file_path, "w") do |f|
    config.map do |key, value|
      f.puts "#{key}=#{value}\n"
    end
  end

  Log.success("Configuration file created. Location: `#{@file_path}`")
end

#load!Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/migrate/conf.rb', line 32

def load!
  Log.info("Loading configuration...")
  config = ParseConfig.new(@file_path)

  config.get_params.map do |param|
    value = nil
    env_var = config[param].match(/\$\{(.*)\}/)

    if env_var != nil
      value = ENV[env_var[1]]
    else
      value = config[param]
    end

    self.class.send(:attr_reader, param)
    instance_variable_set("@#{param}", value)
  end

  @loaded = true
  Log.success("Configuration loaded.")
end