monorm 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +89 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/monorm/adapters/pg_connection.rb +59 -0
- data/lib/monorm/adapters/sqlite_connection.rb +66 -0
- data/lib/monorm/associatable.rb +107 -0
- data/lib/monorm/base.rb +140 -0
- data/lib/monorm/searchable.rb +23 -0
- data/lib/monorm/version.rb +3 -0
- data/lib/monorm.rb +57 -0
- data/monorm.gemspec +44 -0
- metadata +150 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d7664f3cda4ded213163e60352cc72779a217c85
|
4
|
+
data.tar.gz: 5360e29578d4474a74365697ae4ea1e169aee84a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: df145d1b83b37bcefb642ef344a8ad7902024b3a17f7fa85249fa0a45bfdffa9a6a8b659442a488e88974493fad61e845ee598a1c48368d3c568e284e74f0e2b
|
7
|
+
data.tar.gz: cf5119a729ce3531438c72bddf782a1a0d6a345b939a649ca09944263289f99e251b60476d0b074270969afd628382420eaf7cd9efea400446baa956c3480978
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at michaelc962@yahoo.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Michael Chilton
|
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,89 @@
|
|
1
|
+
# MonoRM
|
2
|
+
|
3
|
+
MonoRM is a lightweight Object Relational Mapping (ORM) library that facilitates the creation and
|
4
|
+
usage of Ruby objects from data retrieved from persistent storage, as well as the insertion and manipulation
|
5
|
+
of that data into persistent storage. When a new model class extends the 'Base' class, the methods are provided
|
6
|
+
allowing for interaction with the chosen database.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'monorm'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install monorm
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
A small sample project that can be used to test MonoRM can be found here (coming soon).
|
27
|
+
|
28
|
+
Relevant OS database drivers must be set up by user in order for gem and dependencies to install.
|
29
|
+
|
30
|
+
MonoRM was designed to work with several configuration files to allow for user customization.
|
31
|
+
|
32
|
+
|
33
|
+
The following files are required:
|
34
|
+
- config folder
|
35
|
+
- database.yml - This file contains relevant configuration for the database
|
36
|
+
- boot.rb - This file (name not enforced) should call `MonoRM::DBInitializer.load_db_adapter`, which loads the appropriate database adapter file in the gem.
|
37
|
+
- model_loader.rb - This file (optional, but recommended), that loads all the models in the model directory that will be used in the application.
|
38
|
+
- bin folder
|
39
|
+
- There should be a file that requires `bundler/setup` and your choice of Ruby console to interact with the application, as well as the boot file from the config folder. Be sure to run the initialization methods from boot before startup, and then start the console
|
40
|
+
- db folder (for sqlite databases)
|
41
|
+
- sqlite_db folder
|
42
|
+
- `{database_name}.db` - A database file, with name chosen by the user, is required by sqlite to function. Maintain this folder structure and enter the name (without .db extension) in the `database.yml` file under the _database_ key
|
43
|
+
|
44
|
+
The following files are recommended:
|
45
|
+
- db folder
|
46
|
+
- `sqlite_setup.rb` - This is a setup script to setup a sqlite database. The recommended structure is to take a command line argument for the database name, and then run the setup. The setup will need a .db database file name path that will place the database file in the sqlite_db folder. A .sql file written for sqlite3 located in the sqlite_sql to seed the database with is also recommended. The following commands can then be run: `dropdb`, `createdb`, and `sqlite3 path_to_db_name < path_to_sql_file` to recreate the database to the specifications of the sqlite sql file.
|
47
|
+
- `pg_setup.rb` - This is a setup script to setup a postgres database. The recommended structure is similar to sqlite, but no db folder/file is needed, just a .sql file written for a postgreSQL database,
|
48
|
+
with the command `psql database_name < path_to_sql_file`.
|
49
|
+
- sqlite_sql folder
|
50
|
+
- db_name.sql - This is a .sql file named for the database to seed containing the relevant sql commands to seed the database, written for a sqlite3 database.
|
51
|
+
- pg_sql folder
|
52
|
+
- db_name.sql - This is a .sql file named for the database to seed containing the relevant sql commands to seed the database, written for a postgreSQL database.
|
53
|
+
|
54
|
+
- models folder
|
55
|
+
- This folder should contain any models that will be used to interact with the database, where each model should inherit from the `MonoRM::Base` class
|
56
|
+
|
57
|
+
## Database Support
|
58
|
+
|
59
|
+
Currently there is support for PostgreSQL and SQLite3 databases. Support for additional databases
|
60
|
+
may be added in the future.
|
61
|
+
|
62
|
+
## Development
|
63
|
+
|
64
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests (not yet written). You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
65
|
+
|
66
|
+
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).
|
67
|
+
|
68
|
+
## TODO
|
69
|
+
|
70
|
+
- Documentation and code snippets for major features
|
71
|
+
- Write RSpec tests
|
72
|
+
- Support for additional database query methods, including `joins`
|
73
|
+
- Make `where` lazy and stackable
|
74
|
+
- Implement Relation class
|
75
|
+
- Implement validation methods
|
76
|
+
- Implement `has_many, :through`
|
77
|
+
- Prefetching using `includes`
|
78
|
+
- Make convenience utility methods such as `first` and `last`
|
79
|
+
- Add support for `ENV['DATABASE_URL']`
|
80
|
+
- Write additional database adapters, such as MySQL
|
81
|
+
|
82
|
+
## Contributing
|
83
|
+
|
84
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/monorm. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
85
|
+
|
86
|
+
|
87
|
+
## License
|
88
|
+
|
89
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "monorm"
|
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 "pry"
|
14
|
+
Pry.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'pg'
|
2
|
+
|
3
|
+
class MonoRM::DBConnection
|
4
|
+
|
5
|
+
def self.open
|
6
|
+
# uri = URI.parse(ENV['DATABASE_URL'])
|
7
|
+
@conn = PG::Connection.new(
|
8
|
+
dbname: MonoRM::DB_CONFIG['default']['database']
|
9
|
+
# user: uri.user,
|
10
|
+
# password: uri.password,
|
11
|
+
# host: uri.host,
|
12
|
+
# port: uri.port,
|
13
|
+
)
|
14
|
+
|
15
|
+
@conn
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.instance
|
19
|
+
MonoRM::DBConnection.open if @conn.nil?
|
20
|
+
|
21
|
+
@conn
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.execute(*args)
|
25
|
+
sql_statement = args[0]
|
26
|
+
|
27
|
+
args_counter = 1
|
28
|
+
|
29
|
+
should_return_id = false
|
30
|
+
|
31
|
+
interpolated_sql_statement_elements = sql_statement.split(' ').map do |arg|
|
32
|
+
should_return_id = true if /\bINSERT\b/.match(arg)
|
33
|
+
if /\bINTERPOLATOR_MARK\b/.match(arg)
|
34
|
+
interpolated_arg = arg.gsub(/\bINTERPOLATOR_MARK\b/, "$#{args_counter}")
|
35
|
+
args_counter += 1
|
36
|
+
interpolated_arg
|
37
|
+
else
|
38
|
+
arg
|
39
|
+
end
|
40
|
+
end
|
41
|
+
interpolated_sql_statement = interpolated_sql_statement_elements.join(' ')
|
42
|
+
|
43
|
+
args[0] = interpolated_sql_statement
|
44
|
+
interpolated_args = args.slice(1..-1)
|
45
|
+
interpolated_sql_statement << ' RETURNING id' if should_return_id
|
46
|
+
@returned_id = instance.exec(interpolated_sql_statement, interpolated_args)
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.cols_exec(*args)
|
50
|
+
args = args.join("\n")
|
51
|
+
|
52
|
+
instance.exec(args)[0].keys
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.last_insert_row_id
|
56
|
+
@returned_id
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'sqlite3'
|
2
|
+
|
3
|
+
|
4
|
+
PRINT_QUERIES = ENV['PRINT_QUERIES'] == 'true'
|
5
|
+
|
6
|
+
|
7
|
+
|
8
|
+
class MonoRM::DBConnection
|
9
|
+
|
10
|
+
|
11
|
+
def self.open
|
12
|
+
MonoRM::DBConnection.load_db_path unless @db_path
|
13
|
+
|
14
|
+
@db = SQLite3::Database.new(@db_path)
|
15
|
+
@db.results_as_hash = true
|
16
|
+
@db.type_translation = true
|
17
|
+
|
18
|
+
@db
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.instance
|
22
|
+
MonoRM::DBConnection.open if @db.nil?
|
23
|
+
|
24
|
+
@db
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.execute(*args)
|
28
|
+
print_query(*args)
|
29
|
+
|
30
|
+
interpolated_sql_statement = args[0].gsub(/\bINTERPOLATOR_MARK\b/, '?')
|
31
|
+
|
32
|
+
interpolated_args = args.slice(1..-1)
|
33
|
+
|
34
|
+
|
35
|
+
instance.execute(interpolated_sql_statement, interpolated_args)
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.cols_exec(*args)
|
39
|
+
print_query(*args)
|
40
|
+
|
41
|
+
instance.execute2(*args)[0]
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.last_insert_row_id
|
45
|
+
instance.last_insert_row_id
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.load_db_path
|
49
|
+
dir = File.dirname(__FILE__)
|
50
|
+
db_name = "#{MonoRM::DB_CONFIG['default']['database']}.db"
|
51
|
+
@db_path = File.join(MonoRM::PROJECT_ROOT_DIR, "db", "sqlite_db", "#{db_name}")
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def self.print_query(query, *interpolation_args)
|
57
|
+
return unless PRINT_QUERIES
|
58
|
+
|
59
|
+
puts '--------------------'
|
60
|
+
puts query
|
61
|
+
unless interpolation_args.empty?
|
62
|
+
puts "interpolate: #{interpolation_args.inspect}"
|
63
|
+
end
|
64
|
+
puts '--------------------'
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
class MonoRM::AssocOptions
|
2
|
+
attr_accessor(
|
3
|
+
:foreign_key,
|
4
|
+
:class_name,
|
5
|
+
:primary_key
|
6
|
+
)
|
7
|
+
|
8
|
+
def model_class
|
9
|
+
self.class_name.constantize
|
10
|
+
end
|
11
|
+
|
12
|
+
def table_name
|
13
|
+
model_class.table_name
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class MonoRM::BelongsToOptions < MonoRM::AssocOptions
|
18
|
+
def initialize(name, options = {})
|
19
|
+
@foreign_key = options[:foreign_key] || :"#{name.to_s}_id"
|
20
|
+
@class_name = options[:class_name] || name.to_s.camelcase.singularize
|
21
|
+
@primary_key = options[:primary_key] || :id
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class MonoRM::HasManyOptions < MonoRM::AssocOptions
|
26
|
+
def initialize(name, self_class_name, options = {})
|
27
|
+
@foreign_key = options[:foreign_key] || "#{self_class_name.underscore.singularize}_id".to_sym
|
28
|
+
|
29
|
+
|
30
|
+
@class_name = options[:class_name] || name.to_s.camelcase.singularize
|
31
|
+
@primary_key = options[:primary_key] || :id
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
module Associatable
|
37
|
+
def belongs_to(name, options = {})
|
38
|
+
self.assoc_options[name] = MonoRM::BelongsToOptions.new(name, options)
|
39
|
+
define_method(name) do
|
40
|
+
belongs_options = self.class.assoc_options[name]
|
41
|
+
foreign_key = send(belongs_options.foreign_key)
|
42
|
+
model_class = belongs_options.class_name.constantize
|
43
|
+
|
44
|
+
model_class.where(belongs_options.primary_key => foreign_key).first
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def has_many(name, options = {})
|
49
|
+
|
50
|
+
self.assoc_options[name] = MonoRM::HasManyOptions.new(name, self.name, options)
|
51
|
+
|
52
|
+
define_method(name) do
|
53
|
+
has_many_options = self.class.assoc_options[name]
|
54
|
+
primary_key = send(has_many_options.primary_key)
|
55
|
+
model_class = has_many_options.class_name.constantize
|
56
|
+
|
57
|
+
model_class.where(has_many_options.foreign_key => primary_key)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
def assoc_options
|
63
|
+
@assoc_options ||= {}
|
64
|
+
@assoc_options
|
65
|
+
end
|
66
|
+
|
67
|
+
def has_one_through(name, through_name, source_name)
|
68
|
+
|
69
|
+
|
70
|
+
define_method(name) do
|
71
|
+
through_options = self.class.assoc_options[through_name]
|
72
|
+
source_options = through_options.model_class.assoc_options[source_name]
|
73
|
+
|
74
|
+
through_options_table = through_options.table_name
|
75
|
+
through_options_foreign_key = through_options.foreign_key
|
76
|
+
through_options_primary_key = through_options.primary_key
|
77
|
+
|
78
|
+
source_options_table = source_options.table_name
|
79
|
+
source_options_foreign_key = source_options.foreign_key
|
80
|
+
source_options_primary_key = source_options.primary_key
|
81
|
+
|
82
|
+
|
83
|
+
search_key = self.send(through_options_foreign_key)
|
84
|
+
has_one_results = MonoRM::DBConnection.execute(<<-SQL, search_key)
|
85
|
+
SELECT
|
86
|
+
#{source_options_table}.*
|
87
|
+
FROM
|
88
|
+
#{through_options_table}
|
89
|
+
JOIN
|
90
|
+
#{source_options_table}
|
91
|
+
ON
|
92
|
+
#{source_options_table}.#{source_options_primary_key} = #{through_options_table}.#{source_options_foreign_key}
|
93
|
+
|
94
|
+
WHERE
|
95
|
+
#{through_options_table}.#{through_options_primary_key} = INTERPOLATOR_MARK
|
96
|
+
|
97
|
+
SQL
|
98
|
+
|
99
|
+
source_options.model_class.parse_all(has_one_results).first
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
104
|
+
|
105
|
+
class MonoRM::Base
|
106
|
+
extend Associatable
|
107
|
+
end
|
data/lib/monorm/base.rb
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
require 'active_support/inflector'
|
2
|
+
|
3
|
+
require 'monorm/associatable'
|
4
|
+
require 'monorm/searchable'
|
5
|
+
|
6
|
+
class MonoRM::Base
|
7
|
+
|
8
|
+
def self.columns
|
9
|
+
@cols ||= MonoRM::DBConnection.cols_exec(<<-SQL)
|
10
|
+
SELECT
|
11
|
+
*
|
12
|
+
FROM
|
13
|
+
#{self.table_name}
|
14
|
+
LIMIT
|
15
|
+
1
|
16
|
+
SQL
|
17
|
+
|
18
|
+
@cols.map{|col| col.to_sym}
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.finalize!
|
23
|
+
self.columns.each do |col|
|
24
|
+
define_method(:"#{col}=") { |arg| self.attributes[col] = arg }
|
25
|
+
define_method(:"#{col}") {self.attributes[col]}
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.table_name=(table_name = nil)
|
31
|
+
@table_name = table_name
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.table_name
|
35
|
+
return @table_name unless @table_name.nil?
|
36
|
+
@table_name = self.to_s.tableize
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.all
|
40
|
+
data = MonoRM::DBConnection.execute(<<-SQL)
|
41
|
+
SELECT
|
42
|
+
*
|
43
|
+
FROM
|
44
|
+
#{self.table_name}
|
45
|
+
SQL
|
46
|
+
parse_all(data)
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.parse_all(results)
|
50
|
+
results.map do |result|
|
51
|
+
self.new(result)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.find(id)
|
56
|
+
record = MonoRM::DBConnection.execute(<<-SQL, id)
|
57
|
+
SELECT
|
58
|
+
*
|
59
|
+
FROM
|
60
|
+
#{self.table_name}
|
61
|
+
WHERE
|
62
|
+
id = INTERPOLATOR_MARK
|
63
|
+
SQL
|
64
|
+
self.parse_all(record).first
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
def initialize(params = {})
|
69
|
+
|
70
|
+
params.each do |attr_name, val|
|
71
|
+
raise "unknown attribute '#{attr_name}'" unless self.class.columns.include?(attr_name.to_sym)
|
72
|
+
send(:"#{attr_name}=", val)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def attributes
|
77
|
+
@attributes ||= {}
|
78
|
+
end
|
79
|
+
|
80
|
+
def attribute_values
|
81
|
+
|
82
|
+
self.attributes.values.map {|attribute| attribute}
|
83
|
+
end
|
84
|
+
|
85
|
+
def insert
|
86
|
+
col_names = self.class.columns.drop(1).join(', ')
|
87
|
+
question_marks = ['INTERPOLATOR_MARK'] * attribute_values.length
|
88
|
+
question_marks = question_marks.join(', ')
|
89
|
+
MonoRM::DBConnection.execute(<<-SQL, *attribute_values)
|
90
|
+
INSERT INTO
|
91
|
+
#{self.class.table_name}(#{col_names})
|
92
|
+
VALUES
|
93
|
+
(#{question_marks})
|
94
|
+
SQL
|
95
|
+
|
96
|
+
send(:id=, MonoRM::DBConnection.last_insert_row_id)
|
97
|
+
end
|
98
|
+
|
99
|
+
def update
|
100
|
+
attribs = attribute_values.drop(1)
|
101
|
+
id = self.id
|
102
|
+
|
103
|
+
cols = self.class.columns.drop(1)
|
104
|
+
|
105
|
+
set_lines = cols.map do |col|
|
106
|
+
"#{col} = INTERPOLATOR_MARK"
|
107
|
+
end
|
108
|
+
|
109
|
+
set_lines = set_lines.join(', ')
|
110
|
+
|
111
|
+
MonoRM::DBConnection.execute(<<-SQL, *attribs, id)
|
112
|
+
UPDATE
|
113
|
+
#{self.class.table_name}
|
114
|
+
SET
|
115
|
+
#{set_lines}
|
116
|
+
WHERE
|
117
|
+
id = INTERPOLATOR_MARK
|
118
|
+
SQL
|
119
|
+
|
120
|
+
end
|
121
|
+
|
122
|
+
def save
|
123
|
+
if self.class.find(self.id)
|
124
|
+
update
|
125
|
+
else
|
126
|
+
insert
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
def destroy
|
131
|
+
id = self.id
|
132
|
+
MonoRM::DBConnection.execute(<<-SQL, id)
|
133
|
+
DELETE
|
134
|
+
FROM
|
135
|
+
#{self.class.table_name}
|
136
|
+
WHERE
|
137
|
+
id = INTERPOLATOR_MARK
|
138
|
+
SQL
|
139
|
+
end
|
140
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Searchable
|
2
|
+
def where(params)
|
3
|
+
where_line = params.keys.map do |key|
|
4
|
+
"#{key} = INTERPOLATOR_MARK"
|
5
|
+
end
|
6
|
+
where_line = where_line.join(' AND ')
|
7
|
+
param_values = params.values
|
8
|
+
|
9
|
+
data = MonoRM::DBConnection.execute(<<-SQL, *param_values)
|
10
|
+
SELECT
|
11
|
+
*
|
12
|
+
FROM
|
13
|
+
#{self.table_name}
|
14
|
+
WHERE
|
15
|
+
#{where_line}
|
16
|
+
SQL
|
17
|
+
parse_all(data)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class MonoRM::Base
|
22
|
+
extend Searchable
|
23
|
+
end
|
data/lib/monorm.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
require "monorm/version"
|
4
|
+
|
5
|
+
require 'monorm/base'
|
6
|
+
|
7
|
+
|
8
|
+
MonoRM::PROJECT_ROOT_DIR = PROJECT_ROOT_DIR
|
9
|
+
|
10
|
+
module MonoRM
|
11
|
+
|
12
|
+
def self.root
|
13
|
+
|
14
|
+
PROJECT_ROOT_DIR
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.config
|
19
|
+
File.join root, 'config'
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.db_config
|
23
|
+
File.join config, 'database.yml'
|
24
|
+
end
|
25
|
+
#
|
26
|
+
# def self.bin
|
27
|
+
# File.join root, 'bin'
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
def self.lib
|
31
|
+
File.join root, 'lib'
|
32
|
+
end
|
33
|
+
#
|
34
|
+
def self.monorm
|
35
|
+
File.join lib, 'monorm'
|
36
|
+
end
|
37
|
+
|
38
|
+
DB_CONFIG = YAML.load(File.open("#{self.db_config}"))
|
39
|
+
|
40
|
+
class MonoRM::DBInitializer
|
41
|
+
def self.load_db_adapter
|
42
|
+
|
43
|
+
adapter = DB_CONFIG['default']['adapter']
|
44
|
+
case adapter
|
45
|
+
when 'postgresql'
|
46
|
+
adapter_path = File.join('monorm', 'adapters', 'pg_connection')
|
47
|
+
require adapter_path
|
48
|
+
when 'sqlite3'
|
49
|
+
adapter_path = File.join('monorm', 'adapters', 'sqlite_connection')
|
50
|
+
require adapter_path
|
51
|
+
else
|
52
|
+
raise 'Database type not found!'
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
data/monorm.gemspec
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'monorm/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "monorm"
|
8
|
+
spec.version = MonoRM::VERSION
|
9
|
+
spec.authors = ["Michael Chilton"]
|
10
|
+
spec.email = ["michaelc962@yahoo.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{MonoRM is a small ORM library designed to make interacting with the database easier.}
|
13
|
+
spec.description = %q{MonoRM is a small ORM library designed to make interacting with the database easier. It allows for intereaction with multiple database types, model relations, and features such as basic CRUD actions}
|
14
|
+
spec.homepage = "https://github.com/mc962/monorm"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org/"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
23
|
+
"public gem pushes."
|
24
|
+
end
|
25
|
+
|
26
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
27
|
+
f.match(%r{^(test|spec|features)/})
|
28
|
+
end
|
29
|
+
spec.bindir = "exe"
|
30
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
31
|
+
|
32
|
+
spec.require_paths = ["lib"]
|
33
|
+
|
34
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
35
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
36
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
37
|
+
spec.add_development_dependency 'byebug', '~> 9.0'
|
38
|
+
spec.add_development_dependency 'pry', '~> 0.10.4'
|
39
|
+
|
40
|
+
spec.add_runtime_dependency 'activesupport', '~> 5.0'
|
41
|
+
|
42
|
+
|
43
|
+
|
44
|
+
end
|
metadata
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: monorm
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michael Chilton
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-03-27 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: '1.14'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.14'
|
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: byebug
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '9.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '9.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.10.4
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.10.4
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: activesupport
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '5.0'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '5.0'
|
97
|
+
description: MonoRM is a small ORM library designed to make interacting with the database
|
98
|
+
easier. It allows for intereaction with multiple database types, model relations,
|
99
|
+
and features such as basic CRUD actions
|
100
|
+
email:
|
101
|
+
- michaelc962@yahoo.com
|
102
|
+
executables: []
|
103
|
+
extensions: []
|
104
|
+
extra_rdoc_files: []
|
105
|
+
files:
|
106
|
+
- ".gitignore"
|
107
|
+
- ".rspec"
|
108
|
+
- ".travis.yml"
|
109
|
+
- CODE_OF_CONDUCT.md
|
110
|
+
- Gemfile
|
111
|
+
- LICENSE.txt
|
112
|
+
- README.md
|
113
|
+
- Rakefile
|
114
|
+
- bin/console
|
115
|
+
- bin/setup
|
116
|
+
- lib/monorm.rb
|
117
|
+
- lib/monorm/adapters/pg_connection.rb
|
118
|
+
- lib/monorm/adapters/sqlite_connection.rb
|
119
|
+
- lib/monorm/associatable.rb
|
120
|
+
- lib/monorm/base.rb
|
121
|
+
- lib/monorm/searchable.rb
|
122
|
+
- lib/monorm/version.rb
|
123
|
+
- monorm.gemspec
|
124
|
+
homepage: https://github.com/mc962/monorm
|
125
|
+
licenses:
|
126
|
+
- MIT
|
127
|
+
metadata:
|
128
|
+
allowed_push_host: https://rubygems.org/
|
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
|
+
rubyforge_project:
|
145
|
+
rubygems_version: 2.5.2
|
146
|
+
signing_key:
|
147
|
+
specification_version: 4
|
148
|
+
summary: MonoRM is a small ORM library designed to make interacting with the database
|
149
|
+
easier.
|
150
|
+
test_files: []
|