easy-record 0.1.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 +7 -0
- data/lib/application/association.rb +33 -0
- data/lib/application/index.rb +14 -0
- data/lib/application/record.rb +23 -0
- data/lib/easy_record.rb +30 -0
- metadata +46 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 332a82a36277e482de150d9879652543f941580065ae8f5de67397c3c5249a05
|
4
|
+
data.tar.gz: ece90c8c88b5790b57b67abc1f31c6cb7c5fe429ab5041a788152436fdfd5313
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a98c66fea5917a9604e1069a482f03fb6bb41c62cc1c4bb690f8fa91deaba405ae52a19d62470ca369b5dbba8f13260c0c4815ad0817b120ef412fdd8ea8d430
|
7
|
+
data.tar.gz: 6edef226238839b27b3cca042be5dcbf81688fbb016334f869692e5613ed962cbba14b410de0a298247821b173cc6e610efdc16c310a8f545c9e93c3dcf7e7dd
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Association
|
2
|
+
def has_many(name, model_name)
|
3
|
+
if model_name.keys.first == :class_name
|
4
|
+
has_many_simple(name, model_name)
|
5
|
+
else
|
6
|
+
has_many_through(name, model_name)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def belongs_to(name, model_name, target_id)
|
11
|
+
self.define_method(name) do
|
12
|
+
Object.const_get(model_name[:class_name]).all.find { |m| m.id == self.send(target_id.to_s) }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def has_many_simple(name, model_name)
|
19
|
+
# Creates method named as the param name says.
|
20
|
+
self.define_method(name) do
|
21
|
+
# Select those that has the same id that current model_name.
|
22
|
+
Object.const_get(model_name[:class_name]).all.select { |m| m.send("#{self.class.name.snakecase}_id") == @id }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def has_many_through(name, common_model_name)
|
27
|
+
self.define_method(name) do
|
28
|
+
self.send(common_model_name[:through]).select do |m|
|
29
|
+
m.send("#{self.class.name.snakecase}_id") == @id
|
30
|
+
end.map { |m| m.send(name.to_s) }.flatten
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Index
|
2
|
+
$indexes = {} unless defined? $indexes
|
3
|
+
|
4
|
+
def self.next_id(model = nil)
|
5
|
+
return if model.nil?
|
6
|
+
|
7
|
+
if $indexes.map(&:first).include?(model.class.name)
|
8
|
+
$indexes[model.class.name] += 1
|
9
|
+
else
|
10
|
+
$indexes[model.class.name] = 1
|
11
|
+
end
|
12
|
+
$indexes[model.class.name]
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Record
|
2
|
+
$records = {} unless defined? $records
|
3
|
+
|
4
|
+
def self.track(instance = nil)
|
5
|
+
return if instance.nil?
|
6
|
+
|
7
|
+
if $records.map(&:first).include?(instance.class.name)
|
8
|
+
$records[instance.class.name].push(instance)
|
9
|
+
else
|
10
|
+
$records[instance.class.name] = [instance]
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.untrack(instance)
|
15
|
+
index = $records.index(instance)
|
16
|
+
$records.delete_at(index) unless index.nil?
|
17
|
+
$records
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.of(class_name)
|
21
|
+
$records[class_name]
|
22
|
+
end
|
23
|
+
end
|
data/lib/easy_record.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require_relative './application/record'
|
2
|
+
require_relative './application/index'
|
3
|
+
require_relative './application/association'
|
4
|
+
|
5
|
+
class EasyRecord
|
6
|
+
require 'snake_camel'
|
7
|
+
extend Association
|
8
|
+
attr_reader :id
|
9
|
+
|
10
|
+
def self.all
|
11
|
+
Record.of(self.to_s)
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.first
|
15
|
+
self.all.first
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.last
|
19
|
+
self.all.last
|
20
|
+
end
|
21
|
+
|
22
|
+
#private
|
23
|
+
|
24
|
+
def initialize(args = nil)
|
25
|
+
Record.track(self)
|
26
|
+
@id = Index.next_id(self)
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: easy-record
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ricardo Rafael Villagrana Larios
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-02-06 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Easy Record allows you to create associations between models and store
|
14
|
+
them in disk
|
15
|
+
email: 'ricardovillagranal@gmail.com '
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/application/association.rb
|
21
|
+
- lib/application/index.rb
|
22
|
+
- lib/application/record.rb
|
23
|
+
- lib/easy_record.rb
|
24
|
+
homepage: http://github.com/ricvillagrana/easy-record
|
25
|
+
licenses: []
|
26
|
+
metadata: {}
|
27
|
+
post_install_message:
|
28
|
+
rdoc_options: []
|
29
|
+
require_paths:
|
30
|
+
- lib
|
31
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0'
|
36
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
requirements: []
|
42
|
+
rubygems_version: 3.0.1
|
43
|
+
signing_key:
|
44
|
+
specification_version: 4
|
45
|
+
summary: Easy Record
|
46
|
+
test_files: []
|