Class: UKPostcode::Tree
- Inherits:
-
Object
- Object
- UKPostcode::Tree
- Defined in:
- lib/uk_postcode/tree.rb
Instance Method Summary collapse
- #compress ⇒ Object
- #filter(leaf_value) ⇒ Object
-
#initialize(tree = {}) ⇒ Tree
constructor
A new instance of Tree.
- #insert(path, value) ⇒ Object
- #regexp ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(tree = {}) ⇒ Tree
Returns a new instance of Tree.
3 4 5 |
# File 'lib/uk_postcode/tree.rb', line 3 def initialize(tree = {}) @root = tree end |
Instance Method Details
#compress ⇒ Object
11 12 13 |
# File 'lib/uk_postcode/tree.rb', line 11 def compress self.class.new(compress_node(@root)) end |
#filter(leaf_value) ⇒ Object
15 16 17 |
# File 'lib/uk_postcode/tree.rb', line 15 def filter(leaf_value) self.class.new(filter_node(@root, leaf_value)) end |
#insert(path, value) ⇒ Object
7 8 9 |
# File 'lib/uk_postcode/tree.rb', line 7 def insert(path, value) path[0..-2].inject(@root) { |n, p| n[p] ||= {} }[path.last] = value end |
#regexp ⇒ Object
19 20 21 |
# File 'lib/uk_postcode/tree.rb', line 19 def regexp Regexp.new('^' + node_regexp(@root)) end |
#to_h ⇒ Object
23 24 25 |
# File 'lib/uk_postcode/tree.rb', line 23 def to_h @root end |