opal_orm 0.2.0 → 0.3.0
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.
- checksums.yaml +4 -4
- data/README.md +3 -1
- data/lib/opal_orm/associatable.rb +3 -2
- data/lib/opal_orm/query_builder.rb +30 -10
- data/lib/opal_orm/schema_manager.rb +1 -1
- data/lib/opal_orm/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: de724970761475a02838dfbb324c487411a88404
|
4
|
+
data.tar.gz: b938bb3db8d8b24ff01b0951282306199c81460f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d94ad0e10e2e7e1105f85f71708d56eb11f96d372605d58b981d0d3b685419aad51d827fd1075c7ea6327e94110e60880a3b6ef00c2c371c84382b333ad7a5bc
|
7
|
+
data.tar.gz: c17881b5f537257552fb36d85cc14b9b7f9699a7084c3c7403ef62844620c118af5c0c90a904d7a18eaf20fd5d93c2435d94ada64939683523f8742d38f061e1
|
data/README.md
CHANGED
@@ -37,7 +37,7 @@ load "#{spec.gem_dir}/lib/tasks/opal_db.rake"
|
|
37
37
|
|
38
38
|
To start, run:
|
39
39
|
|
40
|
-
$ opal_orm
|
40
|
+
$ opal_orm new DATABASE_NAME
|
41
41
|
|
42
42
|
which will create an empty Sqlite database in `./db/DATABASE_NAME.db`.
|
43
43
|
|
@@ -59,6 +59,7 @@ For example,
|
|
59
59
|
create_table("cats") do |t|
|
60
60
|
t.string :name
|
61
61
|
t.integer :toy_count
|
62
|
+
t.float :weight
|
62
63
|
end
|
63
64
|
```
|
64
65
|
|
@@ -69,6 +70,7 @@ CREATE TABLE cats (
|
|
69
70
|
id INTEGER PRIMARY KEY,
|
70
71
|
name VARCHAR(255),
|
71
72
|
toy_count INTEGER
|
73
|
+
weight REAL
|
72
74
|
);
|
73
75
|
```
|
74
76
|
|
@@ -48,7 +48,8 @@ module Associatable
|
|
48
48
|
def has_many(name, options = {})
|
49
49
|
define_method(name) do
|
50
50
|
@options = HasManyOptions.new(name,self.class.to_s,options)
|
51
|
-
|
51
|
+
# TODO: why is this here?
|
52
|
+
# cats = @options.model_class.where(@options.foreign_key => id)
|
52
53
|
end
|
53
54
|
end
|
54
55
|
|
@@ -60,7 +61,7 @@ module Associatable
|
|
60
61
|
define_method(name) do
|
61
62
|
through_options = self.class.assoc_options[through_name]
|
62
63
|
source_options = through_options.model_class.assoc_options[source_name]
|
63
|
-
|
64
|
+
|
64
65
|
through =
|
65
66
|
through_options.model_class
|
66
67
|
.where(through_options.primary_key => id)
|
@@ -2,6 +2,13 @@ require 'active_support/inflector'
|
|
2
2
|
|
3
3
|
module OpalORM
|
4
4
|
class QueryBuilder
|
5
|
+
DATATYPES = {
|
6
|
+
string: "VARCHAR(255)",
|
7
|
+
integer: "INTEGER",
|
8
|
+
float: "REAL",
|
9
|
+
text: "TEXT"
|
10
|
+
}
|
11
|
+
|
5
12
|
def self.create_table_query(name, &prc)
|
6
13
|
manager = new(name)
|
7
14
|
prc.call(manager)
|
@@ -25,29 +32,35 @@ module OpalORM
|
|
25
32
|
@columns << {type: :integer, name: name, options: options}
|
26
33
|
end
|
27
34
|
|
35
|
+
def float(name, *options)
|
36
|
+
@columns << {type: :float, name: name, options: options}
|
37
|
+
end
|
38
|
+
|
39
|
+
def text(name, *options)
|
40
|
+
@columns << {type: :text, name: name, options: options}
|
41
|
+
end
|
42
|
+
|
28
43
|
# def foreign_key(ref_name)
|
29
44
|
# @foreign_keys << ref_name
|
30
45
|
# end
|
31
46
|
|
32
47
|
def build!
|
33
48
|
query_start = "CREATE TABLE #{@table_name} ("
|
49
|
+
|
34
50
|
column_queries = @columns.map do |col_info|
|
35
|
-
result = []
|
36
|
-
|
37
|
-
|
38
|
-
result = "#{col_info[:name]} VARCHAR(255) "
|
39
|
-
when :integer
|
40
|
-
result = "#{col_info[:name]} INTEGER "
|
41
|
-
end
|
51
|
+
result = ["#{col_info[:name]}"]
|
52
|
+
result << get_datatype_string(col_info[:type])
|
53
|
+
|
42
54
|
unless col_info[:options].nil?
|
43
55
|
col_info[:options].each do |key|
|
44
56
|
case col_info[:options]
|
45
57
|
when :null
|
46
|
-
result
|
58
|
+
result << "NOT NULL" unless col_info[:options][:null]
|
47
59
|
end
|
48
60
|
end
|
49
61
|
end
|
50
|
-
|
62
|
+
|
63
|
+
result.join(" ")
|
51
64
|
end
|
52
65
|
column_queries.unshift("id INTEGER PRIMARY KEY")
|
53
66
|
|
@@ -55,7 +68,7 @@ module OpalORM
|
|
55
68
|
if foreign_key_valid?(ref_name)
|
56
69
|
"FOREIGN KEY(#{ref_name}) REFERENCES #{@table_name.singularize}(id)"
|
57
70
|
else
|
58
|
-
raise ForeignKeyMissingError, "Error adding foreign key
|
71
|
+
raise ForeignKeyMissingError, "Error adding foreign key constraint for #{ref_name}: couldn't find column named #{ref_name}."
|
59
72
|
end
|
60
73
|
end
|
61
74
|
|
@@ -65,6 +78,10 @@ module OpalORM
|
|
65
78
|
@query
|
66
79
|
end
|
67
80
|
|
81
|
+
def get_datatype_string(type)
|
82
|
+
DATATYPES[type]
|
83
|
+
end
|
84
|
+
|
68
85
|
def foreign_key_valid?(key)
|
69
86
|
@columns.any? {|col| key == col[:name]}
|
70
87
|
end
|
@@ -72,4 +89,7 @@ module OpalORM
|
|
72
89
|
|
73
90
|
class ForeignKeyMissingError < StandardError
|
74
91
|
end
|
92
|
+
|
93
|
+
class DatatypeMissingError < StandardError
|
94
|
+
end
|
75
95
|
end
|
data/lib/opal_orm/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: opal_orm
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nathan Matteson
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-03-
|
11
|
+
date: 2016-03-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sqlite3
|