Class: Bunto::EntryFilter
- Inherits:
-
Object
- Object
- Bunto::EntryFilter
- Defined in:
- lib/bunto/entry_filter.rb
Constant Summary collapse
- SPECIAL_LEADING_CHARACTERS =
[ ".", "_", "#", "~", ].freeze
Instance Attribute Summary collapse
-
#site ⇒ Object
readonly
Returns the value of attribute site.
Instance Method Summary collapse
- #backup?(entry) ⇒ Boolean
- #base_directory ⇒ Object
- #derive_base_directory(site, base_dir) ⇒ Object
- #excluded?(entry) ⇒ Boolean
- #filter(entries) ⇒ Object
-
#glob_include?(enum, e) ⇒ Boolean
-- Check if an entry matches a specific pattern and return true,false.
- #included?(entry) ⇒ Boolean
-
#initialize(site, base_directory = nil) ⇒ EntryFilter
constructor
A new instance of EntryFilter.
- #relative_to_source(entry) ⇒ Object
- #special?(entry) ⇒ Boolean
-
#symlink?(entry) ⇒ Boolean
-- Check if a file is a symlink.
-
#symlink_outside_site_source?(entry) ⇒ Boolean
-- NOTE: Pathutil#in_path? gets the realpath.
Constructor Details
#initialize(site, base_directory = nil) ⇒ EntryFilter
Returns a new instance of EntryFilter.
8 9 10 11 12 13 |
# File 'lib/bunto/entry_filter.rb', line 8 def initialize(site, base_directory = nil) @site = site @base_directory = derive_base_directory( @site, base_directory.to_s.dup ) end |
Instance Attribute Details
#site ⇒ Object (readonly)
Returns the value of attribute site.
3 4 5 |
# File 'lib/bunto/entry_filter.rb', line 3 def site @site end |
Instance Method Details
#backup?(entry) ⇒ Boolean
47 48 49 |
# File 'lib/bunto/entry_filter.rb', line 47 def backup?(entry) entry[-1..-1] == "~" end |
#base_directory ⇒ Object
15 16 17 |
# File 'lib/bunto/entry_filter.rb', line 15 def base_directory @base_directory.to_s end |
#derive_base_directory(site, base_dir) ⇒ Object
19 20 21 22 |
# File 'lib/bunto/entry_filter.rb', line 19 def derive_base_directory(site, base_dir) base_dir[site.source] = "" if base_dir.start_with?(site.source) base_dir end |
#excluded?(entry) ⇒ Boolean
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/bunto/entry_filter.rb', line 51 def excluded?(entry) glob_include?(site.exclude, relative_to_source(entry)).tap do |excluded| if excluded Bunto.logger.debug( "EntryFilter:", "excluded #{relative_to_source(entry)}" ) end end end |
#filter(entries) ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/bunto/entry_filter.rb', line 30 def filter(entries) entries.reject do |e| unless included?(e) special?(e) || backup?(e) || excluded?(e) || symlink?(e) end end end |
#glob_include?(enum, e) ⇒ Boolean
--
Check if an entry matches a specific pattern and return true,false. Returns true if path matches against any glob pattern.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/bunto/entry_filter.rb', line 86 def glob_include?(enum, e) entry = Pathutil.new(site.in_source_dir).join(e) enum.any? do |exp| # Users who send a Regexp knows what they want to # exclude, so let them send a Regexp to exclude files, # we will not bother caring if it works or not, it's # on them at this point. if exp.is_a?(Regexp) entry =~ exp else item = Pathutil.new(site.in_source_dir).join(exp) # If it's a directory they want to exclude, AKA # ends with a "/" then we will go on to check and # see if the entry falls within that path and # exclude it if that's the case. if e.end_with?("/") entry.in_path?( item ) else File.fnmatch?(item, entry) || entry.to_path.start_with?( item ) end end end end |
#included?(entry) ⇒ Boolean
38 39 40 |
# File 'lib/bunto/entry_filter.rb', line 38 def included?(entry) glob_include?(site.include, entry) end |
#relative_to_source(entry) ⇒ Object
24 25 26 27 28 |
# File 'lib/bunto/entry_filter.rb', line 24 def relative_to_source(entry) File.join( base_directory, entry ) end |
#special?(entry) ⇒ Boolean
42 43 44 45 |
# File 'lib/bunto/entry_filter.rb', line 42 def special?(entry) SPECIAL_LEADING_CHARACTERS.include?(entry[0..0]) || SPECIAL_LEADING_CHARACTERS.include?(File.basename(entry)[0..0]) end |
#symlink?(entry) ⇒ Boolean
--
Check if a file is a symlink. NOTE: This can be converted to allowing even in safe, since we use Pathutil#in_path? now.
67 68 69 |
# File 'lib/bunto/entry_filter.rb', line 67 def symlink?(entry) site.safe && File.symlink?(entry) && symlink_outside_site_source?(entry) end |
#symlink_outside_site_source?(entry) ⇒ Boolean
--
NOTE: Pathutil#in_path? gets the realpath. Check if a path is outside of our given root.
76 77 78 79 80 |
# File 'lib/bunto/entry_filter.rb', line 76 def symlink_outside_site_source?(entry) !Pathutil.new(entry).in_path?( site.in_source_dir ) end |