relation 0.1.5 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: df6322bf0f490d490afb201cf26f915bfeb80be2baf9719c2a038c896a653d85
4
- data.tar.gz: 4d2b576cb95debcedcb8116a1f1eaf898ca3b79e70c4472afd3b17f520cbfd69
3
+ metadata.gz: 5527b21869e536172e48b7d49e14c6bddeb1f7c522c8c1e9a23005d9a008f635
4
+ data.tar.gz: ad2d9f23632c38ed4f65ce9e367de6ad051b28f3dfc8de447fe62b07f129d3ed
5
5
  SHA512:
6
- metadata.gz: 5a796bb464316ee82537a1116b54f66900615242ad5c12f1acd097a0216e45ebb0e39dce2d959982339a334108c1e37bd10361f4e1c06a305bef41c63ad57970
7
- data.tar.gz: 1fc49c642dbb8030bd83810415de6cfd1e9d5e9f98d1a44db219e6278dd4fc86ab346f6a68a6eec3d908ce34f5628e78c775f8aee651256551bfd2458002f741
6
+ metadata.gz: 33fcd78c742fbd5c768595b4c4f7badabcdfbb54084acfacf971d603c923f6c746c80ca5cfdd69e4a652680f6c2620dd9bbf059cd0a7738dce05ae7d61bad68c
7
+ data.tar.gz: 7ec3c167144696171214d1a525a78fed82ad202efc5fe8e2655441f918a748e9f4aaeb321664cfed9576919fad90566d403e61c0851678f5a135c45d82d72d12
data/.gitignore CHANGED
@@ -8,9 +8,7 @@ pkg/*
8
8
  # Ignore the default SQLite database.
9
9
  /db/*.sqlite3
10
10
  /db/*.sqlite3-journal
11
- /spec/internal/db/*.sqlite3
12
11
 
13
12
  # Ignore all logfiles and tempfiles.
14
- /spec/internal/log
15
13
  log
16
14
  tmp
data/.watchr CHANGED
@@ -16,7 +16,7 @@ end
16
16
 
17
17
  def run_it(type, file)
18
18
  case type
19
- when 'test'; run %Q{ruby -I"lib:test" -rubygems #{file}}
19
+ when 'test'; run %Q{ruby -I test #{file}}
20
20
  # when 'spec'; run %Q{spring rspec -X #{file}}
21
21
  else; puts "#{H} unknown type: #{type}, file: #{file}"
22
22
  end
@@ -1,4 +1,4 @@
1
- Copyright 2015-2017 Dittmar Krall - http://matique.de
1
+ Copyright 2015-2018 Dittmar Krall - http://matique.de
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
@@ -0,0 +1,31 @@
1
+ # extended Relation: extracts relation from rows
2
+ class Relation < ActiveRecord::Base
3
+
4
+ def self.dangling
5
+ names = Relation.pluck(:name).uniq
6
+ models = []
7
+ names.each { |name|
8
+ models |= name.split(' ')
9
+ }
10
+ hsh = {}
11
+ models.each { |class_name|
12
+ klass = class_name.constantize
13
+ ids = klass.pluck(:id)
14
+ idx = Relation.where('name like ?', "#{class_name} %").pluck(:x_id)
15
+ idy = Relation.where('name like ?', "% #{class_name}").pluck(:y_id)
16
+ arr = (idx | idy) - ids
17
+ hsh[class_name] = arr if arr.length > 0
18
+ }
19
+ hsh
20
+ end
21
+
22
+ def self.remove_dangling(hsh)
23
+ hsh.each { |name, arr|
24
+ arr.each { |idx|
25
+ Relation.where(x_id: idx).where('name like ?', "#{name} %").delete_all
26
+ Relation.where(y_id: idx).where('name like ?', "% #{name}").delete_all
27
+ }
28
+ }
29
+ end
30
+
31
+ end
@@ -1,78 +1,21 @@
1
1
  class Relation < ActiveRecord::Base
2
2
 
3
- def self.add(row_from, row_to)
4
- name_from, id_from = name_id(row_from)
5
- name_to, id_to = name_id(row_to)
6
- name = "#{name_from} #{name_to}"
3
+ def self.add_raw(name, id_from, id_to)
7
4
  hsh = { name: name, x_id: id_from, y_id: id_to }
8
-
9
5
  Relation.create!(hsh) if Relation.where(hsh).first == nil
10
6
  end
11
7
 
12
- def self.delete(row_from, row_to)
13
- name_from, id_from = name_id(row_from)
14
- name_to, id_to = name_id(row_to)
15
- name = "#{name_from} #{name_to}"
8
+ def self.delete_raw(name, id_from, id_to)
16
9
  hsh = { name: name, x_id: id_from, y_id: id_to }
17
10
  Relation.where(hsh).delete_all
18
11
  end
19
12
 
20
- def self.references(row, kind)
21
- klass = kind
22
- klass = kind.constantize unless klass.kind_of?(Class)
23
- ids = self.references_ids(row, klass)
24
- klass.where(id: ids)
25
- end
26
-
27
- def self.followers(kind, row)
28
- klass = kind
29
- klass = kind.constantize unless klass.kind_of?(Class)
30
- ids = self.followers_ids(klass, row)
31
- klass.where(id: ids)
32
- end
33
-
34
- def self.dangling
35
- names = Relation.pluck(:name).uniq
36
- models = []
37
- names.each { |name|
38
- models |= name.split(' ')
39
- }
40
- hsh = {}
41
- models.each { |class_name|
42
- klass = class_name.constantize
43
- ids = klass.pluck(:id)
44
- idx = Relation.where('name like ?', "#{class_name} %").pluck(:x_id)
45
- idy = Relation.where('name like ?', "% #{class_name}").pluck(:y_id)
46
- arr = (idx | idy) - ids
47
- hsh[class_name] = arr if arr.length > 0
48
- }
49
- hsh
50
- end
51
-
52
- def self.remove_dangling(hsh)
53
- hsh.each { |name, arr|
54
- arr.each { |idx|
55
- Relation.where(x_id: idx).where('name like ?', "#{name} %").delete_all
56
- Relation.where(y_id: idx).where('name like ?', "% #{name}").delete_all
57
- }
58
- }
59
- end
60
-
61
- private
62
- def self.name_id(resource)
63
- raise 'missing resource' unless resource
64
- [resource.class.name, resource.id]
65
- end
66
-
67
- def self.references_ids(row, klass)
68
- name_from, id_from = name_id(row)
69
- name = "#{name_from} #{klass.name}"
13
+ def self.references_raw(name, id_from)
70
14
  Relation.where(name: name, x_id: id_from).pluck(:y_id)
71
15
  end
72
16
 
73
- def self.followers_ids(klass, row)
74
- name_to, id_to = name_id(row)
75
- name = "#{klass.name} #{name_to}"
17
+ def self.followers_raw(name, id_to)
76
18
  Relation.where(name: name, y_id: id_to).pluck(:x_id)
77
19
  end
20
+
78
21
  end
@@ -0,0 +1,48 @@
1
+ # extended Relation: extracts relation from rows
2
+ class Relation < ActiveRecord::Base
3
+
4
+ def self.add(row_from, row_to)
5
+ hsh = normalize(row_from, row_to)
6
+ Relation.create!(hsh) if Relation.where(hsh).first == nil
7
+ end
8
+
9
+ def self.delete(row_from, row_to)
10
+ hsh = normalize(row_from, row_to)
11
+ Relation.where(hsh).delete_all
12
+ end
13
+
14
+ def self.references(row, kind)
15
+ klass, name, id_from = normalize2(kind, row)
16
+ name = "#{name} #{klass.name}"
17
+ ids = references_raw(name, id_from)
18
+ klass.where(id: ids)
19
+ end
20
+
21
+ def self.followers(kind, row)
22
+ klass, name, id_to = normalize2(kind, row)
23
+ name = "#{klass.name} #{name}"
24
+ ids = followers_raw(name, id_to)
25
+ klass.where(id: ids)
26
+ end
27
+
28
+ private
29
+ def self.name_id(resource)
30
+ raise 'missing resource' unless resource
31
+ [resource.class.name, resource.id]
32
+ end
33
+
34
+ def self.normalize(row_from, row_to)
35
+ name_from, id_from = name_id(row_from)
36
+ name_to, id_to = name_id(row_to)
37
+ name = "#{name_from} #{name_to}"
38
+ { name: name, x_id: id_from, y_id: id_to }
39
+ end
40
+
41
+ def self.normalize2(kind, row)
42
+ klass = kind
43
+ klass = kind.constantize unless klass.kind_of?(Class)
44
+ name, id = name_id(row)
45
+ [klass, name, id]
46
+ end
47
+
48
+ end
@@ -1,3 +1,3 @@
1
1
  module ModRelation
2
- VERSION = '0.1.5'
2
+ VERSION = '0.2.1'
3
3
  end
@@ -0,0 +1,63 @@
1
+ require 'test_helper'
2
+
3
+ describe Relation do
4
+ let(:user) { User.create! name: 'user' }
5
+ let(:user2) { User.create! name: 'user2' }
6
+ let(:order) { Order.create! name: 'order' }
7
+ let(:order2) { Order.create! name: 'order2' }
8
+
9
+ def setup
10
+ DB.setup
11
+
12
+ Relation.add user, order
13
+ Relation.add user2, order
14
+ Relation.add user2, order2
15
+ end
16
+
17
+ def teardown
18
+ DB.teardown
19
+ end
20
+
21
+ it "should add a connection" do
22
+ assert_difference('Relation.count') do
23
+ Relation.add user2, user
24
+ end
25
+ end
26
+
27
+ it "should delete a connection" do
28
+ Relation.add user2, user
29
+ assert_difference('Relation.count', -1) do
30
+ Relation.delete user2, user
31
+ end
32
+ end
33
+
34
+ it "should return references (using class name)" do
35
+ arr = Relation.references(user, 'Order')
36
+ assert_equal [order].sort, arr.sort
37
+
38
+ arr = Relation.references(user2, 'Order')
39
+ assert_equal [order, order2].sort, arr.sort
40
+ end
41
+
42
+ it "should return followers (using class name)" do
43
+ arr = Relation.followers('User', order)
44
+ assert_equal [user, user2].sort, arr.sort
45
+
46
+ arr = Relation.followers('User', order2)
47
+ assert_equal [user2].sort, arr.sort
48
+ end
49
+
50
+ it "should not add twice the same connection" do
51
+ assert_difference('Relation.count') do
52
+ Relation.add user2, user
53
+ Relation.add user2, user
54
+ end
55
+ end
56
+
57
+ it "should handle unexistent connection" do
58
+ assert_difference('Relation.count', 0) do
59
+ Relation.delete user2, user
60
+ end
61
+ end
62
+
63
+ end
@@ -1,72 +1,64 @@
1
1
  require 'test_helper'
2
2
 
3
+ # testing raw/basic relations
3
4
  describe Relation do
4
- let(:user) { User.create! name: 'user' }
5
- let(:user2) { User.create! name: 'user2' }
6
- let(:order) { Order.create! name: 'order' }
7
- let(:order2) { Order.create! name: 'order2' }
5
+ let(:u_id) { User.create!(name: 'user').id }
6
+ let(:u2_id) { User.create!(name: 'user2').id }
7
+ let(:o_id) { Order.create!(name: 'order').id }
8
+ let(:o2_id) { Order.create!(name: 'order2').id }
9
+ let(:unknown_id) { 123456 }
8
10
 
9
11
  def setup
10
12
  DB.setup
11
13
 
12
- Relation.add user, order
13
- Relation.add user2, order
14
- Relation.add user2, order2
14
+ Relation.add_raw :rel, u_id, o_id
15
+ Relation.add_raw :rel, u2_id, o_id
16
+ Relation.add_raw :rel, u2_id, o2_id
15
17
  end
16
18
 
17
19
  def teardown
18
20
  DB.teardown
19
21
  end
20
22
 
21
- it "should add a connection" do
23
+ it "should add a relation" do
22
24
  assert_difference('Relation.count') do
23
- Relation.add user2, user
25
+ Relation.add_raw :raw, u2_id, u_id
24
26
  end
25
27
  end
26
28
 
27
- it "should delete a connection" do
28
- Relation.add user2, user
29
+ it "should delete a relation" do
30
+ Relation.add_raw :raw, u2_id, u_id
29
31
  assert_difference('Relation.count', -1) do
30
- Relation.delete user2, user
32
+ Relation.delete_raw :raw, u2_id, u_id
31
33
  end
32
34
  end
33
35
 
34
- it "should return references (using class name)" do
35
- arr = Relation.references(user, 'Order')
36
- assert_equal [order].sort, arr.sort
36
+ it "should return referenced ids" do
37
+ arr = Relation.references_raw(:rel, u_id)
38
+ assert_equal [o_id].sort, arr.sort
37
39
 
38
- arr = Relation.references(user2, 'Order')
39
- assert_equal [order, order2].sort, arr.sort
40
+ arr = Relation.references_raw(:rel, u2_id)
41
+ assert_equal [o_id, o2_id].sort, arr.sort
40
42
  end
41
43
 
42
- it "should return references (using class)" do
43
- arr = Relation.references(user, Order)
44
- assert_equal [order].sort, arr.sort
45
- end
46
-
47
- it "should return followers (using class name)" do
48
- arr = Relation.followers('User', order)
49
- assert_equal [user, user2].sort, arr.sort
50
-
51
- arr = Relation.followers('User', order2)
52
- assert_equal [user2].sort, arr.sort
53
- end
44
+ it "should return followers ids" do
45
+ arr = Relation.followers_raw(:rel, o_id)
46
+ assert_equal [u_id, u2_id].sort, arr.sort
54
47
 
55
- it "should return followers (using class)" do
56
- arr = Relation.followers(User, order)
57
- assert_equal [user, user2].sort, arr.sort
48
+ arr = Relation.followers_raw(:rel, o2_id)
49
+ assert_equal [u2_id].sort, arr.sort
58
50
  end
59
51
 
60
52
  it "should not add twice the same connection" do
61
53
  assert_difference('Relation.count') do
62
- Relation.add user2, user
63
- Relation.add user2, user
54
+ Relation.add_raw :rel, u2_id, unknown_id
55
+ Relation.add_raw :rel, u2_id, unknown_id
64
56
  end
65
57
  end
66
58
 
67
59
  it "should handle unexistent connection" do
68
60
  assert_difference('Relation.count', 0) do
69
- Relation.delete user2, user
61
+ Relation.delete_raw :rel, u2_id, unknown_id
70
62
  end
71
63
  end
72
64
 
@@ -55,3 +55,5 @@ class User < ActiveRecord::Base
55
55
  end
56
56
 
57
57
  require_relative ('../../app/models/relation')
58
+ require_relative ('../../app/models/relation_ext')
59
+ require_relative ('../../app/models/dangling')
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: relation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dittmar Krall
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-02-22 00:00:00.000000000 Z
11
+ date: 2018-02-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -127,7 +127,9 @@ files:
127
127
  - MIT-LICENSE
128
128
  - README.md
129
129
  - Rakefile
130
+ - app/models/dangling.rb
130
131
  - app/models/relation.rb
132
+ - app/models/relation_ext.rb
131
133
  - db/migrate/20150810152808_relation.rb
132
134
  - gemfiles/rails_4.gemfile
133
135
  - gemfiles/rails_5.0.gemfile
@@ -136,6 +138,7 @@ files:
136
138
  - lib/relation/version.rb
137
139
  - relation.gemspec
138
140
  - test/dangling_test.rb
141
+ - test/relation_ext_test.rb
139
142
  - test/relation_test.rb
140
143
  - test/support/database.rb
141
144
  - test/test_helper.rb