Class: UKPostcode::Tree

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

Instance Method Summary collapse

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

#compressObject



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

#regexpObject



19
20
21
# File 'lib/uk_postcode/tree.rb', line 19

def regexp
  Regexp.new('^' + node_regexp(@root))
end

#to_hObject



23
24
25
# File 'lib/uk_postcode/tree.rb', line 23

def to_h
  @root
end