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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f1cd945fd3b9484b0580826d90a4d9bcd537ca4b
4
- data.tar.gz: 0ba5cb818f4320bfce0660722fec1cab525dc0d1
3
+ metadata.gz: de724970761475a02838dfbb324c487411a88404
4
+ data.tar.gz: b938bb3db8d8b24ff01b0951282306199c81460f
5
5
  SHA512:
6
- metadata.gz: c25f6c0b2496dc42e5d3429132290b191d587438b6ab77ba4747a3ce36b36bbe5fb9f6164d54958f7ed70e014e34fe581c511347956b63730f87037e0f515fa6
7
- data.tar.gz: 11ef2d0c87d8a977877320b1e579f48ac15924b00207ab932c399dcabf2728ae0919264c429a8c2392d61f05dafe065fbc7dad06202134a4be038c5352eccc4b
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 create DATABASE_NAME
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
- cats = @options.model_class.where(@options.foreign_key => id)
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
- pk = 1
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
- case col_info[:type]
37
- when :string
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 += "NOT NULL" unless col_info[:options][:null]
58
+ result << "NOT NULL" unless col_info[:options][:null]
47
59
  end
48
60
  end
49
61
  end
50
- result
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 contsraint for #{ref_name}: couldn't find column named #{ref_name}."
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
@@ -39,7 +39,7 @@ end
39
39
  RB
40
40
  end
41
41
 
42
- def comment_string
42
+ def self.comment_string
43
43
  <<-RB
44
44
  # Example usage:
45
45
  # create_table 'table_name' do |t|
@@ -1,3 +1,3 @@
1
1
  module OpalOrm
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
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.2.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-17 00:00:00.000000000 Z
11
+ date: 2016-03-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sqlite3