Class: MasterView::RailsExt::ShortPathCalculator
- Inherits:
-
Object
- Object
- MasterView::RailsExt::ShortPathCalculator
- Defined in:
- lib/masterview/rails_ext/short_path_calc.rb
Overview
short path calculator, computes the short relative path for given the mv_view_path, a base_path, and a template_path which may be relative or absolute. This calculator checks to see if it is absolute or relative and cleans up the ./../ returning the short_path relative to mv_view_path if it can be calculated, otherwise simply returns full_path.
Example usage: short_path = MasterView::RailsExt::ShortPathCalculator.calc_short_path(view_base_path, template_path)
Requires Pathname extensions Requires String#starts_with? Requires MasterView::ConfigSettings.rails_views_dir_path be set before this class is required
Example data:
MV_RAILS_ROOT_VIEW_DIR = /home/foo/app/views RAILS_ROOT = /home/foo/config/.. RAILS_ROOT_VIEW_DIR = /home/foo/config/../app/views BASE_DIR = /home/foo/config/../app/views BASE_DIR = /home/foo/vendor/plugins/goldberg/lib/../app/views template_path = store/list # already relative, return existing template_path = /home/foo/config/../app/views/store/list # starts with RAILS_ROOT_VIEW_DIR return short template_path = /home/foo/vendor/plugins/goldberg/lib/../app/views/../../../../../app/views/store/list # cleaned starts with RAILS_ROOT_VIEW_DIR return short template_path = ../../../../../app/views/store/list # when combined with base_path and cleaned starts with RAILS_ROOT_VIEW_DIR, return short template_path = /home/foo/vendor/plugins/goldberg/lib/../app/views/not/railsroot, return nil template_path = store/list # but base_path is not RAILS_ROOT_VIEW_DIR, return nil
Constant Summary collapse
- PATH_NOT_FOUND =
-1
Class Method Summary collapse
-
.calc_short_path(view_base_path, template_path) ⇒ Object
class method that computes the short relative path to mv_view_path for a template_path which may be absolute or relative.
-
.calc_short_path_and_get_mtime(view_base_path, template_path, mioTree) ⇒ Object
calculate the short_path and if exists return the mtime, otherwise nil.
-
.calc_short_path_and_retrieve(view_base_path, template_path, mioTree) ⇒ Object
calculate the short path and if exists then retrieve the data from the mioTree, otherwise return nil.
- .mv_path ⇒ Object
-
.mv_path=(view_path) ⇒ Object
cleans and expands the path storing in a class instance var.
Class Method Details
.calc_short_path(view_base_path, template_path) ⇒ Object
class method that computes the short relative path to mv_view_path for a template_path which may be absolute or relative. If short_path cannot be calculated (not under mv_view_path) then return nil. This calculation cleans up messy paths including ./.. and resultant calculations are hashed.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/masterview/rails_ext/short_path_calc.rb', line 58 def calc_short_path(view_base_path, template_path) base_path_cache = short_path_cache(view_base_path) short_path = base_path_cache.fetch(template_path, PATH_NOT_FOUND) # defaults to PATH_NOT_FOUND #MasterView::Log.debug { "calc_short_path - short_path=#{short_path} using cache view_base=#{view_base_path} template_path=#{template_path}"} unless short_path == PATH_NOT_FOUND #todo comment out return short_path unless short_path == PATH_NOT_FOUND # if already cached (even if nil was saved), then return it clean_view_base_pathname = Pathname.for_path(view_base_path)..cleanpath # expanded and cleaned clean_view_base_str = clean_view_base_pathname.to_s clean_template_pathname = Pathname.for_path(template_path).(clean_view_base_pathname).cleanpath # expanded from view_base and cleaned clean_template_str = clean_template_pathname.to_s raise "MasterView::ConfigSettings.rails_views_dir_path is nil, need to set config.rails_views_dir_path" if mv_path.nil? short_path = nil if clean_template_str.starts_with?(mv_path) # if clean_template_path starts with MV_VIEW_DIR (both are absolute) short_path = clean_template_str[mv_path.length+1..-1] # strip off MV_VIEW_DIR and return short path end #MasterView::Log.debug { "calc_short_path - short_path=#{short_path} view_base=#{view_base_path} template_path=#{template_path} RAILS_VIEW=#{@clean_mv_view_path}"} # todo comment out #MasterView::Log.debug { "cspdetails- #{clean_view_base_str}+#{clean_template_str} <=> #{mv_path}"} base_path_cache[template_path] = short_path # store in cache and return end |
.calc_short_path_and_get_mtime(view_base_path, template_path, mioTree) ⇒ Object
calculate the short_path and if exists return the mtime, otherwise nil
93 94 95 96 97 |
# File 'lib/masterview/rails_ext/short_path_calc.rb', line 93 def calc_short_path_and_get_mtime(view_base_path, template_path, mioTree) calc_short_path_and_call_block(view_base_path, template_path, mioTree) do |mio| mio.mtime end end |
.calc_short_path_and_retrieve(view_base_path, template_path, mioTree) ⇒ Object
calculate the short path and if exists then retrieve the data from the mioTree, otherwise return nil
86 87 88 89 90 |
# File 'lib/masterview/rails_ext/short_path_calc.rb', line 86 def calc_short_path_and_retrieve(view_base_path, template_path, mioTree) calc_short_path_and_call_block(view_base_path, template_path, mioTree) do |mio| mio.read end end |
.mv_path ⇒ Object
41 42 43 |
# File 'lib/masterview/rails_ext/short_path_calc.rb', line 41 def mv_path @clean_mv_view_path end |
.mv_path=(view_path) ⇒ Object
cleans and expands the path storing in a class instance var
36 37 38 39 |
# File 'lib/masterview/rails_ext/short_path_calc.rb', line 36 def mv_path=(view_path) @clean_mv_view_path = Pathname.for_path(view_path)..cleanpath.to_s unless view_path.nil? #MasterView::Log.debug { "ShortPathCalculator.mv_path=#{@clean_mv_view_path}"} end |