Class: ImageHash

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll/image_optimizer/image_hash.rb

Instance Method Summary collapse

Constructor Details

#initialize(use_hash) ⇒ ImageHash

Returns a new instance of ImageHash.



2
3
4
# File 'lib/jekyll/image_optimizer/image_hash.rb', line 2

def initialize(use_hash)
  @use_hash=use_hash
end

Instance Method Details

#dir_search_pattern(dir, file) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/jekyll/image_optimizer/image_hash.rb', line 11

def dir_search_pattern(dir, file)
  if @use_hash
    pos=file.rindex('.')
    dir+'/'+file[0..pos-1]+'-*'+file[pos..-1]
  else
    dir+'/'+file
  end
end

#file_search_pattern(file) ⇒ Object



6
7
8
9
# File 'lib/jekyll/image_optimizer/image_hash.rb', line 6

def file_search_pattern(file)
  pos=file.rindex('/')
  self.dir_search_pattern(file[0..pos-1], file[pos+1..-1])
end

#hash?(file) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/jekyll/image_optimizer/image_hash.rb', line 36

def hash?(file)
  @use_hash ? file.match(/-[0-9a-f]{32}\./)!=nil : true
end

#name_with_hash(file) ⇒ Object



20
21
22
23
# File 'lib/jekyll/image_optimizer/image_hash.rb', line 20

def name_with_hash(file)
  pos=file.rindex('.')
  file[0..pos-1]+'-'+Digest::MD5.file(file).hexdigest+file[pos..-1]
end

#with_hash(file) ⇒ Object



25
26
27
28
29
30
# File 'lib/jekyll/image_optimizer/image_hash.rb', line 25

def with_hash(file)
  return file unless @use_hash
  new_file=self.name_with_hash(file)
  File.rename(file, new_file)
  new_file
end

#without_hash(file) ⇒ Object



32
33
34
# File 'lib/jekyll/image_optimizer/image_hash.rb', line 32

def without_hash(file)
  @use_hash ? file.sub(/-[0-9a-f]{32}\./, '.') : file
end