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
-
#asciify(value) ⇒ Object
Replace accented characters with the closest ascii equivalent.
-
#branch(path) ⇒ Object
Load the entire branch of the tree under path at once.
-
#expanded_paths(path) ⇒ Object
Expand a path into an array of the path and all its ancestor paths.
- #path_delimiter ⇒ Object
-
#path_delimiter=(char) ⇒ Object
Set the path delimiter (default is ‘.’).
-
#path_like(value) ⇒ Object
Abstract way of finding paths that start with a value so it can be overridden by non-SQL implementations.
-
#pathify(value) ⇒ Object
Translate a value into a valid path part.
-
#roots ⇒ Object
Get all the root nodes (i.e. those without any parents).
-
#unquote(value) ⇒ Object
Remove quotation marks from a string.
Instance Method Details
#asciify(value) ⇒ Object
Replace accented characters with the closest ascii equivalent
80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/path_tree.rb', line 80 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.
62 63 64 65 66 67 68 69 |
# File 'lib/path_tree.rb', line 62 def branch (path) raise ArgumentError.new("branch path must not be blank") if path.blank? root = first(:conditions => {:path => path}) 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.
106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/path_tree.rb', line 106 def (path) = [] path.split(path_delimiter).each do |part| if .empty? << part else << "#{expanded.last}#{path_delimiter}#{part}" end end end |
#path_delimiter ⇒ Object
56 57 58 |
# File 'lib/path_tree.rb', line 56 def path_delimiter @path_delimiter ||= '.' end |
#path_delimiter=(char) ⇒ Object
Set the path delimiter (default is ‘.’).
52 53 54 |
# File 'lib/path_tree.rb', line 52 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.
101 102 103 |
# File 'lib/path_tree.rb', line 101 def path_like (value) all(:conditions => ["path LIKE ?", "#{value}#{path_delimiter}%"]) 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.
73 74 75 76 77 |
# File 'lib/path_tree.rb', line 73 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 |
#roots ⇒ Object
Get all the root nodes (i.e. those without any parents)
47 48 49 |
# File 'lib/path_tree.rb', line 47 def roots all(:conditions => {:parent_path => nil}) end |
#unquote(value) ⇒ Object
Remove quotation marks from a string.
96 97 98 |
# File 'lib/path_tree.rb', line 96 def unquote (value) value.gsub(/['"]/, '') if value end |