pomona 0.6.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e46fd3f505f3d16d9a6e52dccb81777709281e31
4
+ data.tar.gz: 68c792bc59061bc4885f1184879a8ac059d53b39
5
+ SHA512:
6
+ metadata.gz: 6156620e8aeaf867c94b28cd19ae5c68d7149da1a3b82ce36c2db1ac891be11326ca993b9d7cfd472084c3fae54e71b525e034ef8d9b9775d224d24edd2bdf25
7
+ data.tar.gz: 7fc7ed2ac3c3e316c8c9f9919b776402ca4f7fce5e1ff5caa02f58318d8efb0458578843cdec48d860522903dc3b748b07b54df80c79110a269e1c7d4fb4e9e8
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.12.5
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at elliott.a.young@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pomona.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 ElliottAYoung
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,162 @@
1
+ # Pomona
2
+
3
+ Pomona is a simple, lightweight gem for creating and managing tree data structures in Ruby. It works using a custom data structure (the Tree) to contain small hashes of data (the Nodes) that can be queried and searched.
4
+
5
+ And of course it's a subtle, shameless [Harry Potter reference](http://harrypotter.wikia.com/wiki/Pomona_Sprout).
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'pomona'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install pomona
22
+
23
+ ## Usage
24
+
25
+ ###Basics
26
+
27
+ Using Pomona is simple - to create a new Tree simply run:
28
+
29
+ ```ruby
30
+ tree = Tree.new
31
+
32
+ # Tree is now created as so:
33
+ {
34
+ tree_array: []
35
+ }
36
+ ```
37
+
38
+ Once you have your tree you can simply add any nodes to it by using ```#add_node``` like so:
39
+
40
+ ```ruby
41
+ tree.add_node({ data: "Your Data"})
42
+ ```
43
+ After creation your new Node will automatically have a unique auto-generated id and an empty ```children``` array added to it. It will now look like this:
44
+
45
+ ```ruby
46
+ {
47
+ tree_array: [
48
+ { id: 1, data: "Your Data", children: [] }
49
+ ]
50
+ }
51
+ ```
52
+
53
+ You can now attach any additional (child) Nodes onto the one you've created by simply specifying the id when adding the Node.
54
+
55
+ ```ruby
56
+ tree.add_node({ data: "New Data" }, 1)
57
+ ```
58
+
59
+ This will attach the Node to your previously created one, making the tree look like this:
60
+
61
+ ```ruby
62
+ {
63
+ tree_array: [
64
+ { id: 1, data: "Your Data", children: [
65
+ { id: 2, data: "New Data", children: [] }
66
+ ]}
67
+ ]
68
+ }
69
+ ```
70
+
71
+ ###Searching the Tree
72
+ Given the following example tree:
73
+
74
+ ```ruby
75
+ {
76
+ tree_array: [
77
+ { name: "Rickon Stark", id: 1, children: [
78
+ { name: "Brandon Stark", id: 2, children: [] },
79
+ { name: "Eddard Stark", id: 3, children: [
80
+ { name: "Robb Stark", id: 6, children: [] },
81
+ { name: "Sansa Stark", id: 7, children: [] },
82
+ { name: "Arya Stark", id: 8, children: [] },
83
+ { name: "Bran Stark", id: 9, children: [] },
84
+ { name: "Rickon Stark II", id: 10, children: [] },
85
+ ]
86
+ },
87
+ { name: "Lyanna Stark", id: 4, children: [
88
+ { name: "Jon Snow", id: 11, children: [] },
89
+ ]
90
+ },
91
+ { name: "Benjen Stark", id: 5, children: [] }
92
+ ]
93
+ }
94
+ ]
95
+ }
96
+ ```
97
+
98
+ We can search and aggregate any data from this by using the following commands:
99
+
100
+ #### #find(id)
101
+ Find will return the Node with the id you've passed as a paramater:
102
+
103
+ ```ruby
104
+ tree.find(8)
105
+ { name: "Arya Stark", id: 8, children: [] }
106
+ ```
107
+
108
+ #### #values_at(keys)
109
+ Values_at works very similarly to its Hash counterpart with two primary differences:
110
+
111
+ 1. It is able to take in an array of keys that it will search with, or a singular key you want to find.
112
+ 2. It will return a nested Array if an array of keys is passed, or a flattened array if only one is passed.
113
+
114
+ ```ruby
115
+ # Multiple Keys
116
+
117
+ tree.values_at([:id, :name])
118
+ [
119
+ ["Rickon Stark", 1], ["Brandon Stark", 2], ["Eddard Stark", 3],
120
+ ["Robb Stark", 6], ["Sansa Stark", 7], ["Arya Stark", 8],
121
+ ["Bran Stark", 9], ["Rickon Stark II", 10], ["Lyanna Stark", 4],
122
+ ["Jon Stark", 11], ["Benjen Stark", 5]
123
+ ]
124
+
125
+ # One Key
126
+ tree.values_at(:id)
127
+ [
128
+ 1, 2, 3, 6, 7, 8, 9, 10, 4, 11, 5
129
+ ]
130
+ ```
131
+
132
+ ###Deleteing from the Tree
133
+ To remove a Node from the tree simply use the ```remove_node``` method on your ```Tree``` object, specifying the id of the Node you wish to remove.
134
+
135
+ NOTE: This method will delete any children of the Node you've deleted as well, as they are now orphaned data.
136
+
137
+ ```ruby
138
+ tree.remove_node(3)
139
+
140
+ {
141
+ tree_array: [
142
+ { name: "Rickon Stark", id: 1, children: [
143
+ { name: "Brandon Stark", id: 2, children: [] },
144
+ { name: "Lyanna Stark", id: 4, children: [
145
+ { name: "Jon Snow", id: 11, children: [] },
146
+ ]
147
+ },
148
+ { name: "Benjen Stark", id: 5, children: [] }
149
+ ]
150
+ }
151
+ ]
152
+ }
153
+ ```
154
+
155
+ ## Contributing
156
+
157
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/pomona. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
158
+
159
+
160
+ ## License
161
+
162
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "pomona"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ require "pry"
11
+ Pry.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,2 @@
1
+ require "pomona/version"
2
+ require "pomona/tree"
@@ -0,0 +1,5 @@
1
+ class NodeNotFound < ArgumentError
2
+ def initialize(msg="The ID for a Node you have provided does not exist")
3
+ super
4
+ end
5
+ end
@@ -0,0 +1,25 @@
1
+ require "pomona/exceptions"
2
+
3
+ module Extractor
4
+ def self.get_all_by_keys(tree_array = [], keys = [], values = [])
5
+ tree_array.each do |tree_node|
6
+ values << tree_node.node.values_at(*keys)
7
+ Extractor.get_all_by_keys(tree_node.children, keys, values) if tree_node.children.any?
8
+ end
9
+
10
+ values
11
+ end
12
+
13
+ def self.find_node_by_id(id, tree_array)
14
+ #TODO: Get this working without using an instance variable for target
15
+ tree_array.each do |tree_node|
16
+ if tree_node.id == id
17
+ @target = tree_node
18
+ else
19
+ Extractor.find_node_by_id(id, tree_node.children) if tree_node.children.any?
20
+ end
21
+ end
22
+
23
+ @target.nil? ? (raise NodeNotFound) : (return @target)
24
+ end
25
+ end
@@ -0,0 +1,14 @@
1
+ require "pomona/exceptions"
2
+
3
+ class Node
4
+ attr_reader :id, :parent_id
5
+ attr_accessor :children, :node
6
+
7
+ def initialize(data_hash, id, parent_id)
8
+ @id = id
9
+ node_data = { id: @id, children: [] }
10
+ @node = data_hash.merge(node_data)
11
+ @children = @node[:children]
12
+ @parent_id = parent_id
13
+ end
14
+ end
@@ -0,0 +1,11 @@
1
+ require "pomona/exceptions"
2
+
3
+ module Pruner
4
+ def self.remove_node_and_descendents(node, parent_node)
5
+ if parent_node.kind_of?(Array)
6
+ parent_node.delete_if { |n| n.id == node.id }
7
+ else
8
+ parent_node.children.delete_if { |n| n.id == node.id }
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,72 @@
1
+ require "pomona/node"
2
+ require "pomona/extractor"
3
+ require "pomona/pruner"
4
+ require "pomona/exceptions"
5
+
6
+ class Tree
7
+ attr_reader :data
8
+
9
+ def initialize
10
+ @data = { tree_array: [] }
11
+ end
12
+
13
+ def add_node(node_hash, parent_id = nil)
14
+ #Creates a new node to be attached to a parent
15
+ #Grants a generated id to the node
16
+ node = Node.new(node_hash, next_id, parent_id)
17
+ attach_to_parent(node, parent_id)
18
+ end
19
+
20
+ def remove_node(id)
21
+ #Removes a node from the Tree by its id
22
+ #Removes all of their descendents as well
23
+ node_to_delete = find(id)
24
+ parent_node = find_parent_node(node_to_delete)
25
+ Pruner.remove_node_and_descendents(node_to_delete, parent_node)
26
+ end
27
+
28
+ def values_at(keys)
29
+ #Returns an array of data from the tree based on the given keys
30
+ data = Extractor.get_all_by_keys(@data[:tree_array], keys)
31
+ flatten_output_array(data)
32
+ end
33
+
34
+ def find(id)
35
+ #Finds and returns a Node by its id using the Extractor
36
+ Extractor.find_node_by_id(id, @data[:tree_array])
37
+ end
38
+
39
+ private
40
+
41
+ def next_id
42
+ #Finds the next highest id to put a node in at
43
+ ids = values_at(:id).sort
44
+ ids[-1].nil? ? 1 : (ids[-1] + 1)
45
+ end
46
+
47
+ def attach_to_parent(node, parent_id)
48
+ #Attaches the newly created node to a parent node
49
+ #If parent_id is nil will simply attach it to the top level
50
+ if parent_id.nil?
51
+ @data[:tree_array] << node
52
+ else
53
+ parent = find(parent_id)
54
+ parent.children << node
55
+ end
56
+ end
57
+
58
+ def flatten_output_array(output_array)
59
+ #Flattens the output array of values_at if it only contains data for one key
60
+ return output_array if output_array.empty?
61
+ output_array[0].length == 1 ? output_array.flatten : output_array
62
+ end
63
+
64
+ def find_parent_node(node)
65
+ #Finds the parent node of the given node. If it is a top level node, returns the tree_array instead
66
+ if node.parent_id.nil?
67
+ @data[:tree_array]
68
+ else
69
+ find(node.parent_id)
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,3 @@
1
+ module Pomona
2
+ VERSION = "0.6.0"
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pomona/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "pomona"
8
+ spec.version = Pomona::VERSION
9
+ spec.authors = ["ElliottAYoung"]
10
+ spec.email = ["elliott.a.young@gmail.com"]
11
+
12
+ spec.summary = "A lightweight solution for creating and managing tree data structures"
13
+ spec.description = "A lightweight solution for creating and managing tree data structures"
14
+ spec.homepage = "https://github.com/ElliottAYoung/Pomona"
15
+ spec.license = "MIT"
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.12"
22
+ spec.add_development_dependency "pry"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pomona
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.6.0
5
+ platform: ruby
6
+ authors:
7
+ - ElliottAYoung
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-07-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.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.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: A lightweight solution for creating and managing tree data structures
70
+ email:
71
+ - elliott.a.young@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - lib/pomona.rb
87
+ - lib/pomona/exceptions.rb
88
+ - lib/pomona/extractor.rb
89
+ - lib/pomona/node.rb
90
+ - lib/pomona/pruner.rb
91
+ - lib/pomona/tree.rb
92
+ - lib/pomona/version.rb
93
+ - pomona.gemspec
94
+ homepage: https://github.com/ElliottAYoung/Pomona
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.5.1
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: A lightweight solution for creating and managing tree data structures
118
+ test_files: []
119
+ has_rdoc: