Class: CheckFiles::Checker
- Inherits:
-
Object
- Object
- CheckFiles::Checker
- Defined in:
- lib/check_files/checker.rb
Instance Attribute Summary collapse
-
#cache_path ⇒ Object
readonly
Returns the value of attribute cache_path.
-
#pattern ⇒ Object
readonly
Returns the value of attribute pattern.
Instance Method Summary collapse
- #changed? ⇒ Boolean
- #compute_digest(entry) ⇒ Object
- #current_digest ⇒ Object
-
#initialize(pattern) ⇒ Checker
constructor
A new instance of Checker.
- #previous_digest ⇒ Object
- #update_cache ⇒ Object
Constructor Details
#initialize(pattern) ⇒ Checker
Returns a new instance of Checker.
7 8 9 10 11 |
# File 'lib/check_files/checker.rb', line 7 def initialize(pattern) @pattern = pattern pattern_digest = Digest::SHA1.hexdigest(pattern) @cache_path = Rails.root.join("tmp/cache/#{pattern_digest}") end |
Instance Attribute Details
#cache_path ⇒ Object (readonly)
Returns the value of attribute cache_path.
5 6 7 |
# File 'lib/check_files/checker.rb', line 5 def cache_path @cache_path end |
#pattern ⇒ Object (readonly)
Returns the value of attribute pattern.
5 6 7 |
# File 'lib/check_files/checker.rb', line 5 def pattern @pattern end |
Instance Method Details
#changed? ⇒ Boolean
29 30 31 32 33 34 35 36 37 |
# File 'lib/check_files/checker.rb', line 29 def changed? prev_digest = previous_digest if prev_digest prev_digest != current_digest else update_cache && false end end |
#compute_digest(entry) ⇒ Object
44 45 46 47 |
# File 'lib/check_files/checker.rb', line 44 def compute_digest(entry) content = File.exist?(entry) && File.read(entry) Digest::SHA1.hexdigest("#{entry}-#{content}") end |
#current_digest ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/check_files/checker.rb', line 13 def current_digest entries = Dir[pattern].sort entries_digest = Digest::SHA1.hexdigest(entries.join) if pattern.end_with?("/*") entries_digest else content_digest = entries.map {|entry| compute_digest(entry) }.join Digest::SHA1.hexdigest("#{entries_digest}#{content_digest}") end end |
#previous_digest ⇒ Object
25 26 27 |
# File 'lib/check_files/checker.rb', line 25 def previous_digest cache_path.file? && cache_path.read end |
#update_cache ⇒ Object
39 40 41 42 |
# File 'lib/check_files/checker.rb', line 39 def update_cache FileUtils.mkdir_p(File.dirname(cache_path)) File.open(cache_path, "w") {|io| io << current_digest } end |