gorm 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/gorm.rb +30 -0
- data/lib/gorm/column.rb +24 -0
- data/lib/gorm/migration.rb +48 -0
- data/lib/gorm/schema.rb +70 -0
- data/lib/gorm/table.rb +58 -0
- data/lib/gorm_version.rb +3 -0
- metadata +100 -0
data/lib/gorm.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'gorm/schema'
|
2
|
+
|
3
|
+
module Gorm
|
4
|
+
|
5
|
+
# Creates a Schema under the given package +path+ and makes it available to a
|
6
|
+
# block that can then define it using the Schema::SchemaDefinitionMethods.
|
7
|
+
# ==== Arguments
|
8
|
+
# * +path+ - A / separated path where the source files should be generated.
|
9
|
+
# Do not include any file extension, they will be added automatically
|
10
|
+
# depending on the target language.
|
11
|
+
# ==== Options
|
12
|
+
# * <tt>:namespace</tt> - If provided, all generated classes will be placed
|
13
|
+
# inside this namespace.
|
14
|
+
def self.schema(path, opt={}, &block) # :yields: s
|
15
|
+
s = Schema.new path, opt
|
16
|
+
block.call s unless s.nil?
|
17
|
+
s.generate!
|
18
|
+
nil
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.template_path(subpath) # :nodoc:
|
22
|
+
spec = Gem.loaded_specs['gorm']
|
23
|
+
prefix = if spec.nil?
|
24
|
+
File.expand_path File.join(File.dirname(__FILE__), '..')
|
25
|
+
else
|
26
|
+
spec.gem_dir
|
27
|
+
end
|
28
|
+
File.join prefix, 'templates', subpath
|
29
|
+
end
|
30
|
+
end
|
data/lib/gorm/column.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
module Gorm
|
2
|
+
|
3
|
+
# Represents a column in a Table.
|
4
|
+
class Column
|
5
|
+
|
6
|
+
# The name of this column, both in the database and the ORM class.
|
7
|
+
attr_reader :name
|
8
|
+
|
9
|
+
# The type of this column. For example, <tt>:string</tt> or <tt>:integer</tt> or ...
|
10
|
+
attr_reader :type
|
11
|
+
|
12
|
+
def initialize(name, type, opt={}) # :nodoc:
|
13
|
+
@name = name
|
14
|
+
@type = type
|
15
|
+
# TODO: process opts
|
16
|
+
end
|
17
|
+
|
18
|
+
def <=>(other) # :nodoc:
|
19
|
+
@name <=> other.name
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module Gorm
|
2
|
+
|
3
|
+
# A migration within a Schema
|
4
|
+
class Migration
|
5
|
+
|
6
|
+
# Defines the ordering of migrations.
|
7
|
+
attr_reader :index
|
8
|
+
|
9
|
+
def initialize(schema, opt={}) # :nodoc:
|
10
|
+
@schema = schema
|
11
|
+
@index = opt[:index]
|
12
|
+
end
|
13
|
+
|
14
|
+
# A special migration which is always the first in any schema.
|
15
|
+
# Introduces a meta table that is used to keep track of the current
|
16
|
+
# database schema version.
|
17
|
+
def self.new_meta(schema)
|
18
|
+
Migration.new schema, :index => 0
|
19
|
+
end
|
20
|
+
|
21
|
+
# Generate a stub for the migration method.
|
22
|
+
# ==== Options
|
23
|
+
# * <tt>:qualify</tt> - Should the stub be qualified with a class name?
|
24
|
+
def method_stub(opt={})
|
25
|
+
prefix = opt[:qualify] ? "#{@schema.class_name}::" : ""
|
26
|
+
"bool #{prefix}applyMigration#{@index}()"
|
27
|
+
end
|
28
|
+
|
29
|
+
# Generate a call to the migration method.
|
30
|
+
def method_call
|
31
|
+
"applyMigration#{@index}()"
|
32
|
+
end
|
33
|
+
|
34
|
+
# An interface for defining a Migration.
|
35
|
+
module MigrationDefinitionMethods
|
36
|
+
# Creates a Table called +name+ and makes it available to a block that
|
37
|
+
# can then define it using the Table::TableDefinitionMethods.
|
38
|
+
def create_table(name, &block) # :yields: t
|
39
|
+
t = @schema.register_table name
|
40
|
+
block.call t unless block.nil?
|
41
|
+
nil
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
include MigrationDefinitionMethods
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
data/lib/gorm/schema.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'gorm/migration'
|
2
|
+
require 'gorm/table'
|
3
|
+
# $LOAD_PATH << File.join(File.dirname(__FILE__), '../../../cog/lib')
|
4
|
+
require 'cog/mixins/uses_templates'
|
5
|
+
|
6
|
+
module Gorm
|
7
|
+
|
8
|
+
# A collection of migrations which cumulatively defines a database schema.
|
9
|
+
class Schema
|
10
|
+
|
11
|
+
include Cog::Mixins::UsesTemplates
|
12
|
+
|
13
|
+
# Where to place the generated code.
|
14
|
+
attr_reader :output_path
|
15
|
+
|
16
|
+
# Namespace to place generated class into.
|
17
|
+
attr_reader :namespace
|
18
|
+
|
19
|
+
# The class name to use for the generated schema class.
|
20
|
+
attr_reader :class_name
|
21
|
+
|
22
|
+
# List of migrations ordered as there were defined.
|
23
|
+
attr_reader :migrations
|
24
|
+
|
25
|
+
# The include guard name to use in the header file.
|
26
|
+
attr_reader :include_guard_name
|
27
|
+
|
28
|
+
def initialize(path, opt={}) # :nodoc:
|
29
|
+
@output_path = path.to_s
|
30
|
+
@include_guard_name = @output_path.gsub(/[^A-Za-z0-9]/, '_').upcase
|
31
|
+
@namespace = opt[:namespace]
|
32
|
+
@class_name = @output_path.split('/').last
|
33
|
+
@migrations = [Migration.new_meta self]
|
34
|
+
@tables = {}
|
35
|
+
end
|
36
|
+
|
37
|
+
# Register or retrieve a registered model
|
38
|
+
def register_table(name)
|
39
|
+
@tables[name] = @tables[name] || Table.new(self, name)
|
40
|
+
end
|
41
|
+
|
42
|
+
# Generate the ORM source code
|
43
|
+
def generate!
|
44
|
+
[:cpp, :h].each do |ext|
|
45
|
+
stamp Gorm::template_path("schema.#{ext}"),
|
46
|
+
:use_absolute_path => true,
|
47
|
+
:target => "#{@output_path}.#{ext}"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# Index of the last migration
|
52
|
+
def app_schema_version
|
53
|
+
@migrations.last.index
|
54
|
+
end
|
55
|
+
|
56
|
+
# An interface for defining a Schema
|
57
|
+
module SchemaDefinitionMethods
|
58
|
+
# Adds a new Migration to this schema and makes it available to a block
|
59
|
+
# that can then define it using the Migration::MigrationDefinitionMethods.
|
60
|
+
def migration(&block)
|
61
|
+
@migrations << Migration.new(self, :index => @migrations.last.index + 1)
|
62
|
+
block.call @migrations.last unless block.nil?
|
63
|
+
nil
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
include SchemaDefinitionMethods
|
68
|
+
|
69
|
+
end
|
70
|
+
end
|
data/lib/gorm/table.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'gorm/column'
|
2
|
+
require 'active_support/core_ext'
|
3
|
+
|
4
|
+
module Gorm
|
5
|
+
|
6
|
+
# Represents a table in a Schema.
|
7
|
+
class Table
|
8
|
+
|
9
|
+
# The name of the table in the database
|
10
|
+
attr_reader :table_name
|
11
|
+
|
12
|
+
# The name of the ORM class for this table
|
13
|
+
attr_reader :class_name
|
14
|
+
|
15
|
+
def initialize(schema, name) # :nodoc:
|
16
|
+
@schema = schema
|
17
|
+
@table_name = name.to_s.pluralize.dasherize
|
18
|
+
@class_name = name.to_s.singularize.camelize
|
19
|
+
@columns = {}
|
20
|
+
end
|
21
|
+
|
22
|
+
# A sorted list of columns in the table
|
23
|
+
def columns
|
24
|
+
@columns.values.sort
|
25
|
+
end
|
26
|
+
|
27
|
+
# An interface for defining a Table
|
28
|
+
module TableDefinitionMethods
|
29
|
+
# Define a Column in this table.
|
30
|
+
# ==== Arguments
|
31
|
+
# * +name+ - The name of the field. Use lowercase.
|
32
|
+
# * +type+ - One of <tt>:string</tt>, <tt>:text</tt>, <tt>:integer</tt>
|
33
|
+
# ==== Options
|
34
|
+
# * <tt>:limit</tt> - A maximum size when using <tt>:string</tt> type.
|
35
|
+
def column(name, type, opt={})
|
36
|
+
col = @columns[name] || Column.new(name, type, opt)
|
37
|
+
end
|
38
|
+
|
39
|
+
# Shortcut for a call to #column with +type+ set to <tt>:integer</tt>.
|
40
|
+
def integer(name, opt={})
|
41
|
+
column name, :integer, opt
|
42
|
+
end
|
43
|
+
|
44
|
+
# Shortcut for a call to #column with +type+ set to <tt>:string</tt>.
|
45
|
+
def string(name, opt={})
|
46
|
+
column name, :string, opt
|
47
|
+
end
|
48
|
+
|
49
|
+
# Shortcut for a call to #column with +type+ set to <tt>:text</tt>.
|
50
|
+
def text(name, opt={})
|
51
|
+
column name, :text, opt
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
include TableDefinitionMethods
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
data/lib/gorm_version.rb
ADDED
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gorm
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Kevin Tonon
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-10-27 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rake
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: rdoc
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
hash: 3
|
43
|
+
segments:
|
44
|
+
- 0
|
45
|
+
version: "0"
|
46
|
+
type: :development
|
47
|
+
version_requirements: *id002
|
48
|
+
description:
|
49
|
+
email: kevin@betweenconcepts.com
|
50
|
+
executables: []
|
51
|
+
|
52
|
+
extensions: []
|
53
|
+
|
54
|
+
extra_rdoc_files: []
|
55
|
+
|
56
|
+
files:
|
57
|
+
- lib/gorm/column.rb
|
58
|
+
- lib/gorm/migration.rb
|
59
|
+
- lib/gorm/schema.rb
|
60
|
+
- lib/gorm/table.rb
|
61
|
+
- lib/gorm.rb
|
62
|
+
- lib/gorm_version.rb
|
63
|
+
homepage: https://github.com/ktonon/gorm
|
64
|
+
licenses: []
|
65
|
+
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options:
|
68
|
+
- --title
|
69
|
+
- gorm
|
70
|
+
- -ri
|
71
|
+
require_paths:
|
72
|
+
- lib
|
73
|
+
- lib
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
hash: 3
|
80
|
+
segments:
|
81
|
+
- 0
|
82
|
+
version: "0"
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
none: false
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
hash: 3
|
89
|
+
segments:
|
90
|
+
- 0
|
91
|
+
version: "0"
|
92
|
+
requirements: []
|
93
|
+
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 1.8.24
|
96
|
+
signing_key:
|
97
|
+
specification_version: 3
|
98
|
+
summary: A code-Generating Object Relational Mapper.
|
99
|
+
test_files: []
|
100
|
+
|