bezel-app 0.0.7 → 0.0.8
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/bezel-app.gemspec +1 -1
- data/lib/bezelrecord_base/bezelrecord_base.rb +95 -93
- data/lib/db_connection.rb +0 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 703eaccf4e5de1143a209437645a632304dd3390
|
4
|
+
data.tar.gz: 78819b0d174f637158a6c23e048783c30af75e3c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 13327481eda4e0e601de696d572a4885ecbe2830bd4935b2cd648601cb93ff901bc89be7896b1a86a51265b924af9be41d8a16e840e2a2fb90780cdec4ff7f7a
|
7
|
+
data.tar.gz: 0f54c6edb6d86cce7dc8eb8b1628007575785c8ee6d51b25310fc537603bd18108550886f348fe55b3ce0407059f8df0400d08e418284e7194a059fe76d914ff
|
data/bezel-app.gemspec
CHANGED
@@ -3,120 +3,122 @@ require_relative '../../lib/db_connection'
|
|
3
3
|
require_relative 'associatable'
|
4
4
|
require_relative 'searchable'
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
6
|
+
module Bezel
|
7
|
+
class BezelrecordBase
|
8
|
+
extend Associatable
|
9
|
+
extend Searchable
|
10
|
+
|
11
|
+
def self.columns
|
12
|
+
unless @columns
|
13
|
+
temp_columns = DBConnection.execute2(<<-SQL)
|
14
|
+
SELECT
|
15
|
+
*
|
16
|
+
FROM
|
17
|
+
#{table_name}
|
18
|
+
SQL
|
19
|
+
@columns = temp_columns.first.map { |column| column.to_sym }
|
20
|
+
end
|
21
|
+
@columns
|
19
22
|
end
|
20
|
-
@columns
|
21
|
-
end
|
22
23
|
|
23
|
-
|
24
|
-
|
24
|
+
def self.finalize!
|
25
|
+
columns.each do |column|
|
25
26
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
27
|
+
define_method("#{column}") do
|
28
|
+
@attributes = self.attributes
|
29
|
+
@attributes[column]
|
30
|
+
end
|
30
31
|
|
31
|
-
|
32
|
-
|
33
|
-
|
32
|
+
define_method("#{column}=") do |value|
|
33
|
+
@attributes = self.attributes
|
34
|
+
@attributes[column] = value
|
35
|
+
end
|
34
36
|
end
|
35
37
|
end
|
36
|
-
end
|
37
38
|
|
38
|
-
|
39
|
-
|
40
|
-
|
39
|
+
def self.table_name=(table_name)
|
40
|
+
@table_name = table_name
|
41
|
+
end
|
41
42
|
|
42
|
-
|
43
|
-
|
44
|
-
|
43
|
+
def self.table_name
|
44
|
+
@table_name || to_s.tableize
|
45
|
+
end
|
45
46
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
47
|
+
def self.all
|
48
|
+
results = DBConnection.execute(<<-SQL)
|
49
|
+
SELECT
|
50
|
+
#{table_name}.*
|
51
|
+
FROM
|
52
|
+
#{table_name}
|
53
|
+
SQL
|
54
|
+
parse_all(results)
|
55
|
+
end
|
55
56
|
|
56
|
-
|
57
|
-
|
58
|
-
|
57
|
+
def self.parse_all(results)
|
58
|
+
objects = results.map do |object|
|
59
|
+
self.new(object)
|
60
|
+
end
|
59
61
|
end
|
60
|
-
end
|
61
62
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
63
|
+
def self.find(id)
|
64
|
+
object = DBConnection.execute(<<-SQL, id)
|
65
|
+
SELECT
|
66
|
+
*
|
67
|
+
FROM
|
68
|
+
#{table_name}
|
69
|
+
WHERE
|
70
|
+
id = ?
|
71
|
+
LIMIT
|
72
|
+
1
|
73
|
+
SQL
|
74
|
+
nil || object.map {|obj| self.new(obj)}.first
|
75
|
+
end
|
75
76
|
|
76
|
-
|
77
|
-
|
77
|
+
def initialize(params = {})
|
78
|
+
table_columns = self.class.columns
|
78
79
|
|
79
|
-
|
80
|
-
|
81
|
-
|
80
|
+
params.each do |key, value|
|
81
|
+
raise "unknown attribute '#{key}'" unless table_columns.include?(key.to_sym)
|
82
|
+
send("#{key}=",value)
|
83
|
+
end
|
82
84
|
end
|
83
|
-
end
|
84
85
|
|
85
|
-
|
86
|
-
|
87
|
-
|
86
|
+
def attributes
|
87
|
+
@attributes = @attributes || {}
|
88
|
+
end
|
88
89
|
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
90
|
+
def attribute_values
|
91
|
+
table_cols = self.class.columns
|
92
|
+
table_cols
|
93
|
+
table_cols.map{ |col| self.send("#{col}")}
|
94
|
+
end
|
94
95
|
|
95
|
-
|
96
|
-
|
96
|
+
def insert
|
97
|
+
columns = self.class.columns
|
97
98
|
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
99
|
+
DBConnection.execute(<<-SQL, *attribute_values)
|
100
|
+
INSERT INTO
|
101
|
+
#{self.class.table_name} (#{columns.join(", ")})
|
102
|
+
VALUES
|
103
|
+
(#{Array.new(columns.length,"?").join(", ")})
|
104
|
+
SQL
|
105
|
+
self.send(:id=,DBConnection.last_insert_row_id)
|
106
|
+
end
|
106
107
|
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
108
|
+
def update
|
109
|
+
DBConnection.execute(<<-SQL,*attribute_values[1..-1], attribute_values.first)
|
110
|
+
UPDATE
|
111
|
+
#{self.class.table_name}
|
112
|
+
SET
|
113
|
+
#{self.class.columns[1..-1].map{|col| "#{col} = ?"}.join(", ")}
|
114
|
+
WHERE
|
115
|
+
id = ?
|
116
|
+
SQL
|
116
117
|
|
117
|
-
|
118
|
+
end
|
118
119
|
|
119
|
-
|
120
|
-
|
120
|
+
def save
|
121
|
+
id.nil? ? insert : update
|
122
|
+
end
|
121
123
|
end
|
122
124
|
end
|
data/lib/db_connection.rb
CHANGED
@@ -4,7 +4,6 @@ require 'yaml'
|
|
4
4
|
PRINT_QUERIES = ENV['PRINT_QUERIES'] == 'true'
|
5
5
|
MIGRATIONS = Dir.glob('./db/migrate/*.sql').to_a
|
6
6
|
|
7
|
-
module Bezel
|
8
7
|
class DBConnection
|
9
8
|
def self.app_name
|
10
9
|
YAML.load_file(Dir.pwd + '/config/database.yml')['database']
|
@@ -114,4 +113,3 @@ module Bezel
|
|
114
113
|
commands.each { |command| `#{command}` }
|
115
114
|
end
|
116
115
|
end
|
117
|
-
end
|