easy-record 0.1.0 → 0.1.1.alpha1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 332a82a36277e482de150d9879652543f941580065ae8f5de67397c3c5249a05
4
- data.tar.gz: ece90c8c88b5790b57b67abc1f31c6cb7c5fe429ab5041a788152436fdfd5313
3
+ metadata.gz: 17e6219d84848a1ece64c0dd8198409940ba3078ccd9eab9b3105c4af25610da
4
+ data.tar.gz: b913d52c1514ee65c154e5f227fe00e22b285e8333fff8d322ea083921a13cbc
5
5
  SHA512:
6
- metadata.gz: a98c66fea5917a9604e1069a482f03fb6bb41c62cc1c4bb690f8fa91deaba405ae52a19d62470ca369b5dbba8f13260c0c4815ad0817b120ef412fdd8ea8d430
7
- data.tar.gz: 6edef226238839b27b3cca042be5dcbf81688fbb016334f869692e5613ed962cbba14b410de0a298247821b173cc6e610efdc16c310a8f545c9e93c3dcf7e7dd
6
+ metadata.gz: a7c5826a75b1d9d495bd10323a8e71ccd3ef7899844a3e324e4dd4b36051e6968da67fdc8fa706fa7cfd78fe47dc816f8e42c36938a81ca0ecc7d0d55770fc9b
7
+ data.tar.gz: ac7b43b3ea1d56ff172b73d2b4d5067b6c3f627f0eb79680c69083be60244b8983db6449ac44462297d65d89bd0cb4964ebe53bc1fa813747fe1585a4065d90d
@@ -1,5 +1,6 @@
1
1
  module Association
2
2
  def has_many(name, model_name)
3
+ # Determinate if second param is a class_name or a through class
3
4
  if model_name.keys.first == :class_name
4
5
  has_many_simple(name, model_name)
5
6
  else
@@ -16,18 +17,24 @@ module Association
16
17
  private
17
18
 
18
19
  def has_many_simple(name, model_name)
19
- # Creates method named as the param name says.
20
+ # Create method named as the param name says.
20
21
  self.define_method(name) do
21
22
  # 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
+ Object.const_get(model_name[:class_name]).all.select do |m|
24
+ # Return true when target id is equal to slef id
25
+ m.send("#{self.class.name.snakecase}_id") == @id
26
+ end
23
27
  end
24
28
  end
25
29
 
26
30
  def has_many_through(name, common_model_name)
31
+ # Create method with the name of model
27
32
  self.define_method(name) do
33
+ # Get all related transition records with select Enumerable
28
34
  self.send(common_model_name[:through]).select do |m|
35
+ # Return array of target records
29
36
  m.send("#{self.class.name.snakecase}_id") == @id
30
- end.map { |m| m.send(name.to_s) }.flatten
37
+ end.map { |m| m.send(name.to_s) }.flatten # Flat the array of array to an array of records
31
38
  end
32
39
  end
33
40
  end
@@ -1,14 +1,20 @@
1
1
  module Index
2
+ # Initilialize $indexes
2
3
  $indexes = {} unless defined? $indexes
3
4
 
4
5
  def self.next_id(model = nil)
6
+ # If model was not privided just exit
5
7
  return if model.nil?
6
8
 
9
+ # map indexes of $indexes to know if the model is alredy in it.
7
10
  if $indexes.map(&:first).include?(model.class.name)
11
+ # If so, increment
8
12
  $indexes[model.class.name] += 1
9
13
  else
14
+ # Otherwise initialize with 1
10
15
  $indexes[model.class.name] = 1
11
16
  end
17
+ # return the current index
12
18
  $indexes[model.class.name]
13
19
  end
14
20
  end
@@ -1,23 +1,30 @@
1
1
  module Record
2
+ # Initialize $records
2
3
  $records = {} unless defined? $records
3
4
 
4
5
  def self.track(instance = nil)
6
+ # If model was not provided just exit.
5
7
  return if instance.nil?
6
8
 
9
+ # mao indexes of $records and know if model is alredy in it.
7
10
  if $records.map(&:first).include?(instance.class.name)
11
+ # If so, push the new record on array,
8
12
  $records[instance.class.name].push(instance)
9
13
  else
14
+ # Otherwise initilize it with the record
10
15
  $records[instance.class.name] = [instance]
11
16
  end
12
17
  end
13
18
 
14
19
  def self.untrack(instance)
20
+ # Delete the record from tracked records
15
21
  index = $records.index(instance)
16
22
  $records.delete_at(index) unless index.nil?
17
23
  $records
18
24
  end
19
25
 
20
26
  def self.of(class_name)
27
+ # Return array of records of provided class_name
21
28
  $records[class_name]
22
29
  end
23
30
  end
@@ -0,0 +1,37 @@
1
+ module Storage
2
+ module StoreCSV
3
+ require 'csv'
4
+ require 'snake_camel'
5
+
6
+ def save_as_csv
7
+ CSV.open("./#{self.name.snakecase}.csv", 'wb') do |csv|
8
+ # Headers
9
+ csv << attrs
10
+ self.all.each do |record|
11
+ csv << attrs.map { |attr| record.send(attr) }
12
+ end
13
+ end
14
+ end
15
+
16
+ def load_from_csv
17
+ data = csv_data.to_a
18
+ headers = data.shift
19
+
20
+ records = data.map { |record| Hash[headers.zip(record)] }
21
+
22
+ records.each do |record|
23
+ self.new(record)
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def csv_data
30
+ CSV.read("./#{self.name.snakecase}.csv", headers: true)
31
+ end
32
+
33
+ def attrs
34
+ self.first.instance_variables.map { |var| var.to_s[1..-1] }
35
+ end
36
+ end
37
+ end
data/lib/easy_record.rb CHANGED
@@ -1,11 +1,16 @@
1
- require_relative './application/record'
2
- require_relative './application/index'
3
- require_relative './application/association'
1
+ require_relative './easy_record/record'
2
+ require_relative './easy_record/index'
3
+ require_relative './easy_record/association'
4
+ require_relative './easy_record/storage'
4
5
 
5
6
  class EasyRecord
6
7
  require 'snake_camel'
8
+ require 'pry'
9
+
7
10
  extend Association
8
- attr_reader :id
11
+ extend Storage
12
+
13
+ attr_accessor :id
9
14
 
10
15
  def self.all
11
16
  Record.of(self.to_s)
@@ -19,12 +24,21 @@ class EasyRecord
19
24
  self.all.last
20
25
  end
21
26
 
22
- #private
23
-
24
- def initialize(args = nil)
27
+ def initialize(attributes = nil)
25
28
  Record.track(self)
26
29
  @id = Index.next_id(self)
30
+
31
+ set_attributes(attributes)
27
32
  end
28
33
 
34
+ private
35
+
36
+ def set_attributes(attributes)
37
+ attributes.each { |key, value| set(key, value) } if attributes.is_a?(Hash)
38
+ end
39
+
40
+ def set(key, value)
41
+ self.send("#{key}=", value)
42
+ end
29
43
 
30
44
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy-record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1.alpha1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ricardo Rafael Villagrana Larios
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-02-06 00:00:00.000000000 Z
11
+ date: 2019-02-08 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Easy Record allows you to create associations between models and store
14
14
  them in disk
@@ -17,10 +17,11 @@ executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
- - lib/application/association.rb
21
- - lib/application/index.rb
22
- - lib/application/record.rb
23
20
  - lib/easy_record.rb
21
+ - lib/easy_record/association.rb
22
+ - lib/easy_record/index.rb
23
+ - lib/easy_record/record.rb
24
+ - lib/easy_record/storage/store_csv.rb
24
25
  homepage: http://github.com/ricvillagrana/easy-record
25
26
  licenses: []
26
27
  metadata: {}
@@ -35,9 +36,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
35
36
  version: '0'
36
37
  required_rubygems_version: !ruby/object:Gem::Requirement
37
38
  requirements:
38
- - - ">="
39
+ - - ">"
39
40
  - !ruby/object:Gem::Version
40
- version: '0'
41
+ version: 1.3.1
41
42
  requirements: []
42
43
  rubygems_version: 3.0.1
43
44
  signing_key: