Class: Dog::Map
- Inherits:
-
Object
- Object
- Dog::Map
- Defined in:
- lib/dog/map.rb
Instance Attribute Summary collapse
-
#topic_name ⇒ Object
Returns the value of attribute topic_name.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #add(topic: @topic_name, name:, path:) ⇒ Object
- #count(topic = @topic_name) ⇒ Object
- #find_incoming_links(destination) ⇒ Object
- #has_topic?(name) ⇒ Boolean
- #here ⇒ Object
-
#initialize ⇒ Map
constructor
A new instance of Map.
- #link_names(topic) ⇒ Object
- #load ⇒ Object
- #move_down(topic: @topic_name, index:) ⇒ Object
- #move_up(topic: @topic_name, index:) ⇒ Object
- #name(topic: @topic_name, index:) ⇒ Object
- #navigate(destination) ⇒ Object
- #path(topic: @topic_name, index:) ⇒ Object
- #remove_link(topic: @topic_name, name:) ⇒ Object
- #remove_topic(topic = @topic) ⇒ Object
- #replace(topic: @topic_name, name:, new_name:, new_path:) ⇒ Object
- #save ⇒ Object
Constructor Details
#initialize ⇒ Map
Returns a new instance of Map.
8 9 10 11 |
# File 'lib/dog/map.rb', line 8 def initialize @map = {"@home" => []} @topic_name = "@home" end |
Instance Attribute Details
#topic_name ⇒ Object
Returns the value of attribute topic_name.
6 7 8 |
# File 'lib/dog/map.rb', line 6 def topic_name @topic_name end |
Instance Method Details
#[](key) ⇒ Object
99 100 101 |
# File 'lib/dog/map.rb', line 99 def [](key) @map[key] end |
#add(topic: @topic_name, name:, path:) ⇒ Object
71 72 73 74 75 |
# File 'lib/dog/map.rb', line 71 def add(topic: @topic_name, name:, path:) @map[@topic_name].push( create_link(name,path) ) end |
#count(topic = @topic_name) ⇒ Object
21 22 23 |
# File 'lib/dog/map.rb', line 21 def count(topic = @topic_name) @map[topic].length end |
#find_incoming_links(destination) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/dog/map.rb', line 103 def find_incoming_links(destination) links = [] @map.each do |key, value| value.each do |item| if item["path"] == destination if key == @topic_name links.push( create_link("#{key} (<<)", key) ) else links.push( create_link(key, key) ) end end end end return links end |
#has_topic?(name) ⇒ Boolean
43 44 45 |
# File 'lib/dog/map.rb', line 43 def has_topic?(name) @map.has_key?(name) end |
#here ⇒ Object
47 48 49 |
# File 'lib/dog/map.rb', line 47 def here @map[@topic_name] end |
#link_names(topic) ⇒ Object
123 124 125 |
# File 'lib/dog/map.rb', line 123 def link_names(topic) @map[topic].map{|l| l["name"]} end |
#load ⇒ Object
85 86 87 88 89 90 91 92 93 |
# File 'lib/dog/map.rb', line 85 def load begin data = YAML.load_file(MAP_FILENAME) @map = data if data rescue Errno::ENOENT, Errno::ENOTDIR return false end return true end |
#move_down(topic: @topic_name, index:) ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/dog/map.rb', line 34 def move_down(topic: @topic_name, index:) if index < count(@topic_name) - 1 @map[@topic_name][index], @map[@topic_name][index + 1] = @map[@topic_name][index + 1], @map[@topic_name][index] end end |
#move_up(topic: @topic_name, index:) ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/dog/map.rb', line 25 def move_up(topic: @topic_name, index:) if index > 0 @map[@topic_name][index], @map[@topic_name][index - 1] = @map[@topic_name][index - 1], @map[@topic_name][index] end end |
#name(topic: @topic_name, index:) ⇒ Object
13 14 15 |
# File 'lib/dog/map.rb', line 13 def name(topic: @topic_name, index:) @map[topic][index]["name"] end |
#navigate(destination) ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/dog/map.rb', line 77 def navigate( destination ) if !@map[destination] @map[destination] = [] end @topic_name = destination end |
#path(topic: @topic_name, index:) ⇒ Object
17 18 19 |
# File 'lib/dog/map.rb', line 17 def path(topic: @topic_name, index:) @map[topic][index]["path"] end |
#remove_link(topic: @topic_name, name:) ⇒ Object
55 56 57 58 59 |
# File 'lib/dog/map.rb', line 55 def remove_link(topic: @topic_name, name:) here.delete_if do |link| link["name"] == name end end |
#remove_topic(topic = @topic) ⇒ Object
51 52 53 |
# File 'lib/dog/map.rb', line 51 def remove_topic(topic = @topic) @map.delete(@topic) end |
#replace(topic: @topic_name, name:, new_name:, new_path:) ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/dog/map.rb', line 61 def replace(topic: @topic_name, name:, new_name:, new_path:) index = @map[@topic_name].index do |link| link["name"] == name end if index here[index] = create_link(new_name, new_path) end end |
#save ⇒ Object
95 96 97 |
# File 'lib/dog/map.rb', line 95 def save File.write(MAP_FILENAME, @map.to_yaml) end |