ListBrowser 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2010 Aldric Giacomoni
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,26 @@
1
+ ~~~~ ListBrowser README ~~~~
2
+
3
+ I was working on a parser for a very specific tree structure, and was frustrated
4
+ that there wasn't a simple way to parse it in irb. "What??", I thought. "I have
5
+ to use my brain?! God forbid!".
6
+ So I set to using my brain a little more to create this tool. It's not much,
7
+ but maybe it'll make someone's life a little easier.
8
+
9
+ It can be used in irb, and really should be used there - when you call it on
10
+ a tree structure, you'll get a menu with a list of choices on how you want to
11
+ go through whatever structure you gave it.
12
+
13
+ In my particular, special case, I would do something like this:
14
+
15
+ require 'sgf_parser' # For my tree structure
16
+ require 'list_browser' # For this.
17
+
18
+ tree = SgfParser::Tree.new :filename => "kogo.sgf"
19
+
20
+ ListBrowser.new tree.root, 'parent', 'children', 'properties'
21
+
22
+ # And follow the menu!
23
+
24
+ In my particular case, I would not need to enter those strings, as they
25
+ just happen to be the names I chose for my tree structure, but they should
26
+ serve as a good enough example!
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,86 @@
1
+ # A class to browse tree structures.
2
+ # When creating the object, you have one required attribute and three optional
3
+ # attributes:
4
+ # node (the node where you wish to start browsing, usually the root of the tree)
5
+ #
6
+ # parent (the name of the method used to get to the parent)
7
+ #
8
+ # children (the name of the method used to get to the children)
9
+ #
10
+ # properties (the name of the method used to get to properties)
11
+ class ListBrowser
12
+
13
+ attr_accessor :node, :parent, :children, :properties
14
+
15
+ def initialize node, parent='parent', children='children', properties='properties'
16
+ @node = node
17
+ @parent = parent.to_sym
18
+ @children = children.to_sym
19
+ @properties = properties.to_sym
20
+ puts @node.send(@properties)
21
+ generate_menu
22
+ end
23
+
24
+ # Call this method to go to the current node's parent
25
+ def parent
26
+ unless @node.send(@parent).nil?
27
+ @node = @node.send(@parent)
28
+ end
29
+ end
30
+
31
+ # Call this method, with a number between 0 and the size of the array of
32
+ # children -1 (if there are three children, between 0 and 2), to go to
33
+ # the current node's child.
34
+ def child number
35
+ unless @node.send(@children).nil?
36
+ @node = ( ( @node.send(@children) ).at(number) )
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def generate_menu
43
+ input = nil
44
+ until ['q', 'Q'].include? input
45
+ input = nil
46
+ @menu = "Press Q to quit.\n"
47
+ count = '1'
48
+ @node.send(@children).each do
49
+ @menu << "#{count}. Go to child ##{count}.\n"
50
+ count.succ!
51
+ end
52
+ @menu << "#{count}. Go to parent.\n"
53
+ @menu << "#{count.succ}. Print current node's properties again.\n"
54
+
55
+ choices = ['q', 'Q'] + ('1'..count.succ).to_a
56
+ while !choices.include?(input)
57
+ puts @menu
58
+ input = gets.chomp
59
+ end
60
+ case input
61
+ when 'q', 'Q'
62
+ leave
63
+ when count
64
+ parent
65
+ when count.succ
66
+ puts @node.send(@properties)
67
+ else
68
+ child input.to_i - 1
69
+ end
70
+ end
71
+ end
72
+
73
+ def print_properties
74
+ properties = @node.send(@properties)
75
+ keylength = properties.max {|a, b| a.size }
76
+ #valuelength = properties.max {|a, b| b.size}
77
+ properties.each do |k, v|
78
+ puts "#{k.ljust(keylength)} => #{v}"
79
+ end
80
+ end
81
+
82
+ def leave
83
+ # AND DON'T COME BACK!
84
+ end
85
+
86
+ end
@@ -0,0 +1,9 @@
1
+ LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'ListBrowser'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ListBrowser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Aldric Giacomoni
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-15 00:00:00 -05:00
13
+ default_executable: list_browser.rb
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.2.9
24
+ version:
25
+ description: ListBrowser allows you to navigate non-obvious structures like linked lists.
26
+ email: aldric@trevoke.net
27
+ executables:
28
+ - list_browser.rb
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.rdoc
34
+ files:
35
+ - VERSION
36
+ - bin/list_browser.rb
37
+ - LICENSE
38
+ - README.rdoc
39
+ has_rdoc: true
40
+ homepage: http://github.com/Trevoke/ListBrowser
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options:
45
+ - --charset=UTF-8
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.3.5
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: A tool to browse tree structures.
67
+ test_files:
68
+ - spec/spec_helper.rb