my_ancestry 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/LICENSE +14 -0
  2. data/README.md +57 -0
  3. data/lib/my_ancestry.rb +89 -0
  4. data/my_ancestry.gemspec +22 -0
  5. metadata +50 -0
data/LICENSE ADDED
@@ -0,0 +1,14 @@
1
+ Copyright (c) 2013 Thiago Coutinho
2
+
3
+ Written by
4
+
5
+ - Thiago Coutinho <thiago@osfeio.com>
6
+
7
+ Version: 0.0.1 stable
8
+
9
+ "THE BEER-WARE LICENSE" (Revision 42):
10
+ <thiago@osfeio.com> wrote this file based on the other descriptions. As long as
11
+ you retain this notice you can do whatever you want with this stuff. If we meet
12
+ some day, and you think this stuff is worth it, you can buy me a beer in return.
13
+
14
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,57 @@
1
+ # MyAncestry
2
+
3
+ Make search of objects parents/childrem/brothers .....
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'my_ancestry'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install my_ancestry
19
+
20
+ ## Usage
21
+
22
+ list = [
23
+ {
24
+ id:1,
25
+ name: "couto",
26
+ ancestry:nil
27
+ },
28
+ {
29
+ id:2,
30
+ name: "teste",
31
+ ancestry:"000001"
32
+ },
33
+ {
34
+ id:3,
35
+ name: "teste2",
36
+ ancestry:"0001"
37
+ }
38
+ ]
39
+
40
+ result = MyAncestry.new(list,{:id=> :id, :ancestry_id => :ancestry})
41
+
42
+ brothers = result.brothers_of(2)
43
+
44
+ childrem = result.childrem_of(1)
45
+
46
+ parent = result.parent_of(3)
47
+ .....
48
+
49
+
50
+ ## Contributing
51
+
52
+ 1. Fork it
53
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
54
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
55
+ 4. Push to the branch (`git push origin my-new-feature`)
56
+ 5. Create new Pull Request
57
+ 6. Execure the "THE BEER-WARE LICENSE".
@@ -0,0 +1,89 @@
1
+ class TypeException < Exception ;end
2
+ class MyAncestry
3
+
4
+ def initialize(objs, options={})
5
+ validate_objs(objs)
6
+ @objs = objs
7
+ load_default_options(options)
8
+ end
9
+
10
+ def load_default_options(options)
11
+ @id = options[:id] || "id"
12
+ @ancestry_id = options[:ancestry_id] || "ancestry_id"
13
+ @objects_type = @objs.first.class.name
14
+ @comparision_type = options[:comparision_type] || "to_i"
15
+ end
16
+
17
+ def all
18
+ @objs
19
+ end
20
+
21
+ def search
22
+ @objs.select do
23
+ yield
24
+ end
25
+ end
26
+
27
+ #Search parent by id of child
28
+ def parent_of(id)
29
+ item = find_by_id(id)
30
+ find_by_id(eval(comparision_childrem_value("item")))
31
+ end
32
+
33
+ #Search all childrem by ancestry_id
34
+ def childrem_of(id)
35
+ @objs.select do |obj|
36
+ eval(comparision_childrem_value("obj")).to_i == id.to_i
37
+ end
38
+ end
39
+
40
+ #Search all brothers of id
41
+ def brothers_of(id)
42
+ parent = parent_of(id)
43
+ childrem_of(eval(comparision_obj_value("parent")))
44
+ end
45
+
46
+ #Search a object by id
47
+ def find_by_id(id)
48
+ @objs.select {|obj| eval(comparision_obj_value("obj")).to_i == id.to_i}.first
49
+ end
50
+
51
+ def validate_objs(objs)
52
+ first_class_name = objs.first.class.name
53
+ objs.each do |obj|
54
+ raise TypeException.new "The list of objets must be a single type" unless first_class_name == obj.class.name
55
+ end
56
+ end
57
+
58
+ private
59
+
60
+ def comparision_childrem_value(var)
61
+ comparision_value(var,@ancestry_id)
62
+ end
63
+
64
+ def comparision_obj_value(var)
65
+ comparision_value(var,@id)
66
+ end
67
+
68
+ def comparision_value(var,id)
69
+ items = {
70
+ "Array" => "#{var}[#{id}]",
71
+ "Hash" => "#{var}[ #{type_value(id)} ]",
72
+ "Symbol" => "#{var}[#{type_value(id)}]",
73
+ }
74
+ if items.key?(@objects_type)
75
+ items[@objects_type]
76
+ else
77
+ "#{var}.#{id}"
78
+ end
79
+ end
80
+
81
+ def type_value(var)
82
+ items ={
83
+ "String" => var,
84
+ "Symbol" => ":#{var}",
85
+ }
86
+ items[var.class.name]
87
+ end
88
+
89
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ Gem::Specification.new do |gem|
3
+ gem.authors = ["Thiago Coutinho"]
4
+ gem.email = ["thiago@osfeio.com"]
5
+ gem.description = %q{
6
+ Make search of objects parents/childrem/brothers}
7
+ gem.summary = %q{ Make search of objects parents/childrem/brothers }
8
+ gem.homepage = "http://github.com/selialkile/myancestry"
9
+
10
+ gem.files = [
11
+ "LICENSE",
12
+ "README.md",
13
+ "my_ancestry.gemspec",
14
+ "lib/my_ancestry.rb",
15
+ ]
16
+
17
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.name = "my_ancestry"
20
+ gem.require_paths = ["lib"]
21
+ gem.version = "0.0.1"
22
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: my_ancestry
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Thiago Coutinho
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-22 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: ! "\n Make search of objects parents/childrem/brothers"
15
+ email:
16
+ - thiago@osfeio.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - LICENSE
22
+ - README.md
23
+ - my_ancestry.gemspec
24
+ - lib/my_ancestry.rb
25
+ homepage: http://github.com/selialkile/myancestry
26
+ licenses: []
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ none: false
33
+ requirements:
34
+ - - ! '>='
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 1.8.15
46
+ signing_key:
47
+ specification_version: 3
48
+ summary: Make search of objects parents/childrem/brothers
49
+ test_files: []
50
+ has_rdoc: