mysql_simple_orm 0.1.1
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 +7 -0
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +37 -0
- data/README.md +55 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/mysql_simple_orm.rb +249 -0
- data/lib/mysql_simple_orm/version.rb +3 -0
- data/mysql_simple_orm.gemspec +26 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1b1314e6363871d103316a3424986bbdbe68a0a2
|
4
|
+
data.tar.gz: 1d9c708e47dd4586e8d67420e8aa0b694d1ef897
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d63de1ee8ab874e2dd8821107269ced7305a29ff87290ee56708e407923a3da33778487b9af1299844981d62113fe3c6a2641c8e5cac2e538d21425084bc1a0a
|
7
|
+
data.tar.gz: 3ebba8bd5d083cfd8914127f86304889c13d2b580fd162275d4cb495642d3d3707e2be96f8bc3e8e5d702c5d4d45b7bd06e142274b401e729952e05c3bb0f8af
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
mysql_simple_orm (0.1.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.3)
|
10
|
+
mysql2 (0.4.10)
|
11
|
+
rake (10.5.0)
|
12
|
+
rspec (3.9.0)
|
13
|
+
rspec-core (~> 3.9.0)
|
14
|
+
rspec-expectations (~> 3.9.0)
|
15
|
+
rspec-mocks (~> 3.9.0)
|
16
|
+
rspec-core (3.9.0)
|
17
|
+
rspec-support (~> 3.9.0)
|
18
|
+
rspec-expectations (3.9.0)
|
19
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
20
|
+
rspec-support (~> 3.9.0)
|
21
|
+
rspec-mocks (3.9.0)
|
22
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
23
|
+
rspec-support (~> 3.9.0)
|
24
|
+
rspec-support (3.9.0)
|
25
|
+
|
26
|
+
PLATFORMS
|
27
|
+
ruby
|
28
|
+
|
29
|
+
DEPENDENCIES
|
30
|
+
bundler (~> 1.16)
|
31
|
+
mysql2 (~> 0.4.1)
|
32
|
+
mysql_simple_orm!
|
33
|
+
rake (~> 10.0)
|
34
|
+
rspec (~> 3.0)
|
35
|
+
|
36
|
+
BUNDLED WITH
|
37
|
+
1.16.1
|
data/README.md
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# MysqlSimpleOrm
|
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/mysql_simple_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 'mysql_simple_orm'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install mysql_simple_orm
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
|
27
|
+
#Initialize DB connection
|
28
|
+
|
29
|
+
Orm::Base.setup do
|
30
|
+
{ host: "localhost", username: "root", password: "root", database: "evangelizacion" }
|
31
|
+
end
|
32
|
+
|
33
|
+
class Team < Orm::Base
|
34
|
+
has_many :members
|
35
|
+
end
|
36
|
+
|
37
|
+
class Member < Orm::Base
|
38
|
+
belongs_to :team
|
39
|
+
end
|
40
|
+
|
41
|
+
member = Member.first
|
42
|
+
team = member.team
|
43
|
+
all_members = team.members
|
44
|
+
|
45
|
+
```
|
46
|
+
|
47
|
+
## Development
|
48
|
+
|
49
|
+
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.
|
50
|
+
|
51
|
+
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).
|
52
|
+
|
53
|
+
## Contributing
|
54
|
+
|
55
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/mysql_simple_orm.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "mysql_simple_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,249 @@
|
|
1
|
+
require 'mysql_simple_orm/version'
|
2
|
+
require 'mysql2'
|
3
|
+
|
4
|
+
module MysqlSimpleOrm
|
5
|
+
class Base
|
6
|
+
def initialize(attrs={})
|
7
|
+
self.class.build_entity_methods
|
8
|
+
|
9
|
+
cols = self.class.fetch_columns.select{|c| c.to_s != 'id'}.map(&:to_sym)
|
10
|
+
attrs.keys.each { |key| attrs[key.to_sym] = attrs.delete(key)}
|
11
|
+
cols.each do |c|
|
12
|
+
if !attrs.has_key?(c)
|
13
|
+
attrs[c] = nil
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
attrs.each do |key, val|
|
18
|
+
if !self.class.method_defined?(key)
|
19
|
+
raise "No method `#{key}` for #{self.class.name.capitalize} instance."
|
20
|
+
end
|
21
|
+
send("#{key}=", val) if key.to_s != 'id'
|
22
|
+
@id = val if key.to_s == 'id'
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
def update_with(attrs={})
|
28
|
+
return false if id.nil?
|
29
|
+
|
30
|
+
allowed_attrs = attrs.select do |k, v|
|
31
|
+
self.class.fetch_columns.include?(k.to_s) && k.to_s != 'id'
|
32
|
+
end
|
33
|
+
|
34
|
+
allowed_attrs.each{|k, v| send("#{k}=", v)}
|
35
|
+
|
36
|
+
filtered_attrs = self.class.filtered_attrs
|
37
|
+
@errors = self.class.check_errors(self, filtered_attrs)
|
38
|
+
|
39
|
+
return false if errors.to_a.any?
|
40
|
+
|
41
|
+
values = allowed_attrs.map{|k,v| "`#{k}` = '#{v}'"}.join(', ')
|
42
|
+
sql = "UPDATE #{self.class.table_name} SET #{values} WHERE id=#{id}"
|
43
|
+
self.class.exec(sql)
|
44
|
+
return true
|
45
|
+
rescue
|
46
|
+
false
|
47
|
+
end
|
48
|
+
|
49
|
+
def save
|
50
|
+
filtered_attrs = self.class.filtered_attrs
|
51
|
+
@errors = self.class.check_errors(self, filtered_attrs)
|
52
|
+
return false if errors.to_a.any?
|
53
|
+
if id.nil?
|
54
|
+
items = filtered_attrs.reduce({k: [], v: []}) do |res, k|
|
55
|
+
res[:k] << "`#{k}`"
|
56
|
+
res[:v] << "'#{send(k)}'"
|
57
|
+
res
|
58
|
+
end
|
59
|
+
values = "(#{items[:k].join(', ')}) VALUES (#{items[:v].join(',')})"
|
60
|
+
sql = "INSERT INTO #{self.class.table_name} #{values}"
|
61
|
+
self.class.exec(sql)
|
62
|
+
@id = @@db_client.last_id
|
63
|
+
return true
|
64
|
+
else
|
65
|
+
attrs = filtered_attrs.reduce({}) do |res, c|
|
66
|
+
res[c] = send(c)
|
67
|
+
res
|
68
|
+
end
|
69
|
+
update_with(attrs)
|
70
|
+
end
|
71
|
+
rescue => e
|
72
|
+
puts e
|
73
|
+
false
|
74
|
+
end
|
75
|
+
|
76
|
+
def errors
|
77
|
+
(@errors ||= []).compact.uniq
|
78
|
+
end
|
79
|
+
|
80
|
+
|
81
|
+
class << self
|
82
|
+
|
83
|
+
def setup
|
84
|
+
options = yield
|
85
|
+
@@db_client = Mysql2::Client.new(
|
86
|
+
host: options[:host],
|
87
|
+
username: options[:username],
|
88
|
+
password: options[:password],
|
89
|
+
database: options[:database]
|
90
|
+
)
|
91
|
+
end
|
92
|
+
|
93
|
+
|
94
|
+
def method_missing(method, *args)
|
95
|
+
return where($1 => args.first).first if method.to_s =~ /^get_by_(.+)/
|
96
|
+
super
|
97
|
+
end
|
98
|
+
|
99
|
+
def check_errors(obj, fields)
|
100
|
+
(@validations || []).map do |validation|
|
101
|
+
field = validation[0].to_s
|
102
|
+
msg = validation[1]
|
103
|
+
block = validation[2]
|
104
|
+
if fields.include? field
|
105
|
+
if block
|
106
|
+
evaluation = block.call(obj)
|
107
|
+
{field: field, msg: msg[:msg]} if !evaluation
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end.compact
|
111
|
+
end
|
112
|
+
|
113
|
+
def validate(field, message, &block)
|
114
|
+
validation = [field, message]
|
115
|
+
validation << block if block_given?
|
116
|
+
@validations ||= []
|
117
|
+
@validations << validation
|
118
|
+
end
|
119
|
+
|
120
|
+
def belongs_to(model)
|
121
|
+
define_method(model) do
|
122
|
+
# binding.pry
|
123
|
+
foreign_key = "#{model}_id"
|
124
|
+
Object.const_get(model.to_s.capitalize)
|
125
|
+
.where("id = #{send(foreign_key)}").first
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
def has_one(model, **fk)
|
130
|
+
method_name = model
|
131
|
+
method_name = fk[:as] if fk[:as]
|
132
|
+
define_method(method_name) do
|
133
|
+
# binding.pry
|
134
|
+
foreign_key = "#{model}_id"
|
135
|
+
foreign_key = fk[:via] if fk[:via]
|
136
|
+
Object.const_get("#{model.to_s.capitalize}")
|
137
|
+
.where("id = #{send(foreign_key)}").first
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
def has_many(model, **fk)
|
142
|
+
self.define_singleton_method(:via) do
|
143
|
+
fk[:via]
|
144
|
+
end
|
145
|
+
method_name = model
|
146
|
+
method_name = fk[:as] if fk[:as]
|
147
|
+
singular = model.to_s.chop
|
148
|
+
# p method_name
|
149
|
+
define_method(method_name) do
|
150
|
+
foreign_key = "#{self.class.name.downcase}_id"
|
151
|
+
foreign_key = fk[:via] if !fk[:via].nil?
|
152
|
+
Object.const_get(singular.capitalize)
|
153
|
+
.where("#{foreign_key} = #{send(:id)}")
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
def build_entity_methods
|
158
|
+
columns(*fetch_columns)
|
159
|
+
end
|
160
|
+
|
161
|
+
def filtered_attrs
|
162
|
+
fetch_columns.select{|f| f != 'id'}
|
163
|
+
end
|
164
|
+
|
165
|
+
def fetch_columns
|
166
|
+
cols = exec("EXPLAIN #{table_name}")
|
167
|
+
cols.map{|row| row['Field'] }
|
168
|
+
end
|
169
|
+
|
170
|
+
def first
|
171
|
+
at(:first)
|
172
|
+
end
|
173
|
+
|
174
|
+
def last
|
175
|
+
at(:last)
|
176
|
+
end
|
177
|
+
|
178
|
+
def at(position)
|
179
|
+
if position == :first
|
180
|
+
order = 'ASC'
|
181
|
+
elsif position == :last
|
182
|
+
order = 'DESC'
|
183
|
+
end
|
184
|
+
|
185
|
+
return nil unless order
|
186
|
+
|
187
|
+
sql = "SELECT * FROM #{table_name} ORDER BY id #{order} LIMIT 1"
|
188
|
+
new exec(sql).first
|
189
|
+
end
|
190
|
+
|
191
|
+
def find(id)
|
192
|
+
where(id: id).first
|
193
|
+
end
|
194
|
+
|
195
|
+
def all
|
196
|
+
sql = "SELECT * FROM #{table_name}"
|
197
|
+
exec(sql).map{|row| new(row)}
|
198
|
+
end
|
199
|
+
|
200
|
+
def joins(model, cond=nil)
|
201
|
+
fk = "#{table_name.to_s.chop}_id"
|
202
|
+
fk = self.via.to_s if self.via
|
203
|
+
sql = "SELECT * FROM #{table_name} INNER JOIN #{model.to_s} ON #{model.to_s}.#{fk} = #{table_name}.id"
|
204
|
+
sql += " WHERE #{cond}" if cond
|
205
|
+
p sql
|
206
|
+
exec(sql).map{|row| binding.pry; new(row)}
|
207
|
+
end
|
208
|
+
|
209
|
+
def condition_builder(cond)
|
210
|
+
res = cond.reduce('') do |res, each_cond|
|
211
|
+
attr = each_cond.first
|
212
|
+
value = each_cond.last
|
213
|
+
operator = value.is_a?(Array) ? "IN (#{value.map{|x|"'#{x}'"}.join(', ')})": "= '#{value}'"
|
214
|
+
res += "#{attr.to_s} #{operator} AND "
|
215
|
+
end
|
216
|
+
res[0...-5]
|
217
|
+
end
|
218
|
+
|
219
|
+
def where(cond)
|
220
|
+
cond = condition_builder(cond) if cond.is_a?(Hash)
|
221
|
+
sql = "SELECT * FROM #{table_name} WHERE #{cond}"
|
222
|
+
puts sql
|
223
|
+
exec(sql).map{|row| new(row)}
|
224
|
+
end
|
225
|
+
|
226
|
+
def exec(sql)
|
227
|
+
@@db_client.query(sql)
|
228
|
+
end
|
229
|
+
|
230
|
+
def create(attrs)
|
231
|
+
obj = new(attrs)
|
232
|
+
obj.save ? obj : nil
|
233
|
+
end
|
234
|
+
|
235
|
+
def columns(*args)
|
236
|
+
args.each do |f|
|
237
|
+
attr_accessor(f) if f != 'id'
|
238
|
+
attr_reader(f) if f == 'id'
|
239
|
+
end
|
240
|
+
end
|
241
|
+
|
242
|
+
def table_name
|
243
|
+
return "#{self.to_s.downcase}s" if superclass.to_s == 'MysqlSimpleOrm::Base'
|
244
|
+
"#{superclass.to_s.downcase}s"
|
245
|
+
end
|
246
|
+
|
247
|
+
end
|
248
|
+
end
|
249
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "mysql_simple_orm/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "mysql_simple_orm"
|
8
|
+
spec.version = MysqlSimpleOrm::VERSION
|
9
|
+
spec.authors = ["Melvin Rodriguez"]
|
10
|
+
spec.email = ["melvinrr25@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Simple Mysql2 ORM to handle models}
|
13
|
+
spec.description = %q{Simple Mysql2 ORM}
|
14
|
+
spec.homepage = "https://github.com/melvinrr25/mysql_simple_orm"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mysql_simple_orm
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Melvin Rodriguez
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-11-02 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.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
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
|
+
description: Simple Mysql2 ORM
|
56
|
+
email:
|
57
|
+
- melvinrr25@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- Gemfile.lock
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- lib/mysql_simple_orm.rb
|
72
|
+
- lib/mysql_simple_orm/version.rb
|
73
|
+
- mysql_simple_orm.gemspec
|
74
|
+
homepage: https://github.com/melvinrr25/mysql_simple_orm
|
75
|
+
licenses: []
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 2.5.1
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: Simple Mysql2 ORM to handle models
|
97
|
+
test_files: []
|