flatten_record 1.0.1 → 1.0.2

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
  SHA1:
3
- metadata.gz: a7cefc5c625393a11394ff39a01a8a4b20ba9d34
4
- data.tar.gz: e5773cd9b653a3a841b0453eb85941630e603d4c
3
+ metadata.gz: 3f11d8fa55541889fa6fdcce3dbf9ab49a3e80d1
4
+ data.tar.gz: fe9feffcbd9da172e4922a8b413451e2bedbe8f5
5
5
  SHA512:
6
- metadata.gz: b8027b8cadd6ad8141f19deaa5d17b8c171fca14b9de80487be15c83b1bdbeae7672e56ea9f840334af8ed411fa406b3ed1ae8d36b974b6380292caa00e7f3b5
7
- data.tar.gz: eedc792a0b0c056f25f7d2c0c677d3eb464793ebd272f259b2076faa19b86eefd8a72eeb73b201e093daebd89089145cb3187b91be21b02801e0d732960ffc6c
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 = 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
@@ -1,6 +1,15 @@
1
1
  module FlattenRecord
2
2
  module Meta
3
3
  class BelongsTo < AssociatedAttr
4
+ def prefix
5
+ if options[:polymorphic]
6
+ custom_prefix ||
7
+ "#{parent.prefix}#{_key.to_s}_#{target_model_name}_"
8
+ else
9
+ super
10
+ end
11
+ end
12
+
4
13
  end
5
14
  end
6
15
  end
@@ -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 = col
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
- default_name = parent.prefix + @column.name.to_s
13
- is_parent_root? ? default_name : column_prefix(default_name)
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 = new_column(method, options[:default], :integer, options[:not_null])
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
- target_name + "_" + column_name :
14
- column_prefix(column_name)
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 = new_column(method, options[:default], type, options[:not_null])
6
- super(parent, @column, target_model, model)
7
- end
5
+ @column = Struct.
6
+ new(:name, :default, :type, :null).
7
+ new(method, options[:default], type, options[:null])
8
8
 
9
- private
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.is_a?(ActiveRecord::Base) ?
9
- target_model : target_model.to_s.camelize.constantize
10
- @model = model
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 = instance_variable_get("@#{attr}")
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 @custom_prefix unless @custom_prefix.nil?
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
- definition
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.name.underscore
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
- @custom_prefix || parent.prefix+target_model.name.underscore.to_s + "_"
27
+ custom_prefix || "#{parent.prefix}#{target_model_name}_"
28
28
  end
29
29
 
30
30
  def traverse_by(attr, value)
31
- attr_value = instance_variable_get("@#{attr}")
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
- definition = super(definition)
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[:sql_type]
95
+ type = options[:type]
95
96
  end
96
97
 
97
- ComputeColumn.new(self, method, type, target_model, model, options)
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.new(self, method, type, target_model, model)
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
- children = Hash.new
122
+
123
+ children = {}
118
124
  definition[:include].each do |child, child_definition|
119
- children[child] = node_factory(self, child, child_definition[:definition][:class_name])
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 node_factory(parent, child, class_name)
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 #{target_model.name.underscore} is not found"
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 {|col| Column.new(self, col, target_model, model)}
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)
@@ -1,3 +1,3 @@
1
1
  module FlattenRecord
2
- VERSION = "1.0.1"
2
+ VERSION = "1.0.2"
3
3
  end