easy_save 0.3.0 → 0.4.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.
- checksums.yaml +4 -4
- data/README.md +83 -11
- data/{example.rb → examples/example.rb} +17 -1
- data/lib/easy_save/db_connection.rb +3 -5
- data/lib/easy_save/version.rb +1 -1
- metadata +4 -6
- data/.rspec +0 -2
- data/.travis.yml +0 -5
- /data/{example.sql → examples/example.sql} +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 43de1c6f3d6cdd41f228e922141992b5375cf85d
|
4
|
+
data.tar.gz: 929b8badc55e6cbcc883a8c1e8a5d45cde780249
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: df6cfb4df7c7b646c67fe1dbf5b7f08028259b348744f1841ecbaa7b96148badb9c74ee193efe98b2746e4122c06b1fdb23af7074a178ff58d2b2fcf385bf02b
|
7
|
+
data.tar.gz: c20cdbf9f16cad339e982af03d562c3c68107cf7b736889c1f43374dd24381828e8292133a775db00e21ba0d772cdd07438a8ee8da8e30f3c683173203e9d5ec
|
data/README.md
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
# EasySave
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
EasySave is a lightweight ORM written in Ruby that abstracts away the
|
4
|
+
complexities of working with SQL databases.
|
6
5
|
|
7
6
|
## Installation
|
8
7
|
|
@@ -22,20 +21,93 @@ Or install it yourself as:
|
|
22
21
|
|
23
22
|
## Usage
|
24
23
|
|
25
|
-
|
24
|
+
### Setting up for your own usage:
|
25
|
+
|
26
|
+
Place your sql file in the root of the project directory, and in
|
27
|
+
`lib/easy_save/db_connection.rb` change `'example.sql'` to your sql file's
|
28
|
+
name.
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
#lib/easy_save/db_connection.rb
|
32
|
+
USERS_SQL_FILE = File.join(ROOT_FOLDER, 'example.sql')
|
33
|
+
USERS_DB_FILE = File.join(ROOT_FOLDER, 'example.db')
|
34
|
+
|
35
|
+
```
|
36
|
+
|
37
|
+
Once that's done, in each model that is involved in the database, require
|
38
|
+
'easy_gem', subclass it from SQLObject and `finalize!` it to create the methods.
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
require 'easy_gem'
|
42
|
+
|
43
|
+
class User < SQLObject
|
44
|
+
|
45
|
+
self.finalize!
|
46
|
+
end
|
47
|
+
```
|
48
|
+
|
49
|
+
### Setting up demo:
|
50
|
+
|
51
|
+
A demo database has been included as well, in the example folder.
|
52
|
+
|
53
|
+
Run Pry or IRB in the examples folder, and `load 'example.rb'`.
|
54
|
+
|
55
|
+
At that point `Users`, `Comments`, `Photos`, and their respective associations can all
|
56
|
+
be explored.
|
57
|
+
|
58
|
+
#### A step by step guide:
|
59
|
+
|
60
|
+
1. Clone the repo.
|
61
|
+
2. Open `EasySave/examples` and Pry / IRB in that same folder.
|
62
|
+
3. `load 'example.rb'`
|
63
|
+
4. Try out different things like User.find(1) or Comment.find(2).
|
64
|
+
|
65
|
+
## Features
|
66
|
+
|
67
|
+
### Example Methods
|
68
|
+
|
69
|
+
Methods will return an instance of the object using the information from
|
70
|
+
the database if appropriate.
|
71
|
+
|
72
|
+
#### Object methods:
|
73
|
+
|
74
|
+
* `::all`: Returns an array of all the objects from the corresponding table.
|
75
|
+
* `::find(id)`: Returns the object corresponding to the row with the ID.
|
76
|
+
* `::first`: Returns the first row from that table.
|
77
|
+
* `::table_name`: Returns the instance variable of the table name, or creates
|
78
|
+
the table name name and sets the instance variable for future reference.
|
79
|
+
* `::table_name=(table_name)`: Takes an argument and sets the table name to
|
80
|
+
that.
|
81
|
+
* `#insert`: Inserts the object into it's correponsind table as a new row.
|
82
|
+
* `#update`: Updates an objects information in the database.
|
83
|
+
* `#save`: Either updates or inserts an entry in the database, depending on if
|
84
|
+
you're already
|
85
|
+
* `#finalize!`: Necessary to create the attribute accessors based on database
|
86
|
+
columns. Called at the end of the class declaration..
|
26
87
|
|
27
|
-
|
88
|
+
#### Association methods:
|
28
89
|
|
29
|
-
|
90
|
+
* `::belongs_to(name, options)`: Take a model name and an optional options hash
|
91
|
+
and creates a method called `#name`. The method returns the instance of the
|
92
|
+
model name to whose ID matches the foreign key held by the object calling the
|
93
|
+
method.
|
94
|
+
* `::has_many(name, options)`: Similar to `#belongs_to` but instead returns
|
95
|
+
instances of the model name whose foreign key matches the ID help by object
|
96
|
+
calling the method.
|
97
|
+
* `::has_one_through(name, through_name, source_name)`: Requires three
|
98
|
+
arguments, the name of the target model class, the through name of the
|
99
|
+
intermediary, and the source name of the method in the intermediary. The
|
100
|
+
generated method returns an instance of `#name` whose id matches the foreign
|
101
|
+
key of the `through_name` object whose id matches the foreign key of the
|
102
|
+
`source name` object that is calling the method.
|
30
103
|
|
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
104
|
|
33
|
-
## Contributing
|
34
105
|
|
35
|
-
|
106
|
+
## Future Work
|
36
107
|
|
108
|
+
1. `has_many_through` associations
|
109
|
+
2. Validation check methods
|
110
|
+
3. `joins` to perform SQL joins
|
37
111
|
|
38
|
-
## License
|
39
112
|
|
40
|
-
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
113
|
|
@@ -1,5 +1,4 @@
|
|
1
1
|
require 'sqlite3'
|
2
|
-
require 'byebug'
|
3
2
|
|
4
3
|
PRINT_QUERIES = ENV['PRINT_QUERIES'] == 'true'
|
5
4
|
# https://tomafro.net/2010/01/tip-relative-paths-with-file-expand-path
|
@@ -18,10 +17,9 @@ class DBConnection
|
|
18
17
|
end
|
19
18
|
|
20
19
|
def self.reset
|
21
|
-
commands = [
|
22
|
-
|
23
|
-
|
24
|
-
]
|
20
|
+
commands = []
|
21
|
+
commands << "rm '#{USERS_DB_FILE}'" if File.exist?(USERS_DB_FILE)
|
22
|
+
commands << "cat '#{USERS_SQL_FILE}' | sqlite3 '#{USERS_DB_FILE}'"
|
25
23
|
|
26
24
|
commands.each { |command| `#{command}` }
|
27
25
|
DBConnection.open(USERS_DB_FILE)
|
data/lib/easy_save/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: easy_save
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Akshith Yellapragada
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-05-
|
11
|
+
date: 2017-05-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -88,8 +88,6 @@ extensions: []
|
|
88
88
|
extra_rdoc_files: []
|
89
89
|
files:
|
90
90
|
- ".gitignore"
|
91
|
-
- ".rspec"
|
92
|
-
- ".travis.yml"
|
93
91
|
- CODE_OF_CONDUCT.md
|
94
92
|
- Gemfile
|
95
93
|
- LICENSE.txt
|
@@ -98,8 +96,8 @@ files:
|
|
98
96
|
- bin/console
|
99
97
|
- bin/setup
|
100
98
|
- easy_save.gemspec
|
101
|
-
- example.rb
|
102
|
-
- example.sql
|
99
|
+
- examples/example.rb
|
100
|
+
- examples/example.sql
|
103
101
|
- lib/easy_save.rb
|
104
102
|
- lib/easy_save/associatable.rb
|
105
103
|
- lib/easy_save/db_connection.rb
|
data/.rspec
DELETED
data/.travis.yml
DELETED
File without changes
|