tango_orm 0.1.2

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c7a6774168186558d48d9fb9d61b6f41fbce5370691dc902d8ba019e0d3bf3ee
4
+ data.tar.gz: 73b068490eab67b6de5b4c5c808aa26e563766d49e5529d07e6b26ed1a484079
5
+ SHA512:
6
+ metadata.gz: a0926dda1e37b030c5497a82e52bde2e93a8f248f6b1fe905f12c77b57874add3c27846a6c74dab3aebf40aa4f40626acc7485b5948da278b17a99c950435643
7
+ data.tar.gz: 448f3bd2d2e74349dfce507842f9656232eb60210c8d7c8072088ecc03ecbe84d11f540ed5545a5787d364f38bb76590465c13b0edd81149ff2d3e8c93c9feaf
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.2
7
+ before_install: gem install bundler -v 2.0.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in tango_orm.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Memuna Haruna
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.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # TangoOrm
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/tango_orm`. 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 'tango_orm'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install tango_orm
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. Then, run `rake spec` to run the tests. 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/[USERNAME]/tango_orm.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "tango_orm"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,17 @@
1
+ require 'yaml'
2
+ require 'pg'
3
+ require "tango_orm/environment"
4
+
5
+ module TangoOrm
6
+ class DBConnection
7
+ def self.set_connection
8
+ PG.connect(dbname: config['database'])
9
+ end
10
+
11
+ private
12
+
13
+ def self.config
14
+ @@config ||= YAML.load(File.read("config/database.yml"))[ENVIRONMENT]
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module TangoOrm
2
+ ENVIRONMENT = "development" # temporary default
3
+ end
@@ -0,0 +1,167 @@
1
+ require "active_support/inflector"
2
+ require "tango_orm/db_connection"
3
+
4
+ module TangoOrm
5
+ module Model
6
+ class Base
7
+ def self.table_name
8
+ self.name.downcase.pluralize
9
+ end
10
+
11
+ def self.db
12
+ @@db ||= establish_connection
13
+ end
14
+
15
+ def self.establish_connection
16
+ DBConnection.set_connection
17
+ end
18
+
19
+ def self.columns
20
+ sql = <<-SQL
21
+ SELECT *
22
+ FROM information_schema.columns
23
+ WHERE table_name = '#{table_name}';
24
+ SQL
25
+ names = db.exec(sql)
26
+ names.map{|name| name["column_name"].to_sym}
27
+ end
28
+
29
+ def initialize(options = {})
30
+ options = options.merge(id: nil)
31
+ options.each {|name, value| instance_variable_set("@#{name}", value)}
32
+ end
33
+
34
+ def method_missing(method_name, *arguments, &block)
35
+ attribute_getters = self.class.columns
36
+ attribute_setters = attribute_getters.map{|getter| "#{getter}=".to_sym}
37
+
38
+ if attribute_getters.include?(method_name)
39
+ instance_variable_get("@#{method_name}")
40
+ elsif attribute_setters.include?(method_name)
41
+ name = method_name.to_s.delete("=").to_sym
42
+ instance_variable_set("@#{name}", arguments[0])
43
+ else
44
+ super
45
+ end
46
+ end
47
+
48
+ def respond_to_missing?(method_name, include_private = false)
49
+ getters = self.class.columns
50
+ setters = getters.map{|getter| "#{getter}=".to_sym}
51
+
52
+ getters.include?(method_name) || setters.include?(method_name) || super
53
+ end
54
+
55
+ def self.create_table(options)
56
+ formatted_options = {}
57
+ options.each do |key, value|
58
+ formatted_options[key] = value.upcase.gsub(/[,_]/, " ")
59
+ end
60
+
61
+ column_config = ""
62
+ formatted_options.each do |key, value|
63
+ line = "#{key.to_s} #{value},"
64
+ column_config << line
65
+ end
66
+
67
+ column_config = column_config.chomp(", ")
68
+
69
+ sql = <<-SQL
70
+ CREATE TABLE #{table_name} (
71
+ id SERIAL PRIMARY KEY,
72
+ #{column_config}
73
+ );
74
+ SQL
75
+
76
+ db.exec(sql)
77
+ puts "Table '#{table_name}' created successfully"
78
+ end
79
+
80
+ def self.drop_table
81
+ sql = "DROP TABLE #{table_name}"
82
+ db.exec(sql)
83
+ puts "Table '#{table_name}' dropped successfully"
84
+ end
85
+
86
+ def save
87
+ db = self.class.db
88
+ db_table = self.class.table_name
89
+ variable_names = attributes.join(", ")
90
+ variable_numbers = (1..attributes.count).map{|i| "$#{i}" }.join(", ")
91
+ variable_values = attributes.map{|var| send(var)}
92
+
93
+ sql = <<~SQL
94
+ INSERT INTO #{db_table} (#{variable_names})
95
+ VALUES (#{variable_numbers})
96
+ RETURNING id;
97
+ SQL
98
+
99
+ db.exec(sql, variable_values) do |result|
100
+ self.id = result[0]["id"].to_i
101
+ end
102
+ self
103
+ end
104
+
105
+ def self.create(options)
106
+ new_song = new(options)
107
+ new_song.save
108
+ end
109
+
110
+ def update
111
+ db = self.class.db
112
+ db_table = self.class.table_name
113
+ numbered_var_names = attributes.map.with_index {|attr, i| "#{attr} = $#{i + 1}"}.join(", ")
114
+ variable_values = attributes.map{|var| send(var)} + [self.id]
115
+ id_value = "$#{variable_values.count}"
116
+
117
+ sql = <<~SQL
118
+ UPDATE #{db_table}
119
+ SET #{numbered_var_names}
120
+ WHERE id = #{id_value}
121
+ RETURNING *;
122
+ SQL
123
+
124
+ db.exec(sql, variable_values) do |result|
125
+ self.class.new_from_db(result[0])
126
+ end
127
+ end
128
+
129
+ def self.find_by_id(id)
130
+ sql = "SELECT * FROM #{table_name} WHERE id = $1"
131
+
132
+ db.exec(sql, [id]) do |result|
133
+ result.map do |row|
134
+ new_from_db(row)
135
+ end
136
+ end.first
137
+ end
138
+
139
+ def self.all
140
+ db.exec("SELECT * FROM #{table_name}") do |result|
141
+ result.map do |row|
142
+ new_from_db(row)
143
+ end
144
+ end
145
+ end
146
+
147
+ private
148
+
149
+ def attributes
150
+ instance_variables.map{ |var| var.to_s.gsub("@", "") } - ["id"]
151
+ end
152
+
153
+ def self.new_from_db(row)
154
+ instance = self.new # self.new is the same as running Model.new
155
+
156
+ row.each do |name, value|
157
+ if name == "id"
158
+ instance.instance_variable_set("@#{name}", value.to_i)
159
+ else
160
+ instance.instance_variable_set("@#{name}", value)
161
+ end
162
+ end
163
+ instance # return the newly created instance
164
+ end
165
+ end
166
+ end
167
+ end
@@ -0,0 +1,3 @@
1
+ module TangoOrm
2
+ VERSION = "0.1.2"
3
+ end
data/lib/tango_orm.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "tango_orm/version"
2
+ require "tango_orm/model/base"
3
+
4
+ module TangoOrm
5
+ class Error < StandardError; end
6
+ # Your code goes here...
7
+ end
data/tango_orm.gemspec ADDED
@@ -0,0 +1,45 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "tango_orm/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tango_orm"
8
+ spec.version = TangoOrm::VERSION
9
+ spec.authors = ["Memuna Haruna"]
10
+ spec.email = ["mmharuna16@gmail.com"]
11
+
12
+ spec.summary = "Simple Ruby ORM"
13
+ spec.homepage = "https://github.com/MemunaHaruna/tango_orm"
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ # if spec.respond_to?(:metadata)
19
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
20
+
21
+ # spec.metadata["homepage_uri"] = spec.homepage
22
+ # spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
23
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
24
+ # else
25
+ # raise "RubyGems 2.0 or newer is required to protect against " \
26
+ # "public gem pushes."
27
+ # end
28
+
29
+ # Specify which files should be added to the gem when it is released.
30
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
31
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
32
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
33
+ end
34
+ spec.bindir = "exe"
35
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
36
+ spec.require_paths = ["lib"]
37
+
38
+ spec.add_development_dependency "bundler", "~> 2.0"
39
+ spec.add_development_dependency "rake", "~> 10.0"
40
+ spec.add_development_dependency "rspec", "~> 3.0"
41
+ spec.add_development_dependency "pry-byebug", "~> 3"
42
+
43
+ spec.add_runtime_dependency "activesupport", '~> 5.0', '>= 5.0.0.1'
44
+ spec.add_runtime_dependency "pg", '~> 1.0'
45
+ end
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tango_orm
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Memuna Haruna
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-01-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry-byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '5.0'
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: 5.0.0.1
79
+ type: :runtime
80
+ prerelease: false
81
+ version_requirements: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - "~>"
84
+ - !ruby/object:Gem::Version
85
+ version: '5.0'
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: 5.0.0.1
89
+ - !ruby/object:Gem::Dependency
90
+ name: pg
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '1.0'
96
+ type: :runtime
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '1.0'
103
+ description:
104
+ email:
105
+ - mmharuna16@gmail.com
106
+ executables: []
107
+ extensions: []
108
+ extra_rdoc_files: []
109
+ files:
110
+ - ".gitignore"
111
+ - ".rspec"
112
+ - ".travis.yml"
113
+ - Gemfile
114
+ - LICENSE.txt
115
+ - README.md
116
+ - Rakefile
117
+ - bin/console
118
+ - bin/setup
119
+ - lib/tango_orm.rb
120
+ - lib/tango_orm/db_connection.rb
121
+ - lib/tango_orm/environment.rb
122
+ - lib/tango_orm/model/base.rb
123
+ - lib/tango_orm/version.rb
124
+ - tango_orm.gemspec
125
+ homepage: https://github.com/MemunaHaruna/tango_orm
126
+ licenses:
127
+ - MIT
128
+ metadata: {}
129
+ post_install_message:
130
+ rdoc_options: []
131
+ require_paths:
132
+ - lib
133
+ required_ruby_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ required_rubygems_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ requirements: []
144
+ rubygems_version: 3.0.3
145
+ signing_key:
146
+ specification_version: 4
147
+ summary: Simple Ruby ORM
148
+ test_files: []