object_graph_traverser 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b1c832a8577d709ac324e78bacbd57656708845a
4
+ data.tar.gz: 17440f93f631733cc13103c3930e130c302f314b
5
+ SHA512:
6
+ metadata.gz: 931b04d08b333ca185a0070aeae0728dbdffe9280ff8c8d1ff4af8572185e402542cd828995bd0ab0d555f9a8fa11a4d4748bba0bc88141f45f28d41f29dc2c1
7
+ data.tar.gz: c788739d6a3762d8765a1fa024faa64778d58c161ae9d0e9f0c6bfb72cc89826c88b87dd99e4c21709d67917ad0da6bd30466a148c6290ca0e0ad5f507e94c8d
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", "~> 3.2.0"
10
+ gem "rdoc", "~> 3.12"
11
+ gem "bundler", "~> 1.0"
12
+ gem "jeweler", "~> 2.0.1"
13
+ gem "simplecov", ">= 0"
14
+ gem "pry"
15
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,86 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ addressable (2.3.7)
5
+ builder (3.2.2)
6
+ coderay (1.1.0)
7
+ descendants_tracker (0.0.4)
8
+ thread_safe (~> 0.3, >= 0.3.1)
9
+ diff-lcs (1.2.5)
10
+ docile (1.1.5)
11
+ faraday (0.9.1)
12
+ multipart-post (>= 1.2, < 3)
13
+ git (1.2.9.1)
14
+ github_api (0.12.3)
15
+ addressable (~> 2.3)
16
+ descendants_tracker (~> 0.0.4)
17
+ faraday (~> 0.8, < 0.10)
18
+ hashie (>= 3.3)
19
+ multi_json (>= 1.7.5, < 2.0)
20
+ nokogiri (~> 1.6.3)
21
+ oauth2
22
+ hashie (3.4.0)
23
+ highline (1.7.0)
24
+ jeweler (2.0.1)
25
+ builder
26
+ bundler (>= 1.0)
27
+ git (>= 1.2.5)
28
+ github_api
29
+ highline (>= 1.6.15)
30
+ nokogiri (>= 1.5.10)
31
+ rake
32
+ rdoc
33
+ json (1.8.2)
34
+ jwt (1.2.1)
35
+ method_source (0.8.2)
36
+ mini_portile (0.6.2)
37
+ multi_json (1.10.1)
38
+ multi_xml (0.5.5)
39
+ multipart-post (2.0.0)
40
+ nokogiri (1.6.6.2)
41
+ mini_portile (~> 0.6.0)
42
+ oauth2 (1.0.0)
43
+ faraday (>= 0.8, < 0.10)
44
+ jwt (~> 1.0)
45
+ multi_json (~> 1.3)
46
+ multi_xml (~> 0.5)
47
+ rack (~> 1.2)
48
+ pry (0.10.1)
49
+ coderay (~> 1.1.0)
50
+ method_source (~> 0.8.1)
51
+ slop (~> 3.4)
52
+ rack (1.6.0)
53
+ rake (10.4.2)
54
+ rdoc (3.12.2)
55
+ json (~> 1.4)
56
+ rspec (3.2.0)
57
+ rspec-core (~> 3.2.0)
58
+ rspec-expectations (~> 3.2.0)
59
+ rspec-mocks (~> 3.2.0)
60
+ rspec-core (3.2.1)
61
+ rspec-support (~> 3.2.0)
62
+ rspec-expectations (3.2.0)
63
+ diff-lcs (>= 1.2.0, < 2.0)
64
+ rspec-support (~> 3.2.0)
65
+ rspec-mocks (3.2.1)
66
+ diff-lcs (>= 1.2.0, < 2.0)
67
+ rspec-support (~> 3.2.0)
68
+ rspec-support (3.2.2)
69
+ simplecov (0.9.2)
70
+ docile (~> 1.1.0)
71
+ multi_json (~> 1.0)
72
+ simplecov-html (~> 0.9.0)
73
+ simplecov-html (0.9.0)
74
+ slop (3.6.0)
75
+ thread_safe (0.3.4)
76
+
77
+ PLATFORMS
78
+ ruby
79
+
80
+ DEPENDENCIES
81
+ bundler (~> 1.0)
82
+ jeweler (~> 2.0.1)
83
+ pry
84
+ rdoc (~> 3.12)
85
+ rspec (~> 3.2.0)
86
+ simplecov
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2015 Optoro
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,57 @@
1
+ object_graph_traverser
2
+ ======================
3
+
4
+ This gem will allow you traverse an object graph, executing arbitrary code at each node.
5
+
6
+ Example:
7
+
8
+ ```ruby
9
+ class Team
10
+ attr_accessor :name, :players
11
+
12
+ def initialize(name)
13
+ @name = name
14
+ end
15
+ end
16
+
17
+ class Player < Team; end
18
+
19
+ foo = Team.new('Foo')
20
+ bob = Player.new('Bob')
21
+ jane = Player.new('Jane')
22
+
23
+ foo.players = [bob, jane]
24
+
25
+ ogt = ObjectGraphTraverser.new('Team' => ['players'])
26
+
27
+ names = []
28
+
29
+ ogt.traverse_graph(foo) do |obj, parent, nodes|
30
+ names << obj.name
31
+ end
32
+
33
+ names # ["Foo", "Bob", "Jane"]
34
+ ```
35
+
36
+ Contributing to object_graph_traverser
37
+ --------------------------------------
38
+
39
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
40
+
41
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
42
+
43
+ * Fork the project.
44
+
45
+ * Start a feature/bugfix branch.
46
+
47
+ * Commit and push until you are happy with your contribution.
48
+
49
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
50
+
51
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
52
+
53
+
54
+ Copyright
55
+ ---------
56
+
57
+ Copyright © 2015 Optoro. See LICENSE.txt for further details.
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://guides.rubygems.org/specification-reference/ for more options
17
+ gem.name = "object_graph_traverser"
18
+ gem.homepage = "http://github.com/optoro/object_graph_traverser"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{A small class for traversing object graphs.}
21
+ gem.description = %Q{A small class for traversing object graphs.}
22
+ gem.authors = ["Jeffrey Gillis", "Ben Smith"]
23
+ # dependencies defined in Gemfile
24
+ end
25
+ Jeweler::RubygemsDotOrgTasks.new
26
+
27
+ require 'rspec/core'
28
+ require 'rspec/core/rake_task'
29
+ RSpec::Core::RakeTask.new(:spec) do |spec|
30
+ spec.pattern = FileList['spec/**/*_spec.rb']
31
+ end
32
+
33
+ desc "Code coverage detail"
34
+ task :simplecov do
35
+ ENV['COVERAGE'] = "true"
36
+ Rake::Task['spec'].execute
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rdoc/task'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "object_graph_traverser #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,36 @@
1
+ class ObjectGraphTraverser
2
+
3
+ # Params:
4
+ # +object_graph+:: a hash of class name => association pairs, e.g.
5
+ # { 'Foo' => ['bars'], 'Bar' => ['baz'] }
6
+ def initialize(object_graph)
7
+ @graph = object_graph
8
+ end
9
+
10
+ # Given a starting object, traverses the graph calling the given block on each node.
11
+ #
12
+ # Params:
13
+ # +object+:: root object from which to start traversing
14
+ # +parent+:: optional parent object
15
+ # +block+:: a block that will be called on each node, receiving the node, its parent and the graph of objects under the current node.
16
+ def traverse_graph(object,parent=nil,&block)
17
+ nodes = fetch_object_graph(object.class).map do |node|
18
+ Array(object.send(node.to_sym)).each do |node_object|
19
+ traverse_graph(node_object,object,&block)
20
+ end
21
+ node
22
+ end
23
+ block.call(object,parent,nodes)
24
+ end
25
+
26
+ private
27
+
28
+ def fetch_object_graph(klass)
29
+ nodes = @graph.fetch(klass.name) {[]}
30
+ if nodes.empty? && klass.superclass
31
+ nodes = fetch_object_graph(klass.superclass)
32
+ end
33
+ nodes
34
+ end
35
+
36
+ end
@@ -0,0 +1,69 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+ # stub: object_graph_traverser 1.0.0 ruby lib
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = "object_graph_traverser"
9
+ s.version = "1.0.0"
10
+
11
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
+ s.require_paths = ["lib"]
13
+ s.authors = ["Jeffrey Gillis", "Ben Smith"]
14
+ s.date = "2015-02-24"
15
+ s.description = "A small class for traversing object graphs."
16
+ s.extra_rdoc_files = [
17
+ "LICENSE.txt",
18
+ "README.md"
19
+ ]
20
+ s.files = [
21
+ ".document",
22
+ ".rspec",
23
+ "Gemfile",
24
+ "Gemfile.lock",
25
+ "LICENSE.txt",
26
+ "README.md",
27
+ "Rakefile",
28
+ "VERSION",
29
+ "lib/object_graph_traverser.rb",
30
+ "object_graph_traverser.gemspec",
31
+ "spec/object_graph_traverser_spec.rb",
32
+ "spec/spec_helper.rb",
33
+ "spec/support/bar.rb",
34
+ "spec/support/baz.rb",
35
+ "spec/support/foo.rb"
36
+ ]
37
+ s.homepage = "http://github.com/optoro/object_graph_traverser"
38
+ s.licenses = ["MIT"]
39
+ s.rubygems_version = "2.4.5"
40
+ s.summary = "A small class for traversing object graphs."
41
+
42
+ if s.respond_to? :specification_version then
43
+ s.specification_version = 4
44
+
45
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
46
+ s.add_development_dependency(%q<rspec>, ["~> 3.2.0"])
47
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
48
+ s.add_development_dependency(%q<bundler>, ["~> 1.0"])
49
+ s.add_development_dependency(%q<jeweler>, ["~> 2.0.1"])
50
+ s.add_development_dependency(%q<simplecov>, [">= 0"])
51
+ s.add_development_dependency(%q<pry>, [">= 0"])
52
+ else
53
+ s.add_dependency(%q<rspec>, ["~> 3.2.0"])
54
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
55
+ s.add_dependency(%q<bundler>, ["~> 1.0"])
56
+ s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
57
+ s.add_dependency(%q<simplecov>, [">= 0"])
58
+ s.add_dependency(%q<pry>, [">= 0"])
59
+ end
60
+ else
61
+ s.add_dependency(%q<rspec>, ["~> 3.2.0"])
62
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
63
+ s.add_dependency(%q<bundler>, ["~> 1.0"])
64
+ s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
65
+ s.add_dependency(%q<simplecov>, [">= 0"])
66
+ s.add_dependency(%q<pry>, [">= 0"])
67
+ end
68
+ end
69
+
@@ -0,0 +1,98 @@
1
+ require 'spec_helper'
2
+
3
+ describe ObjectGraphTraverser do
4
+ let(:graph) do
5
+ {
6
+ "Foo" => [
7
+ "bars"
8
+ ],
9
+ "Bar" => [
10
+ "baz"
11
+ ]
12
+ }
13
+ end
14
+
15
+ let(:foo) do
16
+ Foo.new
17
+ end
18
+
19
+ let(:traverser) do
20
+ ObjectGraphTraverser.new(graph)
21
+ end
22
+
23
+ describe "visited objects" do
24
+ subject do
25
+ visited = []
26
+ traverser.traverse_graph(foo) do |obj, parent, nodes|
27
+ visited << obj
28
+ end
29
+ visited
30
+ end
31
+
32
+ it "visited some nodes" do
33
+ expect(subject.length > 3)
34
+ end
35
+
36
+ it "visits the foo" do
37
+ expect(subject.include?(foo)).to eq(true)
38
+ end
39
+
40
+ it "visits the foo's bars" do
41
+ foo.bars.each do |bar|
42
+ expect(subject.include?(bar)).to eq(true)
43
+ end
44
+ end
45
+
46
+ it "visits the foo's bars' bazs" do
47
+ foo.bars.each do |bar|
48
+ expect(subject.include?(bar.baz)).to eq(true)
49
+ end
50
+ end
51
+ end
52
+
53
+ describe "parents of visited objects" do
54
+ subject do
55
+ parents = []
56
+ traverser.traverse_graph(foo) do |obj, parent, nodes|
57
+ parents << parent
58
+ end
59
+ parents
60
+ end
61
+
62
+ it "contain the root node" do
63
+ expect(subject).to include(foo)
64
+ end
65
+
66
+ it "contains the middle nodes" do
67
+ foo.bars.each do |bar|
68
+ expect(subject).to include(bar)
69
+ end
70
+ end
71
+
72
+ it "doesn't contain the leaves" do
73
+ foo.bars.each do |bar|
74
+ expect(subject).not_to include(bar.baz)
75
+ end
76
+ end
77
+ end
78
+
79
+ describe "nodes (child associations)" do
80
+ subject do
81
+ all_nodes = []
82
+ traverser.traverse_graph(foo) do |obj, parent, nodes|
83
+ all_nodes << nodes
84
+ end
85
+ all_nodes.flatten
86
+ end
87
+
88
+ it "contains Strings" do
89
+ expect(subject.first).to be_a(String)
90
+ end
91
+
92
+ it "has the name of the association of an object to its child" do
93
+ %w[bars baz].each do |name|
94
+ expect(subject).to include(name)
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,29 @@
1
+ require 'simplecov'
2
+
3
+ module SimpleCov::Configuration
4
+ def clean_filters
5
+ @filters = []
6
+ end
7
+ end
8
+
9
+ SimpleCov.configure do
10
+ clean_filters
11
+ load_adapter 'test_frameworks'
12
+ end
13
+
14
+ ENV["COVERAGE"] && SimpleCov.start do
15
+ add_filter "/.rvm/"
16
+ end
17
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
18
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
19
+
20
+ require 'rspec'
21
+ require 'object_graph_traverser'
22
+
23
+ # Requires supporting files with custom matchers and macros, etc,
24
+ # in ./support/ and its subdirectories.
25
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
26
+
27
+ RSpec.configure do |config|
28
+
29
+ end
@@ -0,0 +1,7 @@
1
+ class Bar
2
+ attr_reader :baz
3
+
4
+ def initialize
5
+ @baz = Baz.new
6
+ end
7
+ end
@@ -0,0 +1,2 @@
1
+ class Baz
2
+ end
@@ -0,0 +1,7 @@
1
+ class Foo
2
+ attr_reader :bars
3
+
4
+ def initialize
5
+ @bars = [Bar.new, Bar.new]
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: object_graph_traverser
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jeffrey Gillis
8
+ - Ben Smith
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-02-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: 3.2.0
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: 3.2.0
28
+ - !ruby/object:Gem::Dependency
29
+ name: rdoc
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '3.12'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '3.12'
42
+ - !ruby/object:Gem::Dependency
43
+ name: bundler
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '1.0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '1.0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: jeweler
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: 2.0.1
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: 2.0.1
70
+ - !ruby/object:Gem::Dependency
71
+ name: simplecov
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: pry
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ description: A small class for traversing object graphs.
99
+ email:
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files:
103
+ - LICENSE.txt
104
+ - README.md
105
+ files:
106
+ - ".document"
107
+ - ".rspec"
108
+ - Gemfile
109
+ - Gemfile.lock
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - VERSION
114
+ - lib/object_graph_traverser.rb
115
+ - object_graph_traverser.gemspec
116
+ - spec/object_graph_traverser_spec.rb
117
+ - spec/spec_helper.rb
118
+ - spec/support/bar.rb
119
+ - spec/support/baz.rb
120
+ - spec/support/foo.rb
121
+ homepage: http://github.com/optoro/object_graph_traverser
122
+ licenses:
123
+ - MIT
124
+ metadata: {}
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ requirements: []
140
+ rubyforge_project:
141
+ rubygems_version: 2.4.5
142
+ signing_key:
143
+ specification_version: 4
144
+ summary: A small class for traversing object graphs.
145
+ test_files: []