Class: MotionMigrate::IO

Inherits:
Object
  • Object
show all
Defined in:
lib/motion_migrate/motion_generate/io.rb

Class Method Summary collapse

Class Method Details

.current_schemaObject



46
47
48
49
50
51
# File 'lib/motion_migrate/motion_generate/io.rb', line 46

def current_schema
  plist = xcdatamodeld_path(".xccurrentversion")
  return nil unless File.exists?(plist)

  xcdatamodeld_path(Nokogiri::XML(File.open(plist)).at_xpath("/plist/dict/string").text)
end

.current_schema_versionObject



36
37
38
39
40
41
42
43
44
# File 'lib/motion_migrate/motion_generate/io.rb', line 36

def current_schema_version
  return nil unless path = current_schema

  version = nil
  path.match(/\.([0-9]+)\.xcdatamodel$/) do |match|
    version = match[1].to_i
  end
  version
end

.write(xml = "") ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/motion_migrate/motion_generate/io.rb', line 4

def write(xml="")
  create_db

  current_schema = nil
  if version = current_schema_version()
    File.open(File.join(current_schema(), "contents")) do |f|
      current_schema = f.read
    end
  end

  if current_schema != xml
    version = (version || 0) + 1

    latest_schema_path = xcdatamodeld_path("schema.#{version}.xcdatamodel")
    Dir.mkdir(latest_schema_path) unless Dir.exists?(latest_schema_path)

    File.open(File.join(latest_schema_path, "contents"), "w") do |file|
      file.write(xml)
    end
    write_current_schema(version)

    unless File.symlink?("resources/schema.xcdatamodeld")
      File.symlink("../db/schema.xcdatamodeld", "resources/schema.xcdatamodeld")
    end

    puts "# Data model migrated to version #{version}."
  else
    length = 38 + version.to_s.length
    puts "# Data model already at version #{version}."
  end
end

.write_current_schema(version) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/motion_migrate/motion_generate/io.rb', line 53

def write_current_schema(version)
  File.open(xcdatamodeld_path(".xccurrentversion"), "w") do |file|
    file.write(<<-PLIST)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>_XCCurrentVersionName</key>
  <string>schema.#{version}.xcdatamodel</string>
</dict>
</plist>
    PLIST
  end
end