tree_hierarchy 1.0.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: db5dc5ed6f2dbd7d4ed8a3e4e2a9b3e05aef7f73
4
+ data.tar.gz: 5ab59d283cb8cd8f6120d76678397633f91ca6a1
5
+ SHA512:
6
+ metadata.gz: 1811c27470218f21c6d07e3f6aa73b8649a88cd07eb2bb4aa1b64cf7abe03e948714bac414a5185ca737b358a4636636e8da215bfd8d82070ad50bd94ea6861f
7
+ data.tar.gz: b6534b6803544ff2957456a79870b3181778f131e460b6259a36701a37a3fda0a7bde9204d5af4872e514d6ea36968157a2147f624bef99022654f270198c44f
@@ -0,0 +1,11 @@
1
+ module SupportMethod
2
+
3
+ def group_result_set(result_set)
4
+ result_set.group_by(&:parent_id)
5
+ end
6
+
7
+ def total_calc(result_group,field_set)
8
+ result_group.sum{|x| x[field_set].to_i}
9
+ end
10
+
11
+ end
@@ -0,0 +1,76 @@
1
+ module AddedHierarchyLevel
2
+
3
+ @@result_group = []
4
+
5
+ def added_hierarchy(result_set,depth = 0,*args)
6
+ @@result_group = group_result_set(result_set)
7
+ parent_child_add(args)
8
+ (depth == 0) ? self.tree_structure(result_set,@@result_group) : self.tree_structure_depth(result_set,@@result_group,depth.to_i)
9
+ end
10
+
11
+ def added_hierarchy_level(result_set,depth = 0, *args)
12
+ @@result_group = group_result_set(result_set)
13
+ parent_child_add(args)
14
+ (depth == 0) ? self.tree_structure_level(result_set,@@result_group) : self.tree_structure_depth_level(result_set,@@result_group,depth.to_i)
15
+ end
16
+
17
+ def parent_child_add(field_set)
18
+ case field_set.length
19
+ when 0
20
+ when 1
21
+ calc_of_parent_one(field_set)
22
+ when 2
23
+ calc_of_parent_two(field_set)
24
+ when 3
25
+ calc_of_parent_three(field_set)
26
+ when 4
27
+ calc_of_parent_four(field_set)
28
+ else
29
+ calc_of_parent_five(field_set)
30
+ end
31
+ end
32
+
33
+ # Code repetiation occurs to avoid unwanted conditions during execution
34
+ # If these codes are combained together, more conditions need to check for each looping and updating of @@result_group become too complex, this affect the performance
35
+
36
+ def calc_of_parent_one(field_set)
37
+ unless @@result_group[self.id].blank?
38
+ @@result_group[self.id].map{|x| x.calc_of_parent_one(field_set)}
39
+ sum_value = total_calc(@@result_group[self.id],field_set[0])
40
+ @@result_group[self.parent_id].map{|x| x.id == self.id && x[field_set[0]] = sum_value}
41
+ end
42
+ end
43
+
44
+ def calc_of_parent_two(field_set)
45
+ unless @@result_group[self.id].blank?
46
+ @@result_group[self.id].map{|x| x.calc_of_parent_two(field_set)}
47
+ sum_value0,sum_value1 = total_calc(@@result_group[self.id],field_set[0]),total_calc(@@result_group[self.id],field_set[1])
48
+ @@result_group[self.parent_id].map{|x| (x.id == self.id) && (x[field_set[0]] = sum_value0) && (x[field_set[1]] = sum_value1)}
49
+ end
50
+ end
51
+
52
+ def calc_of_parent_three(field_set)
53
+ unless @@result_group[self.id].blank?
54
+ @@result_group[self.id].map{|x| x.calc_of_parent_three(field_set)}
55
+ sum_value0,sum_value1,sum_value2 = total_calc(@@result_group[self.id],field_set[0]),total_calc(@@result_group[self.id],field_set[1]),total_calc(@@result_group[self.id],field_set[2])
56
+ @@result_group[self.parent_id].map{|x| (x.id == self.id) && (x[field_set[0]] = sum_value0) && (x[field_set[1]] = sum_value1) && (x[field_set[2]] = sum_value2)}
57
+ end
58
+ end
59
+
60
+ def calc_of_parent_four(field_set)
61
+ unless @@result_group[self.id].blank?
62
+ @@result_group[self.id].map{|x| x.calc_of_parent_four(field_set)}
63
+ sum_value0,sum_value1,sum_value2,sum_value3 = total_calc(@@result_group[self.id],field_set[0]),total_calc(@@result_group[self.id],field_set[1]),total_calc(@@result_group[self.id],field_set[2]),total_calc(@@result_group[self.id],field_set[3])
64
+ @@result_group[self.parent_id].map{|x| (x.id == self.id) && (x[field_set[0]] = sum_value0) && (x[field_set[1]] = sum_value1) && (x[field_set[2]] = sum_value2) && (x[field_set[3]] = sum_value3)}
65
+ end
66
+ end
67
+
68
+ def calc_of_parent_five(field_set)
69
+ unless @@result_group[self.id].blank?
70
+ @@result_group[self.id].map{|x| x.calc_of_parent_five(field_set)}
71
+ sum_value0,sum_value1,sum_value2,sum_value3,sum_value4 = total_calc(@@result_group[self.id],field_set[0]),total_calc(@@result_group[self.id],field_set[1]),total_calc(@@result_group[self.id],field_set[2]),total_calc(@@result_group[self.id],field_set[3]),total_calc(@@result_group[self.id],field_set[4])
72
+ @@result_group[self.parent_id].map{|x| (x.id == self.id) && (x[field_set[0]] = sum_value0) && (x[field_set[1]] = sum_value1) && (x[field_set[2]] = sum_value2) && (x[field_set[3]] = sum_value3) && (x[field_set[4]] = sum_value4)}
73
+ end
74
+ end
75
+
76
+ end
@@ -0,0 +1,16 @@
1
+ module HierarchyNoLevel
2
+
3
+ def hierarchy(result_set,depth = 0)
4
+ result_group = group_result_set(result_set)
5
+ (depth == 0) ? self.tree_structure(result_set,result_group) : self.tree_structure_depth(result_set,result_group,depth.to_i)
6
+ end
7
+
8
+ def tree_structure(result_set,result_group)
9
+ result_group[self.id].blank? ? self.attributes : self.attributes.merge({:children => result_group[self.id].map{|x| x.tree_structure(result_set,result_group)}})
10
+ end
11
+
12
+ def tree_structure_depth(result_set,result_group,depth,increment = 1)
13
+ result_group[self.id].blank? ? self.attributes : self.attributes.merge({:children => result_group[self.id].map{|x| x.tree_structure_depth(result_set,result_group,depth,increment + 1)}}) unless increment > depth
14
+ end
15
+
16
+ end
@@ -0,0 +1,16 @@
1
+ module HierarchyWithLevel
2
+
3
+ def hierarchy_level(result_set,depth = 0)
4
+ result_group = group_result_set(result_set)
5
+ (depth == 0) ? self.tree_structure_level(result_set,result_group) : self.tree_structure_depth_level(result_set,result_group,depth.to_i)
6
+ end
7
+
8
+ def tree_structure_level(result_set,result_group,increment = 1)
9
+ result_group[self.id].blank? ? self.attributes.merge({:level => increment}) : self.attributes.merge({:level => increment, :children => result_group[self.id].map{|x| x.tree_structure_level(result_set,result_group,increment + 1)}})
10
+ end
11
+
12
+ def tree_structure_depth_level(result_set,result_group,depth,increment = 1)
13
+ result_group[self.id].blank? ? self.attributes.merge({:level => increment}) : self.attributes.merge({:level => increment, :children => result_group[self.id].map{|x| x.tree_structure_depth_level(result_set,result_group,depth,increment + 1)}}) unless increment > depth
14
+ end
15
+
16
+ end
@@ -0,0 +1,11 @@
1
+ require "hierarchy/hierarchy_no_level"
2
+ require "hierarchy/hierarchy_with_level"
3
+ require "hierarchy/added_hierarchy_level"
4
+ require "helper/support_method"
5
+
6
+ class ActiveRecord::Base
7
+ include HierarchyNoLevel
8
+ include HierarchyWithLevel
9
+ include AddedHierarchyLevel
10
+ include SupportMethod
11
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tree_hierarchy
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - dheenaINDIAN
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-02 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: " \n Tree_hierarchy allows to form a tree structure based on parent-child
14
+ relationship \n with simple and fast computation with large records. Additional
15
+ feature of updating\n the parent record with the sum of its child value\n \n"
16
+ email: dheenaindian@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/tree_hierarchy.rb
22
+ - lib/hierarchy/hierarchy_no_level.rb
23
+ - lib/hierarchy/hierarchy_with_level.rb
24
+ - lib/hierarchy/added_hierarchy_level.rb
25
+ - lib/helper/support_method.rb
26
+ homepage: https://github.com/dheenaINDIAN/tree_hierarchy.git
27
+ licenses:
28
+ - MIT
29
+ metadata: {}
30
+ post_install_message: Thanks for installing!
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 2.0.3
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: Create a tree structure and update the parent with sum of its child value
50
+ test_files: []