watcher_data 0.0.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/lib/watcher.rb +132 -0
- metadata +45 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7b21e6142612ea22dea7f3a69a90948fd924c6ca
|
4
|
+
data.tar.gz: f9f0df762e468d63cc917e037238674062f3964a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c0bbdb7f28cd587a5562536c8c368451aa40ac2ebfd64ee4ce2a4b25129bdde9c263a5418058458b91864e9d5e002e2895a802b83594430b39ec2e89c00d765d
|
7
|
+
data.tar.gz: 63010dc009605213f9cf8a85103e7d6a916e87d88208b8371c13859551adbaca33c887880895544aab4260ab81930fb0b40976d967ed14d63bacced72446aa81
|
data/lib/watcher.rb
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
class Watcher
|
2
|
+
attr_accessor :models
|
3
|
+
def initialize(models)
|
4
|
+
self.models = models
|
5
|
+
@association_tables = {}
|
6
|
+
end
|
7
|
+
|
8
|
+
def foreign_keys_for(model)
|
9
|
+
model
|
10
|
+
.reflections
|
11
|
+
.select {|reflection_name, reflection| reflection.macro == :belongs_to && !reflection.options[:polymorphic]}
|
12
|
+
.map { |reflection_name, reflection| reflection.foreign_key }
|
13
|
+
end
|
14
|
+
|
15
|
+
def serialized_column(column_name, column_type, model, foreign_keys)
|
16
|
+
ret = { name: column_name, type: column_type.type }
|
17
|
+
|
18
|
+
ret[:primary_key] = true if column_name == model.primary_key
|
19
|
+
ret[:foreign_key] = true if foreign_keys.include?(column_name)
|
20
|
+
ret
|
21
|
+
end
|
22
|
+
|
23
|
+
def seriaized_association(reflection_name, reflection)
|
24
|
+
foreign_key_on_foreign_table =
|
25
|
+
case
|
26
|
+
when reflection.options[:through]
|
27
|
+
reflection.source_reflection.macro.in? %i{has_many has_one}
|
28
|
+
else
|
29
|
+
reflection.macro.in? %i{has_many has_one}
|
30
|
+
end
|
31
|
+
return [] if reflection.options[:as]
|
32
|
+
ret = {
|
33
|
+
name: reflection_name,
|
34
|
+
joins: reflection.class_name,
|
35
|
+
foreign_table: reflection.table_name,
|
36
|
+
foreign_key: foreign_key_on_foreign_table ? reflection.foreign_key : reflection.association_primary_key,
|
37
|
+
primary_key: foreign_key_on_foreign_table ? reflection.association_primary_key : reflection.foreign_key,
|
38
|
+
type: reflection.macro,
|
39
|
+
through: reflection.options[:through],
|
40
|
+
inverse_of: reflection.inverse_of.try(:name),
|
41
|
+
}
|
42
|
+
|
43
|
+
sec = nil
|
44
|
+
|
45
|
+
if reflection.macro == :has_and_belongs_to_many
|
46
|
+
ret[:through] = reflection.join_table
|
47
|
+
ret[:type] = :has_many
|
48
|
+
ret[:primary_key] = reflection.association_foreign_key
|
49
|
+
sec = {
|
50
|
+
name: reflection.join_table,
|
51
|
+
joins: reflection.join_table.camelize,
|
52
|
+
foreign_table: reflection.join_table,
|
53
|
+
foreign_key: reflection.foreign_key,
|
54
|
+
primary_key: reflection.association_primary_key,
|
55
|
+
type: :has_many
|
56
|
+
}
|
57
|
+
end
|
58
|
+
|
59
|
+
[ret.compact, sec].compact
|
60
|
+
end
|
61
|
+
|
62
|
+
def serialize_habtam_reflection(model, reflection)
|
63
|
+
name = reflection.join_table.camelize
|
64
|
+
@association_tables[name] = {
|
65
|
+
name: name,
|
66
|
+
table_name: reflection.join_table,
|
67
|
+
columns: [
|
68
|
+
{
|
69
|
+
name: reflection.association_foreign_key,
|
70
|
+
type: :integer,
|
71
|
+
foreign_key: true
|
72
|
+
},
|
73
|
+
{
|
74
|
+
name: reflection.foreign_key,
|
75
|
+
type: :integer,
|
76
|
+
foreign_key: true
|
77
|
+
}
|
78
|
+
],
|
79
|
+
associations: [
|
80
|
+
{
|
81
|
+
name: reflection.class_name.underscore,
|
82
|
+
joins: reflection.class_name,
|
83
|
+
foreign_table: reflection.table_name,
|
84
|
+
foreign_key: reflection.association_primary_key,
|
85
|
+
primary_key: reflection.association_foreign_key,
|
86
|
+
type: :belongs_to
|
87
|
+
},
|
88
|
+
{
|
89
|
+
name: model.to_s.underscore,
|
90
|
+
joins: model.to_s,
|
91
|
+
foreign_table: model.table_name,
|
92
|
+
foreign_key: reflection.association_primary_key,
|
93
|
+
primary_key: reflection.foreign_key,
|
94
|
+
type: :belongs_to
|
95
|
+
}
|
96
|
+
]
|
97
|
+
}
|
98
|
+
end
|
99
|
+
|
100
|
+
def serializable_hash
|
101
|
+
models.map do |model|
|
102
|
+
foreign_keys = foreign_keys_for(model)
|
103
|
+
# find association tables
|
104
|
+
model
|
105
|
+
.reflections
|
106
|
+
.select { |_, reflection| reflection.class_name.in?(models.map(&:to_s)) && reflection.macro == :has_and_belongs_to_many }
|
107
|
+
.each { |reflection_name, reflection| serialize_habtam_reflection(model, reflection) }
|
108
|
+
{
|
109
|
+
name: model.to_s,
|
110
|
+
table_name: model.table_name,
|
111
|
+
columns:
|
112
|
+
model
|
113
|
+
.column_types
|
114
|
+
.map { |column_name, column_type| serialized_column(column_name, column_type, model, foreign_keys) },
|
115
|
+
associations:
|
116
|
+
model
|
117
|
+
.reflections
|
118
|
+
.select { |_, reflection| reflection.class_name.in?(models.map(&:to_s)) }
|
119
|
+
.flat_map { |reflection_name, reflection| seriaized_association(reflection_name, reflection) }
|
120
|
+
}
|
121
|
+
end + @association_tables.values
|
122
|
+
end
|
123
|
+
|
124
|
+
def plurality_for(reflection)
|
125
|
+
case reflection.macro
|
126
|
+
when :belongs_to, :has_one
|
127
|
+
false
|
128
|
+
else
|
129
|
+
true
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: watcher_data
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nam Chu Hoai
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-10-01 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Watcher Data
|
14
|
+
email: nambrot@googlemail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/watcher.rb
|
20
|
+
homepage: http://rubygems.org/gems/watcher_data
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.6.7
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: Watcher Data
|
44
|
+
test_files: []
|
45
|
+
has_rdoc:
|