Class: Improvise::DictionaryTree
- Inherits:
-
Object
- Object
- Improvise::DictionaryTree
- Defined in:
- lib/improvise/dictionarytree.rb
Class Method Summary collapse
Instance Method Summary collapse
- #add_entry!(key, value) ⇒ Object
- #as_json(opts = {}) ⇒ Object
-
#initialize(root = nil) ⇒ DictionaryTree
constructor
A new instance of DictionaryTree.
- #random_key ⇒ Object
- #random_value(key) ⇒ Object
- #to_json(*a) ⇒ Object
Constructor Details
#initialize(root = nil) ⇒ DictionaryTree
Returns a new instance of DictionaryTree.
7 8 9 |
# File 'lib/improvise/dictionarytree.rb', line 7 def initialize(root=nil) @root = root || Tree::TreeNode.new('root') end |
Class Method Details
.json_create(json_hash) ⇒ Object
48 49 50 |
# File 'lib/improvise/dictionarytree.rb', line 48 def self.json_create(json_hash) new(json_hash['root']) end |
Instance Method Details
#add_entry!(key, value) ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/improvise/dictionarytree.rb', line 11 def add_entry!(key, value) key_node = DictionaryTree.add_node!(@root, key) key_node.content += 1 value_node = DictionaryTree.add_node!(key_node, value) value_node.content += 1 end |
#as_json(opts = {}) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/improvise/dictionarytree.rb', line 37 def as_json(opts = {}) { JSON.create_id => self.class.name, 'root' => @root.as_json } end |
#random_key ⇒ Object
19 20 21 |
# File 'lib/improvise/dictionarytree.rb', line 19 def random_key DictionaryTree.new_pickup(@root).pick end |
#random_value(key) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/improvise/dictionarytree.rb', line 23 def random_value(key) if key.nil? return nil end node = @root[key] if node.nil? return nil else return DictionaryTree.new_pickup(node).pick end end |
#to_json(*a) ⇒ Object
44 45 46 |
# File 'lib/improvise/dictionarytree.rb', line 44 def to_json(*a) as_json.to_json(*a) end |