Class: Twat::Migrate

Inherits:
Object
  • Object
show all
Defined in:
lib/twat/migration.rb

Instance Method Summary collapse

Instance Method Details

#migrate!(filename) ⇒ Object



3
4
5
6
7
8
9
10
11
12
# File 'lib/twat/migration.rb', line 3

def migrate!(filename)
  @file = filename
  @ran = []
  migration_1 if migration_1?
  if @ran.any?
    puts "Successfully ran migrations: #{@ran.join(", ")}"
  else
    puts "Already up to date"
  end
end

#migration_1Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/twat/migration.rb', line 19

def migration_1
  new = { accounts: {} }
  default = nil
  current = YAML.load_file(@file)
  current.each do |k, v|
    k = k.to_sym
    if k == :default
      default = v[:oauth_token]
    else
      new[:accounts][k] = v
    end
  end

  new[:accounts].each do |k, v|
    if v[:oauth_token] == default
      new[:default] = k
      break
    end
  end

  save(new)
  @ran << "1"
end

#migration_1?Boolean

Returns:

  • (Boolean)


14
15
16
17
# File 'lib/twat/migration.rb', line 14

def migration_1?
  current = YAML.load_file(@file)
  return !current.include?(:accounts)
end

#save(cf) ⇒ Object



43
44
45
46
47
48
# File 'lib/twat/migration.rb', line 43

def save(cf)
  File.open(@file, 'w') do |conf|
    conf.chmod(0600)
    conf.puts(cf.to_yaml)
  end
end