Class: Khayyam::Topic

Inherits:
Object
  • Object
show all
Defined in:
lib/topic.rb

Overview

Overview

Khayyam::Topic: The class for interacting with topic pairs.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, facts = {}) ⇒ Topic

Returns a new instance of Topic.



11
12
13
14
# File 'lib/topic.rb', line 11

def initialize name, facts={}
  @name = name
  @facts = facts
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/topic.rb', line 9

def name
  @name
end

Class Method Details

.import(string) ⇒ Object

Import data from a string.

Example: topic = Khayyam::Topic.import my_data



48
49
50
51
# File 'lib/topic.rb', line 48

def self.import string
  data = YAML.load "--- \n#{string}"
  khayyam = Khayyam::Topic.new data["topic"], data["categories"]
end

Instance Method Details

#categoriesObject

returns an array of sub-topics



17
18
19
# File 'lib/topic.rb', line 17

def categories
  @facts.keys
end

#exportObject

Export data to a string Example:

topic = Khayyam::Topic.new "vi"
topic.regarding "movement" do |items|
  items["h"] = "left"
  items["j"] = "up"
end
topic.regarding "commands" do |items|
  items[":q"] = "quit"
  items[":x"] = "save and quit"
end
output_string = topic.export


39
40
41
42
# File 'lib/topic.rb', line 39

def export
  output = YAML.dump_stream({'topic' => @name, 'categories' => @facts})
  output[5..output.length]
end

#regarding(category) {|| ... } ⇒ Object

used to work with sub-topics

Yields:

  • ()


22
23
24
25
# File 'lib/topic.rb', line 22

def regarding category
  @facts[category] ||= {}
  yield @facts[category]
end