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.
5 6 7 8 9 |
# File 'lib/check_files/checker.rb', line 5 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.
3 4 5 |
# File 'lib/check_files/checker.rb', line 3 def cache_path @cache_path end |
#pattern ⇒ Object (readonly)
Returns the value of attribute pattern.
3 4 5 |
# File 'lib/check_files/checker.rb', line 3 def pattern @pattern end |
Instance Method Details
#changed? ⇒ Boolean
27 28 29 30 31 32 33 34 35 |
# File 'lib/check_files/checker.rb', line 27 def changed? prev_digest = previous_digest if prev_digest prev_digest != current_digest else update_cache && false end end |
#compute_digest(entry) ⇒ Object
41 42 43 44 |
# File 'lib/check_files/checker.rb', line 41 def compute_digest(entry) content = File.exist?(entry) && File.read(entry) Digest::SHA1.hexdigest("#{entry}-#{content}") end |
#current_digest ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/check_files/checker.rb', line 11 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
23 24 25 |
# File 'lib/check_files/checker.rb', line 23 def previous_digest cache_path.file? && cache_path.read end |
#update_cache ⇒ Object
37 38 39 |
# File 'lib/check_files/checker.rb', line 37 def update_cache File.open(cache_path, "w") {|f| f << current_digest } end |