Module: SchemaTools::Writer

Defined in:
lib/schema_tools/writer.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.appObject

Returns the value of attribute app.



4
5
6
# File 'lib/schema_tools/writer.rb', line 4

def app
  @app
end

.schemaObject

Returns the value of attribute schema.



4
5
6
# File 'lib/schema_tools/writer.rb', line 4

def schema
  @schema
end

Class Method Details

.create_schema_sql(schema, app) ⇒ Object



6
7
8
9
10
# File 'lib/schema_tools/writer.rb', line 6

def create_schema_sql(schema, app)
  @schema = schema
  @app = app
  write_schema_file
end

.write_raw_resource_file(filename, content) ⇒ Object



30
31
32
33
34
# File 'lib/schema_tools/writer.rb', line 30

def write_raw_resource_file(filename, content)
  # create raw directory if it doesn't exist
  FileUtils.mkdir_p(File.dirname(filename))
  File.write(filename, content)
end

.write_schema_fileObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/schema_tools/writer.rb', line 18

def write_schema_file
  sql = ''

  schema.each do |table_name, fields|
    fields_string = fields.map { |k, v| "  #{k}   #{v}" }.join(",\n")
    sql += "CREATE TABLE #{table_name}(\n#{fields_string}\n);\n\n"
  end

  filename = File.join(app.resources_dirs.first, 'raw', 'schema.sql')
  write_raw_resource_file(filename, sql)
end

.write_version_file(version, app) ⇒ Object



12
13
14
15
16
# File 'lib/schema_tools/writer.rb', line 12

def write_version_file(version, app)
  # TODO: Version checking, android expects monotonically increasing version ints
  filename = File.join(app.resources_dirs.first, 'raw', 'version')
  write_raw_resource_file(filename, version)
end