flatten_record 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/flatten_record/meta/associated_attr.rb +4 -4
- data/lib/flatten_record/meta/belongs_to.rb +9 -0
- data/lib/flatten_record/meta/column.rb +5 -19
- data/lib/flatten_record/meta/compute_column.rb +4 -7
- data/lib/flatten_record/meta/id_column.rb +2 -3
- data/lib/flatten_record/meta/method_column.rb +4 -6
- data/lib/flatten_record/meta/node.rb +28 -11
- data/lib/flatten_record/meta/normalized_attr.rb +22 -11
- data/lib/flatten_record/version.rb +1 -1
- data/spec/dummy/log/test.log +17508 -0
- data/spec/lib/flatten_record/flattener/denormalize_spec.rb +140 -0
- data/spec/lib/flatten_record/flattener_spec.rb +0 -40
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3f11d8fa55541889fa6fdcce3dbf9ab49a3e80d1
|
4
|
+
data.tar.gz: fe9feffcbd9da172e4922a8b413451e2bedbe8f5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cd1c791340df638d6e41d4800a3b0ba838df3b3c07f4b91c0902167e7571401097af71e2962658aa29e4b2666743e80c4be2a7c6377c3d67b7c7c1160d0b84ea
|
7
|
+
data.tar.gz: d7a6093c910fffcd069b442d3f4dc3e494383db65341de9c497fdaf4c6fcc4b3eaab674f9f73c3d218e82daacfd781f33c5d1d63e2b360dd9accd85c35768dba
|
@@ -3,7 +3,9 @@ module FlattenRecord
|
|
3
3
|
class AssociatedAttr < NormalizedAttr
|
4
4
|
def initialize(parent, association, association_klass, model)
|
5
5
|
super(parent, association_klass, model)
|
6
|
-
@association =
|
6
|
+
@association = Struct.
|
7
|
+
new(:foreign_key, :name, :options).
|
8
|
+
new(association.foreign_key, association.name, association.options)
|
7
9
|
end
|
8
10
|
|
9
11
|
def denormalize(instance, to_record)
|
@@ -31,10 +33,8 @@ module FlattenRecord
|
|
31
33
|
end
|
32
34
|
|
33
35
|
protected
|
34
|
-
attr_reader :association
|
35
|
-
|
36
36
|
def options
|
37
|
-
association.options
|
37
|
+
@association.options
|
38
38
|
end
|
39
39
|
|
40
40
|
private
|
@@ -5,28 +5,14 @@ module FlattenRecord
|
|
5
5
|
|
6
6
|
def initialize(parent, col, target_model, model)
|
7
7
|
super(parent, target_model, model)
|
8
|
-
@column =
|
8
|
+
@column = Struct.
|
9
|
+
new(:name, :default, :type, :null).
|
10
|
+
new(col.name, col.default, col.type, col.null)
|
9
11
|
end
|
10
12
|
|
11
13
|
def name
|
12
|
-
|
13
|
-
|
14
|
-
end
|
15
|
-
|
16
|
-
def column_prefix(default_name)
|
17
|
-
col_prefix = parent._key.to_s
|
18
|
-
|
19
|
-
if target_model.table_name.to_s == col_prefix ||
|
20
|
-
target_model_name == col_prefix
|
21
|
-
return default_name
|
22
|
-
end
|
23
|
-
|
24
|
-
default_prefix = @custom_prefix || parent.parent.prefix
|
25
|
-
parent_key = parent._key.to_s + "_"
|
26
|
-
model_prefix = target_model_name + "_"
|
27
|
-
|
28
|
-
default_prefix + parent_key + model_prefix + @column.name.to_s
|
29
|
-
end
|
14
|
+
parent.prefix + @column.name.to_s
|
15
|
+
end
|
30
16
|
|
31
17
|
def type
|
32
18
|
@column.type
|
@@ -2,14 +2,16 @@ module FlattenRecord
|
|
2
2
|
module Meta
|
3
3
|
class ComputeColumn < Column
|
4
4
|
def initialize(parent, method, type, target_model, model, options={})
|
5
|
-
@column =
|
5
|
+
@column = Struct.
|
6
|
+
new(:name, :default, :type, :null).
|
7
|
+
new(method, options[:default], :integer, options[:null])
|
8
|
+
|
6
9
|
super(parent, @column, target_model, model)
|
7
10
|
end
|
8
11
|
|
9
12
|
def denormalize(instance, to_record)
|
10
13
|
first_record = to_record.respond_to?(:each) ? to_record.flatten.first : to_record
|
11
14
|
|
12
|
-
#if first_record.class.method_defined?(@column.name)
|
13
15
|
begin
|
14
16
|
to_record = assign_value(to_record, name) do |record|
|
15
17
|
record.send("compute_#{@column.name}".to_sym, instance)
|
@@ -19,11 +21,6 @@ module FlattenRecord
|
|
19
21
|
end
|
20
22
|
to_record
|
21
23
|
end
|
22
|
-
|
23
|
-
private
|
24
|
-
def new_column(col_name, col_default, col_type, not_null)
|
25
|
-
ActiveRecord::ConnectionAdapters::Column.new(col_name, col_default, col_type, not_null)
|
26
|
-
end
|
27
24
|
end
|
28
25
|
end
|
29
26
|
end
|
@@ -8,10 +8,9 @@ module FlattenRecord
|
|
8
8
|
|
9
9
|
def name
|
10
10
|
column_name = super
|
11
|
-
target_name = target_model.name.underscore
|
12
11
|
is_parent_root? ?
|
13
|
-
|
14
|
-
|
12
|
+
target_model_name + "_" + column_name :
|
13
|
+
column_name
|
15
14
|
end
|
16
15
|
|
17
16
|
end
|
@@ -2,13 +2,11 @@ module FlattenRecord
|
|
2
2
|
module Meta
|
3
3
|
class MethodColumn < Column
|
4
4
|
def initialize(parent, method, type, target_model, model, options={})
|
5
|
-
@column =
|
6
|
-
|
7
|
-
|
5
|
+
@column = Struct.
|
6
|
+
new(:name, :default, :type, :null).
|
7
|
+
new(method, options[:default], type, options[:null])
|
8
8
|
|
9
|
-
|
10
|
-
def new_column(col_name, col_default, col_type, not_null)
|
11
|
-
ActiveRecord::ConnectionAdapters::Column.new(col_name, col_default, col_type, not_null)
|
9
|
+
super(parent, @column, target_model, model)
|
12
10
|
end
|
13
11
|
end
|
14
12
|
end
|
@@ -5,13 +5,20 @@ module FlattenRecord
|
|
5
5
|
|
6
6
|
def initialize(parent, target_model, model)
|
7
7
|
@parent = parent
|
8
|
-
@target_model = target_model.
|
9
|
-
|
10
|
-
|
8
|
+
@target_model = target_model.to_s.underscore
|
9
|
+
@model = model.to_s.underscore
|
10
|
+
end
|
11
|
+
|
12
|
+
def target_model
|
13
|
+
@target_model.camelize.constantize
|
14
|
+
end
|
15
|
+
|
16
|
+
def model
|
17
|
+
@model.camelize.constantize
|
11
18
|
end
|
12
19
|
|
13
20
|
def traverse_by(attr, value)
|
14
|
-
attr_value =
|
21
|
+
attr_value = send("#{attr}")
|
15
22
|
|
16
23
|
if !value.respond_to?(:to_s) || !attr_value.respond_to?(:to_s)
|
17
24
|
raise "traverse error: to_s method required for comparison"
|
@@ -25,35 +32,45 @@ module FlattenRecord
|
|
25
32
|
end
|
26
33
|
|
27
34
|
def prefix
|
28
|
-
return
|
35
|
+
return custom_prefix unless custom_prefix.nil?
|
29
36
|
return "" if is_parent_root?
|
30
37
|
|
31
38
|
"#{target_model_name}_"
|
32
39
|
end
|
33
|
-
|
40
|
+
|
34
41
|
protected
|
35
42
|
def build(definition)
|
36
|
-
@custom_prefix = definition[:definition][:prefix]
|
37
43
|
definition.validates_with(target_model, model)
|
38
44
|
@_key = definition[:_key]
|
39
|
-
|
45
|
+
@custom_prefix = definition[:definition][:prefix]
|
46
|
+
@custom_prefix = @custom_prefix.to_s unless @custom_prefix.nil?
|
47
|
+
|
40
48
|
raise definition.error_message unless definition.valid?
|
41
|
-
|
49
|
+
self
|
42
50
|
end
|
43
|
-
|
51
|
+
|
52
|
+
def custom_prefix
|
53
|
+
@custom_prefix
|
54
|
+
end
|
55
|
+
|
44
56
|
def _key
|
45
57
|
@_key
|
46
58
|
end
|
47
59
|
|
48
60
|
# target helpers
|
49
61
|
def target_model_name
|
50
|
-
target_model
|
62
|
+
@target_model
|
51
63
|
end
|
52
64
|
|
53
65
|
def target_columns
|
54
66
|
target_model.columns
|
55
67
|
end
|
56
68
|
|
69
|
+
def inspect
|
70
|
+
# this prevents irb/console to inspect
|
71
|
+
# circular references on big tree caused problem on #inspect
|
72
|
+
end
|
73
|
+
|
57
74
|
private
|
58
75
|
def is_parent_root?
|
59
76
|
parent.present? && parent.instance_of?(RootNode)
|
@@ -24,11 +24,11 @@ module FlattenRecord
|
|
24
24
|
end
|
25
25
|
|
26
26
|
def prefix
|
27
|
-
|
27
|
+
custom_prefix || "#{parent.prefix}#{target_model_name}_"
|
28
28
|
end
|
29
29
|
|
30
30
|
def traverse_by(attr, value)
|
31
|
-
attr_value =
|
31
|
+
attr_value = send("#{attr}")
|
32
32
|
|
33
33
|
if !value.respond_to?(:to_s) || !attr_value.respond_to?(:to_s)
|
34
34
|
raise "traverse error: to_s method required for comparison"
|
@@ -58,7 +58,7 @@ module FlattenRecord
|
|
58
58
|
end
|
59
59
|
|
60
60
|
def build(definition)
|
61
|
-
|
61
|
+
super(definition)
|
62
62
|
|
63
63
|
primary_key = target_columns.select(&:primary).first
|
64
64
|
@id_column = IdColumn.new(self, primary_key, target_model, model)
|
@@ -73,6 +73,7 @@ module FlattenRecord
|
|
73
73
|
if !dups.blank?
|
74
74
|
raise "#{@excluded_columns.inspect}Duplicate columns found: #{dups.join(", ")}"
|
75
75
|
end
|
76
|
+
self
|
76
77
|
end
|
77
78
|
|
78
79
|
def children
|
@@ -91,10 +92,12 @@ module FlattenRecord
|
|
91
92
|
options = {}
|
92
93
|
if type.is_a?(Hash)
|
93
94
|
options = type
|
94
|
-
type = options[:
|
95
|
+
type = options[:type]
|
95
96
|
end
|
96
97
|
|
97
|
-
ComputeColumn.
|
98
|
+
ComputeColumn.
|
99
|
+
new(self, method, type, target_model, model, options).
|
100
|
+
build(definition)
|
98
101
|
end
|
99
102
|
end
|
100
103
|
|
@@ -108,15 +111,19 @@ module FlattenRecord
|
|
108
111
|
type = options[:sql_type]
|
109
112
|
end
|
110
113
|
|
111
|
-
MethodColumn.
|
114
|
+
MethodColumn.
|
115
|
+
new(self, method, type, target_model, model).
|
116
|
+
build(definition)
|
112
117
|
end
|
113
118
|
end
|
114
119
|
|
115
120
|
def build_children(definition)
|
116
121
|
return {} unless definition[:include]
|
117
|
-
|
122
|
+
|
123
|
+
children = {}
|
118
124
|
definition[:include].each do |child, child_definition|
|
119
|
-
|
125
|
+
class_name = child_definition[:definition][:class_name]
|
126
|
+
children[child] = associated_node_factory(self, child, class_name)
|
120
127
|
children[child].build(child_definition)
|
121
128
|
end
|
122
129
|
children
|
@@ -137,7 +144,7 @@ module FlattenRecord
|
|
137
144
|
dups
|
138
145
|
end
|
139
146
|
|
140
|
-
def
|
147
|
+
def associated_node_factory(parent, child, class_name)
|
141
148
|
klass_map = [:has_many, :belongs_to, :has_one]
|
142
149
|
|
143
150
|
association = target_model.reflect_on_association(child)
|
@@ -150,7 +157,7 @@ module FlattenRecord
|
|
150
157
|
klass = association.macro.to_s.camelize.to_sym
|
151
158
|
node = Meta.const_get(klass).new(parent, association, class_name, model)
|
152
159
|
elsif associaton.macro.nil?
|
153
|
-
raise "association with '#{child}' on #{
|
160
|
+
raise "association with '#{child}' on #{target_name} is not found"
|
154
161
|
else
|
155
162
|
raise "association type '#{association.macro}' with '#{child}' is not supported"
|
156
163
|
end
|
@@ -160,7 +167,11 @@ module FlattenRecord
|
|
160
167
|
def columns_from_definition(definition)
|
161
168
|
target_columns.
|
162
169
|
select {|col| allow_column?(col, definition) }.
|
163
|
-
map
|
170
|
+
map do |col|
|
171
|
+
Column.
|
172
|
+
new(self, col, target_model, model).
|
173
|
+
build(definition)
|
174
|
+
end
|
164
175
|
end
|
165
176
|
|
166
177
|
def allow_column?(col, definition)
|