Class: Khayyam::Topic
- Inherits:
-
Object
- Object
- Khayyam::Topic
- Defined in:
- lib/topic.rb
Overview
Overview
Khayyam::Topic: The class for interacting with topic pairs.
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
-
.import(string) ⇒ Object
Import data from a string.
Instance Method Summary collapse
-
#categories ⇒ Object
returns an array of sub-topics.
- #export ⇒ Object
-
#initialize(name, facts = {}) ⇒ Topic
constructor
A new instance of Topic.
-
#regarding(category) {|| ... } ⇒ Object
used to work with sub-topics.
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
#name ⇒ Object (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
#categories ⇒ Object
returns an array of sub-topics
17 18 19 |
# File 'lib/topic.rb', line 17 def categories @facts.keys end |
#export ⇒ Object
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
22 23 24 25 |
# File 'lib/topic.rb', line 22 def regarding category @facts[category] ||= {} yield @facts[category] end |