class SchemaTools::SchemaBuilder
Attributes
schema_hash[RW]
version[RW]
Public Class Methods
build_schema(app)
click to toggle source
# File lib/schema_tools/schema_builder.rb, line 11 def self.build_schema(app) schema_filename = File.join(app.project_dir, 'schemas/schema.rb') dsl = new dsl.instance_eval(File.read(schema_filename)) return dsl.schema_hash, dsl.version end
Public Instance Methods
add_column(name, type)
click to toggle source
# File lib/schema_tools/schema_builder.rb, line 62 def add_column(name, type) if @opts[:id] && name == 'id' raise_id_error unless type == 'INTEGER' type << ' PRIMARY KEY AUTOINCREMENT' end @schema_hash[@current_table][name] = type end
add_primary(type, name)
click to toggle source
# File lib/schema_tools/schema_builder.rb, line 112 def add_primary(type, name) if @opts[:id] puts "SWISS DB WARNING: ignoring primary_key: #{name} because `id` is the default primary key" else type << ' PRIMARY KEY' type << ' AUTOINCREMENT' if type == 'INTEGER PRIMARY KEY' end type end
check_opts(opts)
click to toggle source
# File lib/schema_tools/schema_builder.rb, line 28 def check_opts(opts) raise 'schema must supply a hash before the block' unless opts.is_a? Hash raise 'schema must specify a version' unless opts[:version] end
ensure_primary_key()
click to toggle source
# File lib/schema_tools/schema_builder.rb, line 50 def ensure_primary_key # Check that the schema has one and only one primary key somewhere table_schema = @schema_hash[@current_table] valid_table = false if @opts[:id] table_schema['id'] = 'INTEGER PRIMARY KEY AUTOINCREMENT' unless table_schema.has_key? 'id' else primary_keys = table_schema.values.select{ |val| val.include? 'PRIMARY KEY' } raise_primary_keys_error(table_schema.keys) unless primary_keys.length == 1 end end
entity(class_name, opts={}, &block)
click to toggle source
# File lib/schema_tools/schema_builder.rb, line 37 def entity(class_name, opts={}, &block) # set default opts and merge passed in opts @opts = { id: true }.merge opts init_table(class_name) block.call ensure_primary_key end
init_table(class_name)
click to toggle source
# File lib/schema_tools/schema_builder.rb, line 45 def init_table(class_name) @current_table = class_name.tableize @schema_hash[@current_table] = {} end
integer32(column_name, column_opts={})
click to toggle source
# File lib/schema_tools/schema_builder.rb, line 86 def integer32(column_name, column_opts={}) type = 'INTEGER' type = add_primary(type, column_name) if column_opts[:primary_key] add_column column_name.to_s, type end
raise_id_error()
click to toggle source
# File lib/schema_tools/schema_builder.rb, line 92 def raise_id_error error_message = %Q( Your schema defines a non integer `id` column for #{@current_table}. If you do not wish to use the default id column, then you should specify the `id: false` option. ) raise error_message end
raise_primary_keys_error(primary_keys)
click to toggle source
# File lib/schema_tools/schema_builder.rb, line 101 def raise_primary_keys_error(primary_keys) error_message = %Q( Your schema specified `id: false` for #{@current_table} and therefore must specify one primary key for this table. Instead you specified #{primary_keys.length}. ) error_message += "These are the keys you gave: #{primary_keys}\n" if primary_keys.length > 1 raise error_message end
schema(opts={}, &block)
click to toggle source
# File lib/schema_tools/schema_builder.rb, line 20 def schema(opts={}, &block) check_opts(opts) @version = opts[:version] raise 'schema version must be an integer greater than 0' unless version_ok? @schema_hash = {} block.call end
string(column_name, column_opts={})
click to toggle source
# File lib/schema_tools/schema_builder.rb, line 80 def string(column_name, column_opts={}) type = 'VARCHAR' type = add_primary(type, column_name) if column_opts[:primary_key] add_column column_name.to_s, type end
version_ok?()
click to toggle source
# File lib/schema_tools/schema_builder.rb, line 33 def version_ok? version.is_a?(Integer) && version > 0 end