acts_as_graph_vertex 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +80 -0
  3. data/lib/acts_as_graph_vertex.rb +75 -0
  4. metadata +131 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c77bf69f9ed79801c8a55148fc1448a4af1f9b27
4
+ data.tar.gz: d38f2b2cd7290bf95e61e5fe743cf97aa099b248
5
+ SHA512:
6
+ metadata.gz: a765c73435a316e9b14aedfd86b00b70c43bb4676f4f90ddd2a5f360c76ce6967da0f87652ddc5d42d3a5d4132623b138fa4d6e0cf55a6ff2b6239669f0d1115
7
+ data.tar.gz: 561414bae337114e3883d6f45d1a5761b5270dc38f04504a966b54b8f88001ba3056699c733d0acee5d2fa404704c665e049473a57e2d2df041278d18e0c0830
data/README.md ADDED
@@ -0,0 +1,80 @@
1
+ # acts_as_graph_vertex [![Build Status](https://travis-ci.org/nathankleyn/acts_as_graph_vertex.svg?branch=master)](https://travis-ci.org/nathankleyn/acts_as_graph_vertex) [![Coverage Status](https://coveralls.io/repos/nathankleyn/acts_as_graph_vertex/badge.png?branch=master)](https://coveralls.io/r/nathankleyn/acts_as_graph_vertex?branch=master)
2
+
3
+ Simple mixin for adding graph like functions (parents, children, traversal, etc) to any class.
4
+
5
+ ## Installing
6
+
7
+ You can install this gem via RubyGems:
8
+
9
+ ```sh
10
+ gem install acts_as_graph_vertex
11
+ ```
12
+
13
+ ## Using
14
+
15
+ Sometimes it's useful to be able to add graph like functionality to existing classes you have. This is where acts_as_graph_vertex comes in! Simply mixin this module, and you'll get basic directed acyclic graph (DAG) functionality for free:
16
+
17
+ ```ruby
18
+ require 'acts_as_graph_vertex'
19
+
20
+ class MyAmazingClass
21
+ include ActsAsGraphVertex
22
+ end
23
+ ```
24
+
25
+ And now you can use the functions:
26
+
27
+ ```ruby
28
+ parent = MyAmazingClass.new
29
+ child1 = MyAmazingClass.new
30
+ child2 = MyAmazingClass.new
31
+ child3 = MyAmazingClass.new
32
+
33
+ # The following calls will create a DAG like the following:
34
+ #
35
+ # --> child1 --
36
+ # / \
37
+ # parent-- --> child3
38
+ # \ /
39
+ # --> child2 --
40
+
41
+ parent.add_child(child1)
42
+ child2.add_parent(child1)
43
+ child3.add_parent(child1)
44
+ child3.add_parent(child2)
45
+ ```
46
+
47
+ You can now traverse the graph from any of these vertices:
48
+
49
+ ```ruby
50
+ parent.children # => [child1, child2]
51
+ parent.all_children # => [child1, child2, child3]
52
+ child3.parents # => [child1, child2]
53
+ child3.all_parents # => [child1, child2, parent]
54
+ ```
55
+
56
+ Note that this mixin does not currently prevent or handle cyclic dependencies; it's intended to be simple rather than exhaustive. If cycle detection is required, using something as a layer over this mixin such as Tarjan's algorithm for finding strongly connected components of a graph is recommended. Tarjan's algorithm is bundled with the Ruby standard library as `TSort` (despite the naming, this class does not provide topographic sorting).
57
+
58
+ ## License
59
+
60
+ The MIT License (MIT)
61
+
62
+ Copyright (c) 2015 Nathan Kleyn
63
+
64
+ Permission is hereby granted, free of charge, to any person obtaining a copy
65
+ of this software and associated documentation files (the "Software"), to deal
66
+ in the Software without restriction, including without limitation the rights
67
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
68
+ copies of the Software, and to permit persons to whom the Software is
69
+ furnished to do so, subject to the following conditions:
70
+
71
+ The above copyright notice and this permission notice shall be included in
72
+ all copies or substantial portions of the Software.
73
+
74
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
75
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
76
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
77
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
78
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
79
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
80
+ THE SOFTWARE.
@@ -0,0 +1,75 @@
1
+ # A mixin module enabling classes to have parents and children. It provides
2
+ # convenience methods for determining dependencies, and depdenants.
3
+ #
4
+ # Note that in contrast to a tree, this graph module allows multiple parents.
5
+ module ActsAsGraphVertex
6
+ # The self.included idiom. This is described in great detail in a
7
+ # fantastic blog post here:
8
+ #
9
+ # http://www.railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/
10
+ #
11
+ # Basically, this idiom allows us to add both instance *and* class methods
12
+ # to the class that is mixing this module into itself without forcing them
13
+ # to call extend and include for this mixin. You'll see this idiom everywhere
14
+ # in the Ruby/Rails world, so we use it too.
15
+ def self.included(cls)
16
+ cls.extend(ClassMethods)
17
+ end
18
+
19
+ # Common methods inherited by all classes
20
+ module ClassMethods
21
+ attr_writer :parents, :children
22
+ end
23
+
24
+ # Public: The parents linked to this instance.
25
+ #
26
+ # Returns an Array of Objects.
27
+ def parents
28
+ @parents ||= []
29
+ end
30
+
31
+ # Public: The children linked to this instance.
32
+ #
33
+ # Returns an Array of Objects.
34
+ def children
35
+ @children ||= []
36
+ end
37
+
38
+ # Public: Add a vertex to the parents linked to this instance. Will automatically add as a child of the given parent
39
+ # if the given parent also mixes in the graph functionality.
40
+ #
41
+ # vertex - The Object to add as a parent.
42
+ # add_child - Whether to add this item as a child of the given parent.
43
+ def add_parent(vertex, add_child = true)
44
+ return if parents.include?(vertex)
45
+ vertex.add_child(self, false) if add_child && vertex.respond_to?(:add_child)
46
+ parents << vertex
47
+ end
48
+
49
+ # Public: Add a vertex to the children linked to this instance. Will automatically add as a parent of the given child
50
+ # if the given child also mixes in the graph functionality.
51
+ #
52
+ # vertex - The Object to add as a child.
53
+ # add_parent - Whether to add this item as a parent of the given child.
54
+ def add_child(vertex, add_parent = true)
55
+ return if children.include?(vertex)
56
+ vertex.add_parent(self, false) if add_parent && vertex.respond_to?(:add_parent)
57
+ children << vertex
58
+ end
59
+
60
+ # Public: Convenience method to return all of the parents from this vertex in
61
+ # the tree upwards to the root of the tree.
62
+ #
63
+ # Returns an Array of parent Objects.
64
+ def all_parents
65
+ ((parents + parents.map(&:all_parents)).flatten).uniq
66
+ end
67
+
68
+ # Public: Convenience method to return all of the children from this vertex in
69
+ # the tree downwards to the leaves of the tree.
70
+ #
71
+ # Returns an Array of child Objects.
72
+ def all_children
73
+ (children + children.map(&:all_children).flatten).uniq
74
+ end
75
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_graph_vertex
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Nathan Kleyn
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: coveralls
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: filewatcher
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry-byebug
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.1'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.28'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.28'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop-rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.2'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.2'
97
+ description: See https://github.com/nathankleyn/acts_as_graph_vertex for more information!
98
+ email:
99
+ - nathan@nathankleyn.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - README.md
105
+ - lib/acts_as_graph_vertex.rb
106
+ homepage: https://github.com/nathankleyn/acts_as_graph_vertex
107
+ licenses:
108
+ - MIT
109
+ metadata: {}
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.4.5
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: Simple mixin for adding graph like functions (parents, children, traversal,
130
+ etc) to any class.
131
+ test_files: []