From 785adabc4b8d892b6e06fca2f259e9c5147e9ca5 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Mon, 12 Oct 2015 20:41:14 +0200 Subject: implements an evented file update checker [Puneet Agarwal] This is the implementation of the file update checker written by Puneet Agarwal for GSoC 2015 (except for the tiny version of the listen gem, which was 3.0.2 in the original patch). Puneet's branch became too out of sync with upstream. This is the final work in one single clean commit. Credit goes in the first line using a convention understood by the contrib app. --- .../active_support/file_evented_update_checker.rb | 67 ++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 activesupport/lib/active_support/file_evented_update_checker.rb (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/file_evented_update_checker.rb b/activesupport/lib/active_support/file_evented_update_checker.rb new file mode 100644 index 0000000000..d45576bb00 --- /dev/null +++ b/activesupport/lib/active_support/file_evented_update_checker.rb @@ -0,0 +1,67 @@ +require 'listen' + +module ActiveSupport + class FileEventedUpdateChecker + attr_reader :listener + def initialize(files, directories={}, &block) + @files = files.map { |f| File.expand_path(f)}.to_set + @dirs = Hash.new + directories.each do |key,value| + @dirs[File.expand_path(key)] = Array(value) if !Array(value).empty? + end + @block = block + @modified = false + watch_dirs = base_directories + @listener = Listen.to(*watch_dirs,&method(:changed)) if !watch_dirs.empty? + @listener.start if @listener + end + + def updated? + @modified + end + + def execute + @block.call + ensure + @modified = false + end + + def execute_if_updated + if updated? + execute + true + else + false + end + end + + private + + def watching?(file) + return true if @files.include?(file) + cfile = file + while !cfile.eql? "/" + cfile = File.expand_path("#{cfile}/..") + if !@dirs[cfile].nil? and file.end_with?(*(@dirs[cfile].map {|ext| ".#{ext.to_s}"})) + return true + end + end + false + end + + def changed(modified, added, removed) + return if updated? + if (modified + added + removed).any? { |f| watching? f } + @modified = true + end + end + + def base_directories + (@files.map { |f| existing_parent(File.expand_path("#{f}/..")) } + @dirs.keys.map {|dir| existing_parent(dir)}).uniq + end + + def existing_parent(path) + File.exist?(path) ? path : existing_parent(File.expand_path("#{path}/..")) + end + end +end -- cgit v1.2.3 From 07369448d3c7b62c98abc0f497c497d1e4e5e799 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Sun, 18 Oct 2015 23:07:03 +0200 Subject: revises the implementation of the evented file monitor --- .../active_support/file_evented_update_checker.rb | 89 ++++++++++++++++------ 1 file changed, 65 insertions(+), 24 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/file_evented_update_checker.rb b/activesupport/lib/active_support/file_evented_update_checker.rb index d45576bb00..5a7e0d2794 100644 --- a/activesupport/lib/active_support/file_evented_update_checker.rb +++ b/activesupport/lib/active_support/file_evented_update_checker.rb @@ -1,19 +1,26 @@ require 'listen' +require 'set' +require 'pathname' module ActiveSupport class FileEventedUpdateChecker attr_reader :listener - def initialize(files, directories={}, &block) - @files = files.map { |f| File.expand_path(f)}.to_set - @dirs = Hash.new - directories.each do |key,value| - @dirs[File.expand_path(key)] = Array(value) if !Array(value).empty? + + def initialize(files, dirs={}, &block) + @files = files.map {|f| expand_path(f)}.to_set + + @dirs = {} + dirs.each do |dir, exts| + @dirs[expand_path(dir)] = Array(exts).map(&:to_s) end + @block = block @modified = false - watch_dirs = base_directories - @listener = Listen.to(*watch_dirs,&method(:changed)) if !watch_dirs.empty? - @listener.start if @listener + + if (watch_dirs = base_directories).any? + @listener = Listen.to(*watch_dirs, &method(:changed)) + @listener.start + end end def updated? @@ -30,38 +37,72 @@ module ActiveSupport if updated? execute true - else - false end end private - def watching?(file) - return true if @files.include?(file) - cfile = file - while !cfile.eql? "/" - cfile = File.expand_path("#{cfile}/..") - if !@dirs[cfile].nil? and file.end_with?(*(@dirs[cfile].map {|ext| ".#{ext.to_s}"})) - return true - end - end - false + def expand_path(fname) + File.expand_path(fname) end def changed(modified, added, removed) return if updated? - if (modified + added + removed).any? { |f| watching? f } + + if (modified + added + removed).any? {|f| watching?(f)} @modified = true end end + def watching?(file) + file = expand_path(file) + return true if @files.member?(file) + + file = Pathname.new(file) + return false if file.directory? + + ext = file.extname.sub(/\A\./, '') + dir = file.dirname + + loop do + if @dirs.fetch(dir.to_path, []).include?(ext) + break true + else + if dir.root? # TODO: find a common parent directory in initialize + break false + end + dir = dir.parent + end + end + end + + # TODO: Better return a list of non-nested directories. def base_directories - (@files.map { |f| existing_parent(File.expand_path("#{f}/..")) } + @dirs.keys.map {|dir| existing_parent(dir)}).uniq + [].tap do |bd| + bd.concat @files.map {|f| existing_parent(File.dirname(f))} + bd.concat @dirs.keys.map {|dir| existing_parent(dir)} + bd.compact! + bd.uniq! + end end - def existing_parent(path) - File.exist?(path) ? path : existing_parent(File.expand_path("#{path}/..")) + def existing_parent(dir) + dir = Pathname.new(File.expand_path(dir)) + + loop do + if dir.directory? + break dir.to_path + else + if dir.root? + # Edge case in which not even the root exists. For example, Windows + # paths could have a non-existing drive letter. Since the parent of + # root is root, we need to break to prevent an infinite loop. + break + else + dir = dir.parent + end + end + end end end end -- cgit v1.2.3 From 0462329166cc667c2dc81abc85493e2ae6db5e4c Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Wed, 4 Nov 2015 08:32:33 +0100 Subject: no need to have access to the listener --- activesupport/lib/active_support/file_evented_update_checker.rb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/file_evented_update_checker.rb b/activesupport/lib/active_support/file_evented_update_checker.rb index 5a7e0d2794..70d38c10bb 100644 --- a/activesupport/lib/active_support/file_evented_update_checker.rb +++ b/activesupport/lib/active_support/file_evented_update_checker.rb @@ -4,8 +4,6 @@ require 'pathname' module ActiveSupport class FileEventedUpdateChecker - attr_reader :listener - def initialize(files, dirs={}, &block) @files = files.map {|f| expand_path(f)}.to_set @@ -18,8 +16,7 @@ module ActiveSupport @modified = false if (watch_dirs = base_directories).any? - @listener = Listen.to(*watch_dirs, &method(:changed)) - @listener.start + Listen.to(*watch_dirs, &method(:changed)).start end end -- cgit v1.2.3 From b685095fc957a428fd86514d28a9183dbbdc3ab5 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Wed, 4 Nov 2015 08:34:28 +0100 Subject: remove explicit File.expand_path call --- activesupport/lib/active_support/file_evented_update_checker.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/file_evented_update_checker.rb b/activesupport/lib/active_support/file_evented_update_checker.rb index 70d38c10bb..18ac398a43 100644 --- a/activesupport/lib/active_support/file_evented_update_checker.rb +++ b/activesupport/lib/active_support/file_evented_update_checker.rb @@ -84,7 +84,7 @@ module ActiveSupport end def existing_parent(dir) - dir = Pathname.new(File.expand_path(dir)) + dir = Pathname.new(expand_path(dir)) loop do if dir.directory? -- cgit v1.2.3 From a62387d620b8b6862922b6a359b76c584986075d Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Sun, 8 Nov 2015 14:29:26 -0800 Subject: stop ascending at the longest common subpath This commit also bases everything on Pathname internally. --- .../active_support/file_evented_update_checker.rb | 99 ++++++++++++++-------- 1 file changed, 66 insertions(+), 33 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/file_evented_update_checker.rb b/activesupport/lib/active_support/file_evented_update_checker.rb index 18ac398a43..a4dd4ff30c 100644 --- a/activesupport/lib/active_support/file_evented_update_checker.rb +++ b/activesupport/lib/active_support/file_evented_update_checker.rb @@ -3,17 +3,19 @@ require 'set' require 'pathname' module ActiveSupport - class FileEventedUpdateChecker + class FileEventedUpdateChecker #:nodoc: all def initialize(files, dirs={}, &block) - @files = files.map {|f| expand_path(f)}.to_set + @ph = PathHelper.new + @files = files.map {|f| @ph.xpath(f)}.to_set @dirs = {} dirs.each do |dir, exts| - @dirs[expand_path(dir)] = Array(exts).map(&:to_s) + @dirs[@ph.xpath(dir)] = Array(exts).map {|ext| @ph.normalize_extension(ext)} end - @block = block + @block = block @modified = false + @lcsp = @ph.longest_common_subpath(@dirs.keys) if (watch_dirs = base_directories).any? Listen.to(*watch_dirs, &method(:changed)).start @@ -39,35 +41,31 @@ module ActiveSupport private - def expand_path(fname) - File.expand_path(fname) - end - def changed(modified, added, removed) - return if updated? - - if (modified + added + removed).any? {|f| watching?(f)} - @modified = true + unless updated? + @modified = (modified + added + removed).any? {|f| watching?(f)} end end def watching?(file) - file = expand_path(file) - return true if @files.member?(file) + file = @ph.xpath(file) - file = Pathname.new(file) + return true if @files.member?(file) return false if file.directory? - ext = file.extname.sub(/\A\./, '') + ext = @ph.normalize_extension(file.extname) dir = file.dirname loop do - if @dirs.fetch(dir.to_path, []).include?(ext) + if @dirs.fetch(dir, []).include?(ext) break true else - if dir.root? # TODO: find a common parent directory in initialize - break false + if @lcsp + break false if dir == @lcsp + else + break false if dir.root? end + dir = dir.parent end end @@ -76,27 +74,62 @@ module ActiveSupport # TODO: Better return a list of non-nested directories. def base_directories [].tap do |bd| - bd.concat @files.map {|f| existing_parent(File.dirname(f))} - bd.concat @dirs.keys.map {|dir| existing_parent(dir)} + bd.concat @files.map {|f| @ph.existing_parent(f.dirname)} + bd.concat @dirs.keys.map {|dir| @ph.existing_parent(dir)} bd.compact! bd.uniq! end end - def existing_parent(dir) - dir = Pathname.new(expand_path(dir)) + class PathHelper + def xpath(path) + Pathname.new(path).expand_path + end - loop do - if dir.directory? - break dir.to_path - else - if dir.root? - # Edge case in which not even the root exists. For example, Windows - # paths could have a non-existing drive letter. Since the parent of - # root is root, we need to break to prevent an infinite loop. - break + def normalize_extension(ext) + ext.to_s.sub(/\A\./, '') + end + + # Given a collection of Pathname objects returns the longest subpath + # common to all of them, or +nil+ if there is none. + def longest_common_subpath(paths) + return if paths.empty? + + csp = Pathname.new(paths[0]) + + paths[1..-1].each do |path| + loop do + break if path.ascend do |ascendant| + break true if ascendant == csp + end + + if csp.root? + # A root directory is not an ascendant of path. This may happen + # if there are paths in different drives on Windows. + return + else + csp = csp.parent + end + end + end + + csp + end + + # Returns the deepest existing ascendant, which could be the argument itself. + def existing_parent(dir) + loop do + if dir.directory? + break dir else - dir = dir.parent + if dir.root? + # Edge case in which not even the root exists. For example, Windows + # paths could have a non-existing drive letter. Since the parent of + # root is root, we need to break to prevent an infinite loop. + break + else + dir = dir.parent + end end end end -- cgit v1.2.3 From 7ea3207f416b01e89fd4a8e61cb9a2257bb7e991 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Sun, 8 Nov 2015 14:47:15 -0800 Subject: s/@modified/@updated/g --- .../lib/active_support/file_evented_update_checker.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/file_evented_update_checker.rb b/activesupport/lib/active_support/file_evented_update_checker.rb index a4dd4ff30c..41ec12d829 100644 --- a/activesupport/lib/active_support/file_evented_update_checker.rb +++ b/activesupport/lib/active_support/file_evented_update_checker.rb @@ -13,9 +13,9 @@ module ActiveSupport @dirs[@ph.xpath(dir)] = Array(exts).map {|ext| @ph.normalize_extension(ext)} end - @block = block - @modified = false - @lcsp = @ph.longest_common_subpath(@dirs.keys) + @block = block + @updated = false + @lcsp = @ph.longest_common_subpath(@dirs.keys) if (watch_dirs = base_directories).any? Listen.to(*watch_dirs, &method(:changed)).start @@ -23,13 +23,13 @@ module ActiveSupport end def updated? - @modified + @updated end def execute @block.call ensure - @modified = false + @updated = false end def execute_if_updated @@ -43,7 +43,7 @@ module ActiveSupport def changed(modified, added, removed) unless updated? - @modified = (modified + added + removed).any? {|f| watching?(f)} + @updated = (modified + added + removed).any? {|f| watching?(f)} end end -- cgit v1.2.3 From eda503c836c6cd02937e111b175979c5722677fd Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Tue, 10 Nov 2015 02:28:14 -0800 Subject: the evented monitor filters out descendants --- .../active_support/file_evented_update_checker.rb | 46 +++++++++++++++++----- 1 file changed, 36 insertions(+), 10 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/file_evented_update_checker.rb b/activesupport/lib/active_support/file_evented_update_checker.rb index 41ec12d829..262e801ce3 100644 --- a/activesupport/lib/active_support/file_evented_update_checker.rb +++ b/activesupport/lib/active_support/file_evented_update_checker.rb @@ -17,8 +17,8 @@ module ActiveSupport @updated = false @lcsp = @ph.longest_common_subpath(@dirs.keys) - if (watch_dirs = base_directories).any? - Listen.to(*watch_dirs, &method(:changed)).start + if (dtw = directories_to_watch).any? + Listen.to(*dtw, &method(:changed)).start end end @@ -71,14 +71,15 @@ module ActiveSupport end end - # TODO: Better return a list of non-nested directories. - def base_directories - [].tap do |bd| - bd.concat @files.map {|f| @ph.existing_parent(f.dirname)} - bd.concat @dirs.keys.map {|dir| @ph.existing_parent(dir)} - bd.compact! - bd.uniq! - end + def directories_to_watch + bd = [] + + bd.concat @files.map {|f| @ph.existing_parent(f.dirname)} + bd.concat @dirs.keys.map {|dir| @ph.existing_parent(dir)} + bd.compact! + bd.uniq! + + @ph.filter_out_descendants(bd) end class PathHelper @@ -133,6 +134,31 @@ module ActiveSupport end end end + + # Filters out directories which are descendants of others in the collection (stable). + def filter_out_descendants(directories) + return directories if directories.length < 2 + + sorted = directories.sort_by {|dir| dir.each_filename.to_a.length} + descendants = [] + + until sorted.empty? + directory = sorted.shift + + sorted.each do |candidate_to_descendant| + if candidate_to_descendant.to_path.start_with?(directory.to_path) + dparts = directory.each_filename.to_a + cparts = candidate_to_descendant.each_filename.to_a + + if cparts[0, dparts.length] == dparts + descendants << candidate_to_descendant + end + end + end + end + + directories - descendants + end end end end -- cgit v1.2.3 From 8793f77c4a96197bb0f5e29b00828bf5903fcad7 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Tue, 10 Nov 2015 07:15:30 -0800 Subject: simplifies PathHelper with a Pathname refinement --- .../active_support/file_evented_update_checker.rb | 44 +++++++++++----------- 1 file changed, 22 insertions(+), 22 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/file_evented_update_checker.rb b/activesupport/lib/active_support/file_evented_update_checker.rb index 262e801ce3..27da2e1572 100644 --- a/activesupport/lib/active_support/file_evented_update_checker.rb +++ b/activesupport/lib/active_support/file_evented_update_checker.rb @@ -83,6 +83,14 @@ module ActiveSupport end class PathHelper + using Module.new { + refine Pathname do + def ascendant_of?(other) + other.to_s =~ /\A#{Regexp.quote(to_s)}#{Pathname::SEPARATOR_PAT}?/ + end + end + } + def xpath(path) Pathname.new(path).expand_path end @@ -96,25 +104,24 @@ module ActiveSupport def longest_common_subpath(paths) return if paths.empty? - csp = Pathname.new(paths[0]) + lcsp = Pathname.new(paths[0]) paths[1..-1].each do |path| loop do - break if path.ascend do |ascendant| - break true if ascendant == csp - end + break if lcsp.ascendant_of?(path) - if csp.root? - # A root directory is not an ascendant of path. This may happen - # if there are paths in different drives on Windows. + if lcsp.root? + # If we get here a root directory is not an ascendant of path. + # This may happen if there are paths in different drives on + # Windows. return else - csp = csp.parent + lcsp = lcsp.parent end end end - csp + lcsp end # Returns the deepest existing ascendant, which could be the argument itself. @@ -139,22 +146,15 @@ module ActiveSupport def filter_out_descendants(directories) return directories if directories.length < 2 - sorted = directories.sort_by {|dir| dir.each_filename.to_a.length} + sorted_by_nparts = directories.sort_by {|dir| dir.each_filename.to_a.length} descendants = [] - until sorted.empty? - directory = sorted.shift + until sorted_by_nparts.empty? + dir = sorted_by_nparts.shift - sorted.each do |candidate_to_descendant| - if candidate_to_descendant.to_path.start_with?(directory.to_path) - dparts = directory.each_filename.to_a - cparts = candidate_to_descendant.each_filename.to_a - - if cparts[0, dparts.length] == dparts - descendants << candidate_to_descendant - end - end - end + descendants.concat sorted_by_nparts.select { |possible_descendant| + dir.ascendant_of?(possible_descendant) + } end directories - descendants -- cgit v1.2.3 From 4e1cff0e20c473edcd5cbb3ca344284bb5f428c0 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Tue, 10 Nov 2015 07:32:08 -0800 Subject: indents private methods as per our guidelines --- .../active_support/file_evented_update_checker.rb | 56 +++++++++++----------- 1 file changed, 28 insertions(+), 28 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/file_evented_update_checker.rb b/activesupport/lib/active_support/file_evented_update_checker.rb index 27da2e1572..8f734ac4e4 100644 --- a/activesupport/lib/active_support/file_evented_update_checker.rb +++ b/activesupport/lib/active_support/file_evented_update_checker.rb @@ -41,46 +41,46 @@ module ActiveSupport private - def changed(modified, added, removed) - unless updated? - @updated = (modified + added + removed).any? {|f| watching?(f)} + def changed(modified, added, removed) + unless updated? + @updated = (modified + added + removed).any? {|f| watching?(f)} + end end - end - def watching?(file) - file = @ph.xpath(file) + def watching?(file) + file = @ph.xpath(file) - return true if @files.member?(file) - return false if file.directory? + return true if @files.member?(file) + return false if file.directory? - ext = @ph.normalize_extension(file.extname) - dir = file.dirname + ext = @ph.normalize_extension(file.extname) + dir = file.dirname - loop do - if @dirs.fetch(dir, []).include?(ext) - break true - else - if @lcsp - break false if dir == @lcsp + loop do + if @dirs.fetch(dir, []).include?(ext) + break true else - break false if dir.root? - end + if @lcsp + break false if dir == @lcsp + else + break false if dir.root? + end - dir = dir.parent + dir = dir.parent + end end end - end - def directories_to_watch - bd = [] + def directories_to_watch + bd = [] - bd.concat @files.map {|f| @ph.existing_parent(f.dirname)} - bd.concat @dirs.keys.map {|dir| @ph.existing_parent(dir)} - bd.compact! - bd.uniq! + bd.concat @files.map {|f| @ph.existing_parent(f.dirname)} + bd.concat @dirs.keys.map {|dir| @ph.existing_parent(dir)} + bd.compact! + bd.uniq! - @ph.filter_out_descendants(bd) - end + @ph.filter_out_descendants(bd) + end class PathHelper using Module.new { -- cgit v1.2.3 From fe918b3bc4554693e16a536c91480db92ea35440 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Tue, 10 Nov 2015 07:38:38 -0800 Subject: simplifies the implementation of existing parent --- .../lib/active_support/file_evented_update_checker.rb | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/file_evented_update_checker.rb b/activesupport/lib/active_support/file_evented_update_checker.rb index 8f734ac4e4..51c8cf5690 100644 --- a/activesupport/lib/active_support/file_evented_update_checker.rb +++ b/activesupport/lib/active_support/file_evented_update_checker.rb @@ -126,19 +126,8 @@ module ActiveSupport # Returns the deepest existing ascendant, which could be the argument itself. def existing_parent(dir) - loop do - if dir.directory? - break dir - else - if dir.root? - # Edge case in which not even the root exists. For example, Windows - # paths could have a non-existing drive letter. Since the parent of - # root is root, we need to break to prevent an infinite loop. - break - else - dir = dir.parent - end - end + dir.ascend do |ascendant| + break ascendant if ascendant.directory? end end -- cgit v1.2.3 From 0a2e9b02d60ef3bf1ec1dc9d032d25aefc81987e Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Wed, 11 Nov 2015 05:23:29 +0100 Subject: rewrites bare loop as until --- activesupport/lib/active_support/file_evented_update_checker.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/file_evented_update_checker.rb b/activesupport/lib/active_support/file_evented_update_checker.rb index 51c8cf5690..d63c8e2438 100644 --- a/activesupport/lib/active_support/file_evented_update_checker.rb +++ b/activesupport/lib/active_support/file_evented_update_checker.rb @@ -107,9 +107,7 @@ module ActiveSupport lcsp = Pathname.new(paths[0]) paths[1..-1].each do |path| - loop do - break if lcsp.ascendant_of?(path) - + until lcsp.ascendant_of?(path) if lcsp.root? # If we get here a root directory is not an ascendant of path. # This may happen if there are paths in different drives on -- cgit v1.2.3 From c5846344ee2a3e9dcdbc2d26ca4ff5b6002d09b0 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Wed, 11 Nov 2015 05:24:06 +0100 Subject: adds a comment about how does filter_out_descendants preserve order --- activesupport/lib/active_support/file_evented_update_checker.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/file_evented_update_checker.rb b/activesupport/lib/active_support/file_evented_update_checker.rb index d63c8e2438..ca3788a187 100644 --- a/activesupport/lib/active_support/file_evented_update_checker.rb +++ b/activesupport/lib/active_support/file_evented_update_checker.rb @@ -144,6 +144,7 @@ module ActiveSupport } end + # Array#- preserves order. directories - descendants end end -- cgit v1.2.3 From 0eb6d198d3aaddb62b5241a707f6b1fcedfbedb8 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Wed, 11 Nov 2015 06:07:55 +0100 Subject: simplifies directories_to_watch --- .../lib/active_support/file_evented_update_checker.rb | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/file_evented_update_checker.rb b/activesupport/lib/active_support/file_evented_update_checker.rb index ca3788a187..8cb422ad36 100644 --- a/activesupport/lib/active_support/file_evented_update_checker.rb +++ b/activesupport/lib/active_support/file_evented_update_checker.rb @@ -72,14 +72,11 @@ module ActiveSupport end def directories_to_watch - bd = [] + dtw = (@files + @dirs.keys).map {|f| @ph.existing_parent(f)} + dtw.compact! + dtw.uniq! - bd.concat @files.map {|f| @ph.existing_parent(f.dirname)} - bd.concat @dirs.keys.map {|dir| @ph.existing_parent(dir)} - bd.compact! - bd.uniq! - - @ph.filter_out_descendants(bd) + @ph.filter_out_descendants(dtw) end class PathHelper -- cgit v1.2.3 From 40a1945d67869f660e7b07120a74106b0150ff23 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Wed, 11 Nov 2015 06:15:54 +0100 Subject: simplifies the implementation of #watching? --- .../active_support/file_evented_update_checker.rb | 28 ++++++++++------------ 1 file changed, 12 insertions(+), 16 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/file_evented_update_checker.rb b/activesupport/lib/active_support/file_evented_update_checker.rb index 8cb422ad36..6375de4a7d 100644 --- a/activesupport/lib/active_support/file_evented_update_checker.rb +++ b/activesupport/lib/active_support/file_evented_update_checker.rb @@ -50,23 +50,19 @@ module ActiveSupport def watching?(file) file = @ph.xpath(file) - return true if @files.member?(file) - return false if file.directory? - - ext = @ph.normalize_extension(file.extname) - dir = file.dirname - - loop do - if @dirs.fetch(dir, []).include?(ext) - break true - else - if @lcsp - break false if dir == @lcsp - else - break false if dir.root? + if @files.member?(file) + true + elsif file.directory? + false + else + ext = @ph.normalize_extension(file.extname) + + file.dirname.ascend do |dir| + if @dirs.fetch(dir, []).include?(ext) + break true + elsif dir == @lcsp || dir.root? + break false end - - dir = dir.parent end end end -- cgit v1.2.3 From f3516f171b7a975e1d3d8aaa37f721476fe49806 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Wed, 11 Nov 2015 06:38:18 +0100 Subject: applies code style guidelines --- .../lib/active_support/file_evented_update_checker.rb | 12 ++++++------ activesupport/lib/active_support/file_update_checker.rb | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/file_evented_update_checker.rb b/activesupport/lib/active_support/file_evented_update_checker.rb index 6375de4a7d..ae500d697f 100644 --- a/activesupport/lib/active_support/file_evented_update_checker.rb +++ b/activesupport/lib/active_support/file_evented_update_checker.rb @@ -4,13 +4,13 @@ require 'pathname' module ActiveSupport class FileEventedUpdateChecker #:nodoc: all - def initialize(files, dirs={}, &block) + def initialize(files, dirs = {}, &block) @ph = PathHelper.new - @files = files.map {|f| @ph.xpath(f)}.to_set + @files = files.map { |f| @ph.xpath(f) }.to_set @dirs = {} dirs.each do |dir, exts| - @dirs[@ph.xpath(dir)] = Array(exts).map {|ext| @ph.normalize_extension(ext)} + @dirs[@ph.xpath(dir)] = Array(exts).map { |ext| @ph.normalize_extension(ext) } end @block = block @@ -43,7 +43,7 @@ module ActiveSupport def changed(modified, added, removed) unless updated? - @updated = (modified + added + removed).any? {|f| watching?(f)} + @updated = (modified + added + removed).any? { |f| watching?(f) } end end @@ -68,7 +68,7 @@ module ActiveSupport end def directories_to_watch - dtw = (@files + @dirs.keys).map {|f| @ph.existing_parent(f)} + dtw = (@files + @dirs.keys).map { |f| @ph.existing_parent(f) } dtw.compact! dtw.uniq! @@ -126,7 +126,7 @@ module ActiveSupport def filter_out_descendants(directories) return directories if directories.length < 2 - sorted_by_nparts = directories.sort_by {|dir| dir.each_filename.to_a.length} + sorted_by_nparts = directories.sort_by { |dir| dir.each_filename.to_a.length } descendants = [] until sorted_by_nparts.empty? diff --git a/activesupport/lib/active_support/file_update_checker.rb b/activesupport/lib/active_support/file_update_checker.rb index 78b627c286..1fa9335080 100644 --- a/activesupport/lib/active_support/file_update_checker.rb +++ b/activesupport/lib/active_support/file_update_checker.rb @@ -35,7 +35,7 @@ module ActiveSupport # This method must also receive a block that will be called once a path # changes. The array of files and list of directories cannot be changed # after FileUpdateChecker has been initialized. - def initialize(files, dirs={}, &block) + def initialize(files, dirs = {}, &block) @files = files.freeze @glob = compile_glob(dirs) @block = block -- cgit v1.2.3 From f87774bb8b3249dbcd1f16035874f9e583df82d0 Mon Sep 17 00:00:00 2001 From: Michael Grosser Date: Sat, 7 Nov 2015 12:27:39 -0800 Subject: fast and consistent return when local_cache does not exist --- .../active_support/cache/strategy/local_cache.rb | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/cache/strategy/local_cache.rb b/activesupport/lib/active_support/cache/strategy/local_cache.rb index d521061004..fa007aad56 100644 --- a/activesupport/lib/active_support/cache/strategy/local_cache.rb +++ b/activesupport/lib/active_support/cache/strategy/local_cache.rb @@ -79,22 +79,26 @@ module ActiveSupport end def clear(options = nil) # :nodoc: - local_cache.clear(options) if local_cache + return super unless cache = local_cache + cache.clear(options) super end def cleanup(options = nil) # :nodoc: - local_cache.clear(options) if local_cache + return super unless cache = local_cache + cache.clear(options) super end def increment(name, amount = 1, options = nil) # :nodoc: + return super unless local_cache value = bypass_local_cache{super} set_cache_value(value, name, amount, options) value end def decrement(name, amount = 1, options = nil) # :nodoc: + return super unless local_cache value = bypass_local_cache{super} set_cache_value(value, name, amount, options) value @@ -120,13 +124,12 @@ module ActiveSupport end def set_cache_value(value, name, amount, options) # :nodoc: - if local_cache - local_cache.mute do - if value - local_cache.write(name, value, options) - else - local_cache.delete(name, options) - end + cache = local_cache + cache.mute do + if value + cache.write(name, value, options) + else + cache.delete(name, options) end end end -- cgit v1.2.3 From cff8b4951fd337075bd6079fa1a679428312baad Mon Sep 17 00:00:00 2001 From: Yuri Kasperovich Date: Mon, 9 Nov 2015 23:55:16 +0300 Subject: Minor fix in Module#mattr_reader documentation --- activesupport/lib/active_support/core_ext/module/attribute_accessors.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb b/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb index bf175a8a70..124f90dc0f 100644 --- a/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb +++ b/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb @@ -5,7 +5,7 @@ require 'active_support/core_ext/array/extract_options' # attributes. class Module # Defines a class attribute and creates a class and instance reader methods. - # The underlying the class variable is set to +nil+, if it is not previously + # The underlying class variable is set to +nil+, if it is not previously # defined. # # module HairColors -- cgit v1.2.3 From 2f4f4d2cf1e4c5a442459fc250daf66186d110fa Mon Sep 17 00:00:00 2001 From: Jon Pascoe Date: Tue, 10 Nov 2015 09:26:48 +0000 Subject: Add days_in_year method --- activesupport/lib/active_support/core_ext/time/calculations.rb | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index 82e003fc3b..675db8a36b 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -26,6 +26,12 @@ class Time end end + # Returns the number of days in the given year. + # If no year is specified, it will use the current year. + def days_in_year(year = current.year) + days_in_month(2, year) + 337 + end + # Returns Time.zone.now when Time.zone or config.time_zone are set, otherwise just returns Time.now. def current ::Time.zone ? ::Time.zone.now : ::Time.now -- cgit v1.2.3 From 1583c8290a0bbb44ae684974e8fdd459f548f0f4 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Tue, 10 Nov 2015 23:41:28 -0600 Subject: update docs for MessageEncryptor#new to recommend a KDF [ci skip] --- activesupport/lib/active_support/message_encryptor.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/message_encryptor.rb b/activesupport/lib/active_support/message_encryptor.rb index c82a13511e..2dde01c844 100644 --- a/activesupport/lib/active_support/message_encryptor.rb +++ b/activesupport/lib/active_support/message_encryptor.rb @@ -34,8 +34,8 @@ module ActiveSupport # Initialize a new MessageEncryptor. +secret+ must be at least as long as # the cipher key size. For the default 'aes-256-cbc' cipher, this is 256 # bits. If you are using a user-entered secret, you can generate a suitable - # key with OpenSSL::Digest::SHA256.new(user_secret).digest or - # similar. + # key by using ActiveSupport::KeyGenerator or a similar key + # derivation function. # # Options: # * :cipher - Cipher to use. Can be any cipher returned by -- cgit v1.2.3