a_series_of_tubes 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1dc91bfe720e392510cb64ea9ff4c83d7eaadf4c
4
- data.tar.gz: 46d79a43419784a9ac5bddf15cdb44aee3c50421
3
+ metadata.gz: b6e2bc8b5fdd1bb5083a61cf73298cc0e083c613
4
+ data.tar.gz: 70dc07303896122766efcbdf44c6c13e437d6b7a
5
5
  SHA512:
6
- metadata.gz: 1be9b67e8dc3639c7c029cd4f463e436fdc46957b7b887599e043d1f5781178a7adc5e83257b9096141d4bb331dd3075c8fc921da5b5fa59109c8446544a912e
7
- data.tar.gz: d285cee2e1f6ebb30431b41f6a3c89193b6cec47c607d336d49003fd2c3933a3aabc4fe5a21654a77afa01e63f9240829d75017c0d37232f1c3ee026d0caad32
6
+ metadata.gz: 169e88526725a607eab7f3f942752e41009148596c00eb15030f32f2351020c2dcbef7fca3b6ab9c0b52128d33478d7af7ba91c223a858608f88d99c4e54870c
7
+ data.tar.gz: 450a0f3d7009384f7fd92445fa9e735b7b3b46ca695c2d4d0d5d8e456212c24ad2af329dbb910348bd26c7e0871c414a8c785c9344a3079c4f0be7fdca491a4f
data/Gemfile.lock CHANGED
@@ -1,9 +1,10 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- a_series_of_tubes (1.0.1)
4
+ a_series_of_tubes (1.1.0)
5
5
  json
6
6
  rack
7
+ sqlite3
7
8
 
8
9
  GEM
9
10
  remote: https://rubygems.org/
@@ -25,6 +26,7 @@ GEM
25
26
  diff-lcs (>= 1.2.0, < 2.0)
26
27
  rspec-support (~> 3.4.0)
27
28
  rspec-support (3.4.1)
29
+ sqlite3 (1.3.11)
28
30
 
29
31
  PLATFORMS
30
32
  ruby
@@ -23,4 +23,5 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency "rspec"
24
24
  spec.add_runtime_dependency "rack"
25
25
  spec.add_runtime_dependency "json"
26
+ spec.add_runtime_dependency "sqlite3"
26
27
  end
@@ -3,6 +3,6 @@ require_relative './a_series_of_tubes/tubes.rb'
3
3
  require_relative './a_series_of_tubes/tube_controller.rb'
4
4
  require_relative './a_series_of_tubes/tube_state.rb'
5
5
  require_relative './a_series_of_tubes/tube_support.rb'
6
-
6
+ require_relative './a_series_of_tubes/tube_record.rb'
7
7
  module ASeriesOfTubes
8
8
  end
@@ -0,0 +1,7 @@
1
+ require_relative './tube_record/sql_object'
2
+ require_relative './tube_record/db_connection'
3
+
4
+ module ASeriesOfTubes
5
+ module TubeRecord
6
+ end
7
+ end
@@ -0,0 +1,56 @@
1
+ require 'sqlite3'
2
+
3
+ module ASeriesOfTubes
4
+ module TubeRecord
5
+ class DBConnection
6
+ def self.open(db_file_name)
7
+ @db = SQLite3::Database.new(db_file_name)
8
+ @db.results_as_hash = true
9
+ @db.type_translation = true
10
+
11
+ @db
12
+ end
13
+
14
+ def self.reset
15
+ commands = [
16
+ "rm '#{TUBE_DB_FILE}'",
17
+ "cat '#{TUBE_SQL_FILE}' | sqlite3 '#{TUBE_DB_FILE}'"
18
+ ]
19
+
20
+ commands.each { |command| `#{command}` }
21
+ DBConnection.open(TUBE_DB_FILE)
22
+ end
23
+
24
+ def self.instance
25
+ reset if @db.nil?
26
+
27
+ @db
28
+ end
29
+
30
+ def self.execute(*args)
31
+ print_query(*args)
32
+ instance.execute(*args)
33
+ end
34
+
35
+ def self.execute2(*args)
36
+ print_query(*args)
37
+ instance.execute2(*args)
38
+ end
39
+
40
+ def self.last_insert_row_id
41
+ instance.last_insert_row_id
42
+ end
43
+
44
+ private
45
+
46
+ def self.print_query(query, *interpolation_args)
47
+ puts '--------------------'
48
+ puts query
49
+ unless interpolation_args.empty?
50
+ puts "interpolate: #{interpolation_args.inspect}"
51
+ end
52
+ puts '--------------------'
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,109 @@
1
+ require 'byebug'
2
+
3
+ module ASeriesOfTubes
4
+ module TubeRecord
5
+ class SQLObject
6
+ def self.columns
7
+ @columns ||= DBConnection.execute2(<<-SQL).first.map(&:to_sym)
8
+ SELECT
9
+ *
10
+ FROM
11
+ #{self.table_name}
12
+ SQL
13
+ end
14
+
15
+ def self.all
16
+ self.parse_all(DBConnection.execute(<<-SQL))
17
+ SELECT
18
+ *
19
+ FROM
20
+ #{self.table_name}
21
+ SQL
22
+ end
23
+
24
+ def self.parse_all(results)
25
+ results.map { |params| self.new(params) }
26
+ end
27
+
28
+ def self.find(id)
29
+ result = DBConnection.execute(<<-SQL, id: id).first
30
+ SELECT
31
+ *
32
+ FROM
33
+ #{self.table_name}
34
+ WHERE
35
+ #{self.table_name}.id = :id
36
+ LIMIT
37
+ 1
38
+ SQL
39
+
40
+ self.new(result) if result
41
+ end
42
+
43
+ def self.finalize!
44
+ self.columns.each do |column|
45
+ define_method(column) { self.attributes[column] }
46
+ define_method("#{column}=") { |value| self.attributes[column] = value }
47
+ end
48
+ end
49
+
50
+ def self.table_name
51
+ @table_name ||= self.to_s.tableize
52
+ end
53
+
54
+ def self.table_name=(table_name)
55
+ @table_name = table_name
56
+ end
57
+
58
+ def initialize(params = {})
59
+ params.each do |attr_name, value|
60
+ if self.class.columns.include?(attr_name.to_sym)
61
+ self.send("#{attr_name}=", value)
62
+ else
63
+ raise "unknown attribute '#{attr_name}'"
64
+ end
65
+ end
66
+ end
67
+
68
+ def attributes
69
+ @attributes ||= {}
70
+ end
71
+
72
+ def attribute_values
73
+ @attributes.keys.map { |k| @attributes[k] }
74
+ end
75
+
76
+ def insert
77
+ columns = self.class.columns.drop(1)
78
+ question_marks = (['?'] * columns.length)
79
+
80
+ ASeriesOfTubes::TubeRecord::DBConnection.execute(<<-SQL, *attribute_values)
81
+ INSERT INTO
82
+ #{self.class.table_name} (#{columns.join(',')})
83
+ VALUES
84
+ (#{question_marks.join(',')})
85
+ SQL
86
+
87
+ self.id = ASeriesOfTubes::TubeRecord::DBConnection.last_insert_row_id
88
+ end
89
+
90
+ def update
91
+ columns = self.class.columns
92
+ set_values = columns.map { |attr_name| "#{attr_name} = ?" }
93
+
94
+ ASeriesOfTubes::TubeRecord::DBConnection.execute(<<-SQL, *attribute_values)
95
+ UPDATE
96
+ #{self.class.table_name}
97
+ SET
98
+ #{set_values.join(',')}
99
+ WHERE
100
+ id = #{self.id}
101
+ SQL
102
+ end
103
+
104
+ def save
105
+ self.id ? self.update : self.insert
106
+ end
107
+ end
108
+ end
109
+ end
@@ -2,6 +2,10 @@ module ASeriesOfTubes
2
2
  module TubeSupport
3
3
  module CoreExtensions
4
4
  module String
5
+ def tableize
6
+ return underscore + 's'
7
+ end
8
+
5
9
  def underscore
6
10
  output = []
7
11
 
@@ -1,3 +1,3 @@
1
1
  module ASeriesOfTubes
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.0"
3
3
  end
Binary file
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: a_series_of_tubes
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Phillips
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-04-16 00:00:00.000000000 Z
11
+ date: 2016-04-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: sqlite3
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  description:
84
98
  email:
85
99
  - dan@danphillips.io
@@ -104,6 +118,9 @@ files:
104
118
  - bin/setup
105
119
  - lib/a_series_of_tubes.rb
106
120
  - lib/a_series_of_tubes/tube_controller.rb
121
+ - lib/a_series_of_tubes/tube_record.rb
122
+ - lib/a_series_of_tubes/tube_record/db_connection.rb
123
+ - lib/a_series_of_tubes/tube_record/sql_object.rb
107
124
  - lib/a_series_of_tubes/tube_state.rb
108
125
  - lib/a_series_of_tubes/tube_state/flash.rb
109
126
  - lib/a_series_of_tubes/tube_state/session.rb
@@ -114,6 +131,7 @@ files:
114
131
  - lib/a_series_of_tubes/tubes/tuber.rb
115
132
  - lib/a_series_of_tubes/version.rb
116
133
  - pkg/a_series_of_tubes-1.0.0.gem
134
+ - pkg/a_series_of_tubes-1.0.1.gem
117
135
  homepage: http://www.github.com/danmakenoise/a_series_of_tubes
118
136
  licenses:
119
137
  - MIT