Module: PathTree::ClassMethods

Includes:
Patterns
Defined in:
lib/path_tree.rb

Constant Summary collapse

NON_WORD_PATTERN =
/[^a-z0-9_]+/.freeze
DASH_AT_START_PATTERN =
/^-+/.freeze
DASH_AT_END_PATTERN =
/-+$/.freeze

Constants included from Patterns

Patterns::LOWER_AE_PATTERN, Patterns::LOWER_A_PATTERN, Patterns::LOWER_C_PATTERN, Patterns::LOWER_E_PATTERN, Patterns::LOWER_I_PATTERN, Patterns::LOWER_N_PATTERN, Patterns::LOWER_O_PATTERN, Patterns::LOWER_U_PATTERN, Patterns::LOWER_Y_PATTERN, Patterns::SS_PATTERN, Patterns::UPPER_AE_PATTERN, Patterns::UPPER_A_PATTERN, Patterns::UPPER_C_PATTERN, Patterns::UPPER_D_PATTERN, Patterns::UPPER_E_PATTERN, Patterns::UPPER_I_PATTERN, Patterns::UPPER_N_PATTERN, Patterns::UPPER_O_PATTERN, Patterns::UPPER_U_PATTERN, Patterns::UPPER_Y_PATTERN

Instance Method Summary collapse

Instance Method Details

#asciify(value) ⇒ Object

Replace accented characters with the closest ascii equivalent. NOTE: characters than can’t be decomposed to a latin equivalent may not be replaced.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/path_tree.rb', line 78

def asciify (value)
  if value
    value.gsub(UPPER_A_PATTERN, 'A').gsub(LOWER_A_PATTERN, 'a').
      gsub(UPPER_E_PATTERN, 'E').gsub(LOWER_E_PATTERN, 'e').
      gsub(UPPER_I_PATTERN, 'I').gsub(LOWER_I_PATTERN, 'i').
      gsub(UPPER_O_PATTERN, 'O').gsub(LOWER_O_PATTERN, 'o').
      gsub(UPPER_U_PATTERN, 'U').gsub(LOWER_U_PATTERN, 'u').
      gsub(UPPER_Y_PATTERN, 'Y').gsub(LOWER_Y_PATTERN, 'y').
      gsub(UPPER_N_PATTERN, 'N').gsub(LOWER_N_PATTERN, 'n').
      gsub(UPPER_C_PATTERN, 'C').gsub(LOWER_C_PATTERN, 'c').
      gsub(UPPER_AE_PATTERN, 'AE').gsub(LOWER_AE_PATTERN, 'ae').
      gsub(SS_PATTERN, 'ss').gsub(UPPER_D_PATTERN, 'D')
  end
end

#branch(path) ⇒ Object

Load the entire branch of the tree under path at once. If you will be traversing the tree, this is the fastest way to load it. Returns the root node of the branch.

Raises:

  • (ArgumentError)


59
60
61
62
63
64
65
66
# File 'lib/path_tree.rb', line 59

def branch (path)
  raise ArgumentError.new("branch path must not be blank") if path.blank?
  root = where(path: path).first
  return [] unless root
  nodes = path_like(path).sort{|a,b| b.path <=> a.path}
  nodes << root
  return populate_tree_structure!(nodes.pop, nodes)
end

#expanded_paths(path) ⇒ Object

Expand a path into an array of the path and all its ancestor paths.



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/path_tree.rb', line 104

def expanded_paths (path)
  expanded = []
  path.split(path_delimiter).each do |part|
    if expanded.empty?
      expanded << part
    else
      expanded << "#{expanded.last}#{path_delimiter}#{part}"
    end
  end
  expanded
end

#path_delimiterObject



53
54
55
# File 'lib/path_tree.rb', line 53

def path_delimiter
  @path_delimiter ||= '.'
end

#path_delimiter=(char) ⇒ Object

Set the path delimiter (default is ‘.’).



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

def path_delimiter= (char)
  @path_delimiter = char
end

#path_like(value) ⇒ Object

Abstract way of finding paths that start with a value so it can be overridden by non-SQL implementations.



99
100
101
# File 'lib/path_tree.rb', line 99

def path_like (value)
  where("path LIKE ?", "#{value}#{path_delimiter}%").to_a
end

#pathify(value) ⇒ Object

Translate a value into a valid path part. By default this will translate it into an ascii lower case value with words delimited by dashes. Implementations can override this logic.



70
71
72
73
74
# File 'lib/path_tree.rb', line 70

def pathify (value)
  if value
    asciify(unquote(value)).strip.downcase.gsub(NON_WORD_PATTERN, '-').gsub(DASH_AT_START_PATTERN, '').gsub(DASH_AT_END_PATTERN, '')
  end
end

#rootsObject

Get all the root nodes (i.e. those without any parents)



44
45
46
# File 'lib/path_tree.rb', line 44

def roots
  where(parent_path: nil).to_a
end

#unquote(value) ⇒ Object

Remove quotation marks from a string.



94
95
96
# File 'lib/path_tree.rb', line 94

def unquote (value)
  value.gsub(/['"]/, '') if value
end