bartleby 0.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 43c95ef066fcbecd5a6c44b783dae6d3f8aa0e00
4
+ data.tar.gz: 0b26817fc8b915b7b1630320bddff8f0d4d980ee
5
+ SHA512:
6
+ metadata.gz: 62dff076cd5fbe9f6dbaf8e4e64ef6f70b9a4a10857a93f360496904c41a2965a5fd228e267c22f7ab722ca3fa16c769fcfb308162c816e84e180322eb1ff05f
7
+ data.tar.gz: 22e07c88f3ff4f89a27ee19dabf0460ada24f368ddbb62078146e9c55c02676eb98f5794c0c7ef28f1cbe14c1baf196e731d56f802b7ae7a90a4865667bbef2e
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Sam Greenlee
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,40 @@
1
+ # Bartleby
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/bartleby`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'bartleby'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install bartleby
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/sgreenlee/bartleby.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,17 @@
1
+ require "bartleby/configuration"
2
+
3
+ module Bartleby
4
+ class << self
5
+ attr_accessor :configuration
6
+ end
7
+
8
+ def self.configuration
9
+ @configuration ||= Configuration.new
10
+ end
11
+
12
+ def self.configure(&prc)
13
+ prc.call(configuration)
14
+ end
15
+ end
16
+
17
+ require "bartleby/objectifier"
@@ -0,0 +1,119 @@
1
+ require 'active_support/inflector'
2
+
3
+ module Bartleby
4
+
5
+ class AssocOptions
6
+ attr_accessor(
7
+ :foreign_key,
8
+ :class_name,
9
+ :primary_key
10
+ )
11
+
12
+ def model_class
13
+ class_name.constantize
14
+ end
15
+
16
+ def table_name
17
+ class_name.underscore + 's'
18
+ end
19
+ end
20
+
21
+ class BelongsToOptions < AssocOptions
22
+ def initialize(name, options = {})
23
+ defaults = {
24
+ foreign_key: "#{name}_id".to_sym,
25
+ primary_key: :id,
26
+ class_name: name.to_s.singularize.capitalize
27
+ }
28
+
29
+ self.foreign_key = options[:foreign_key] || defaults[:foreign_key]
30
+ self.primary_key = options[:primary_key] || defaults[:primary_key]
31
+ self.class_name = options[:class_name] || defaults[:class_name]
32
+ end
33
+ end
34
+
35
+ class HasManyOptions < AssocOptions
36
+ def initialize(name, self_class_name, options = {})
37
+ defaults = {
38
+ foreign_key: "#{self_class_name.underscore}_id".to_sym,
39
+ primary_key: :id,
40
+ class_name: name.to_s.singularize.capitalize
41
+ }
42
+
43
+ self.foreign_key = options[:foreign_key] || defaults[:foreign_key]
44
+ self.primary_key = options[:primary_key] || defaults[:primary_key]
45
+ self.class_name = options[:class_name] || defaults[:class_name]
46
+ end
47
+ end
48
+
49
+ module Associatable
50
+ def belongs_to(name, options = {})
51
+ options = BelongsToOptions.new(name, options)
52
+
53
+ assoc_options[name] = options
54
+
55
+ define_method(name) do
56
+ foreign_key = send(options.foreign_key)
57
+ target_class = options.model_class
58
+ conditions = {options.primary_key => foreign_key}
59
+ target_class.where(conditions).first
60
+ end
61
+ end
62
+
63
+ def has_many(name, options = {})
64
+ options = HasManyOptions.new(name, self.name, options)
65
+
66
+ define_method(name) do
67
+ target_class = options.model_class
68
+ primary_key = send(options.primary_key)
69
+ conditions = {options.foreign_key => primary_key}
70
+ target_class.where(conditions)
71
+ end
72
+ end
73
+
74
+ def has_one_through(name, through_name, source_name)
75
+
76
+ define_method(name) do
77
+ through_options = self.class.assoc_options[through_name]
78
+ source_options = through_options.model_class.assoc_options[source_name]
79
+
80
+ fk1 = through_options.foreign_key
81
+ pk1 = through_options.primary_key
82
+
83
+ fk2 = source_options.foreign_key
84
+ pk2 = source_options.primary_key
85
+
86
+ t1 = self.class.table_name
87
+ t2 = through_options.table_name
88
+ t3 = source_options.table_name
89
+
90
+ first_join_conditions = "#{t1}.#{fk1} = #{t2}.#{pk1}"
91
+ second_join_conditions = "#{t2}.#{fk2} = #{t3}.#{pk2}"
92
+ primary_key = through_options.primary_key
93
+
94
+ query_string = <<-SQL
95
+ SELECT
96
+ #{t3}.*
97
+ FROM
98
+ #{t1}
99
+ JOIN
100
+ #{t2}
101
+ ON
102
+ #{first_join_conditions}
103
+ JOIN
104
+ #{t3}
105
+ ON
106
+ #{second_join_conditions}
107
+ WHERE
108
+ #{t1}.#{primary_key} = ?
109
+ SQL
110
+ result = Connection.execute(query_string, self.id)
111
+ result.empty? ? nil : source_options.model_class.new(result.first)
112
+ end
113
+ end
114
+
115
+ def assoc_options
116
+ @assoc_options ||= {}
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,15 @@
1
+ module Bartleby
2
+ class Configuration
3
+ attr_accessor :seed_file, :db_file, :print_queries
4
+
5
+ def print_queries?
6
+ !!@print_queries
7
+ end
8
+
9
+ def initialize
10
+ @seed_file = nil
11
+ @db_file = nil
12
+ @print_queries = true
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,66 @@
1
+ require 'sqlite3'
2
+
3
+ module Bartleby
4
+ class Connection
5
+ def self.open(db_file_name)
6
+ @db = SQLite3::Database.new(db_file_name)
7
+ @db.results_as_hash = true
8
+ @db.type_translation = true
9
+ @db
10
+ end
11
+
12
+ def self.reset
13
+ commands = [
14
+ "rm '#{db_file}'",
15
+ "cat '#{seed_file}' | sqlite3 '#{db_file}'"
16
+ ]
17
+ commands.each { |command| `#{command}` }
18
+ Connection.open(db_file)
19
+ end
20
+
21
+ def self.instance
22
+ reset if @db.nil?
23
+
24
+ @db
25
+ end
26
+
27
+ def self.execute(*args)
28
+ print_query(*args)
29
+ instance.execute(*args)
30
+ end
31
+
32
+ def self.execute2(*args)
33
+ print_query(*args)
34
+ instance.execute2(*args)
35
+ end
36
+
37
+ def self.last_insert_row_id
38
+ instance.last_insert_row_id
39
+ end
40
+
41
+ private
42
+
43
+ def self.print_queries?
44
+ Bartleby.configuration.print_queries?
45
+ end
46
+
47
+ def self.db_file
48
+ Bartleby.configuration.db_file
49
+ end
50
+
51
+ def self.seed_file
52
+ Bartleby.configuration.seed_file
53
+ end
54
+
55
+ def self.print_query(query, *interpolation_args)
56
+ return unless print_queries?
57
+
58
+ puts '--------------------'
59
+ puts query
60
+ unless interpolation_args.empty?
61
+ puts "interpolate: #{interpolation_args.inspect}"
62
+ end
63
+ puts '--------------------'
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,141 @@
1
+ require_relative 'connection'
2
+ require_relative 'searchable'
3
+ require_relative 'associatable'
4
+ require 'active_support/inflector'
5
+
6
+ module Bartleby
7
+ class Objectifier
8
+ extend Associatable
9
+ extend Searchable
10
+
11
+ def self.columns
12
+ return @columns.dup if @columns
13
+ # ...
14
+ @columns = Connection.execute2(<<-SQL).first.map(&:to_sym)
15
+ SELECT
16
+ *
17
+ FROM
18
+ '#{table_name}'
19
+ SQL
20
+ @columns.dup
21
+ end
22
+
23
+ def self.finalize!
24
+ columns.each do |column|
25
+ create_getter!(column)
26
+ create_setter!(column)
27
+ end
28
+ end
29
+
30
+ def self.table_name=(table_name)
31
+ @table_name = table_name
32
+ end
33
+
34
+ def self.table_name
35
+ @table_name || self.name.tableize
36
+ end
37
+
38
+ def self.all
39
+ results = Connection.execute(<<-SQL)
40
+ SELECT
41
+ *
42
+ FROM
43
+ '#{table_name}'
44
+ SQL
45
+
46
+ parse_all(results)
47
+ end
48
+
49
+ def self.parse_all(results)
50
+ results.map { |result| self.new(result) }
51
+ end
52
+
53
+ def self.find(id)
54
+ result = Connection.execute(<<-SQL, id)
55
+ SELECT
56
+ *
57
+ FROM
58
+ '#{table_name}'
59
+ WHERE
60
+ id = ?
61
+ SQL
62
+
63
+ result.empty? ? nil : self.new(result.first)
64
+ end
65
+
66
+ def initialize(params = {})
67
+ params.each do |attr_name, value|
68
+ raise "unknown attribute '#{attr_name}'" unless valid_attribute? attr_name
69
+ send("#{attr_name}=".to_sym, value)
70
+ end
71
+ end
72
+
73
+ def attributes
74
+ @attributes ||= {}
75
+ end
76
+
77
+ def attribute_values
78
+ self.class.columns.map { |col| send(col) }
79
+ end
80
+
81
+ def insert
82
+ cols = self.class.columns
83
+ cols.delete(:id)
84
+ col_names = cols.join(", ")
85
+ question_marks = (["?"] * (self.class.columns.count - 1)).join(", ")
86
+ id_idx = self.class.columns.index(:id)
87
+ values = attribute_values
88
+ values.delete_at(id_idx)
89
+
90
+ query_string = <<-SQL
91
+ INSERT INTO
92
+ #{self.class.table_name} (#{col_names})
93
+ VALUES
94
+ (#{question_marks})
95
+ SQL
96
+
97
+ Connection.execute(query_string, *values)
98
+ self.id = Connection.last_insert_row_id
99
+ end
100
+
101
+ def update
102
+ cols = self.class.columns
103
+ cols.delete(:id)
104
+ set_string = cols.map { |col| "#{col} = :#{col}"}.join(", ")
105
+
106
+ query_string = <<-SQL
107
+ UPDATE
108
+ #{self.class.table_name}
109
+ SET
110
+ #{set_string}
111
+ WHERE
112
+ id = :id
113
+ SQL
114
+
115
+ Connection.execute(query_string, attributes)
116
+ end
117
+
118
+ def save
119
+ id.nil? ? insert : update
120
+ end
121
+
122
+ private
123
+
124
+ def self.create_getter!(column)
125
+ define_method(column) do
126
+ attributes[column]
127
+ end
128
+ end
129
+
130
+ def self.create_setter!(column)!
131
+ setter_name = "#{column}=".to_sym
132
+ define_method(setter_name) do |value|
133
+ attributes[column] = value
134
+ end
135
+ end
136
+
137
+ def valid_attribute?(attr_name)
138
+ self.class.columns.include? attr_name.to_sym
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,90 @@
1
+ module Bartleby
2
+ class Relation
3
+ include Enumerable
4
+
5
+ def initialize(model_class_name)
6
+ @model_class_name = model_class_name
7
+ @cache = nil
8
+ initialize_select_field
9
+ end
10
+
11
+ def execute_query
12
+ Connection.execute(query, params)
13
+ end
14
+
15
+ def model_class
16
+ Object.constant_get(@model_class_name)
17
+ end
18
+
19
+ def query_results
20
+ parse(execute_query)
21
+ end
22
+
23
+ def query
24
+ <<-SQL
25
+ SELECT
26
+ #{generate_select_field}
27
+ FROM
28
+ #{generate_from_field}
29
+ SQL
30
+ #{generate_joins}
31
+ #{generate_where_field}
32
+ #{generate_group_by_field}
33
+ #{generate_having_field}
34
+ end
35
+
36
+ def select(options = {})
37
+
38
+ end
39
+
40
+ def select_field
41
+ @select_field ||= { model_class.table_name => :* }
42
+ end
43
+
44
+ def generate_select_field
45
+ select = []
46
+ select_field.each { |table, column| select.push "#{table}.#{column}"}
47
+ select.join(", ")
48
+ end
49
+
50
+ def from_field
51
+ @from_field ||= { model_class.table_name }
52
+ end
53
+
54
+ def where_field
55
+ @where_field ||= {}
56
+ end
57
+
58
+ def generate_select_field
59
+ where = []
60
+ where_field.each { |table, column| where.push "#{table}.#{column}"}
61
+ select.join(", ")
62
+ end
63
+
64
+ def having_field
65
+ @having_field ||= {}
66
+ end
67
+
68
+ def params
69
+ @params ||= {}
70
+ end
71
+
72
+ def [](index)
73
+ load if @cache.nil?
74
+ @cache[index]
75
+ end
76
+
77
+ def each(&prc)
78
+ load if @cache.nil?
79
+ @cache.each(&prc)
80
+ end
81
+
82
+ def load
83
+ @cache = query_results
84
+ end
85
+
86
+ def parse(results)
87
+ results.map { |result| model_class.new(result) }
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,19 @@
1
+ module Bartleby
2
+ module Searchable
3
+ def where(params)
4
+ param_string = params.keys.map { |name| "#{name} = :#{name}" }.join(" AND ")
5
+
6
+ query_string = <<-SQL
7
+ SELECT
8
+ *
9
+ FROM
10
+ #{table_name}
11
+ WHERE
12
+ #{param_string}
13
+ SQL
14
+
15
+ results = Connection.execute(query_string, params)
16
+ parse_all(results)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module Bartleby
2
+ VERSION = "0.0.0"
3
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bartleby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Sam Greenlee
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-06-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.11'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.11'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 3.1.0
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: 3.1.0
79
+ type: :development
80
+ prerelease: false
81
+ version_requirements: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - "~>"
84
+ - !ruby/object:Gem::Version
85
+ version: 3.1.0
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: 3.1.0
89
+ - !ruby/object:Gem::Dependency
90
+ name: byebug
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ description:
104
+ email:
105
+ - sam.a.greenlee@gmail.com
106
+ executables: []
107
+ extensions: []
108
+ extra_rdoc_files: []
109
+ files:
110
+ - LICENSE.txt
111
+ - README.md
112
+ - lib/bartleby.rb
113
+ - lib/bartleby/associatable.rb
114
+ - lib/bartleby/configuration.rb
115
+ - lib/bartleby/connection.rb
116
+ - lib/bartleby/objectifier.rb
117
+ - lib/bartleby/relation.rb
118
+ - lib/bartleby/searchable.rb
119
+ - lib/bartleby/version.rb
120
+ homepage: https://github.com/sgreenlee/bartleby
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.6.2
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: A lightweight ORM.
144
+ test_files: []
145
+ has_rdoc: