Class: ResizeCss

Inherits:
Object
  • Object
show all
Defined in:
lib/world_flags/tools/resize_css.rb

Overview

use ImageMagick to Resize, using 32px version as base convert flags32.png -resize 75% flags24.png convert flags32_semi.png -resize 75% flags24_semi.png

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_size, new_size) ⇒ ResizeCss

Returns a new instance of ResizeCss.



9
10
11
12
# File 'lib/world_flags/tools/resize_css.rb', line 9

def initialize base_size, new_size
  @base_size = base_size
  @new_size = new_size
end

Instance Attribute Details

#base_sizeObject (readonly)

Returns the value of attribute base_size.



7
8
9
# File 'lib/world_flags/tools/resize_css.rb', line 7

def base_size
  @base_size
end

#new_sizeObject (readonly)

Returns the value of attribute new_size.



7
8
9
# File 'lib/world_flags/tools/resize_css.rb', line 7

def new_size
  @new_size
end

Instance Method Details

#execute(options = {:ext => 'css', :semi => false}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/world_flags/tools/resize_css.rb', line 14

def execute options = {:ext => 'css', :semi => false}
  semi = options[:semi] ? '-semi': ''
  ext = options[:ext] ? options[:ext] : 'css'
  src_file_path = File.expand_path "../../../vendor/assets/stylesheets/flags/flags#{base_size}#{semi}.#{ext}.erb", File.dirname(__FILE__)
  target_file_path = File.expand_path "../../../vendor/assets/stylesheets/flags/flags#{new_size}#{semi}.#{ext}.erb", File.dirname(__FILE__)

  lines = File.open(src_file_path).readlines

  img_exp = "flags#{base_size}"
  replace_img = "flags#{new_size}"

  repositioned_css = lines.map do |line|
    pos_match = line.match /.+position:0 -(\d+)px.*/
    img_match = line.match /#{img_exp}/
    new_line = line.sub(/#{img_exp}/, replace_img) if img_match
    new_line = replace_pos pos_match, line if pos_match
    new_line ||= line
    new_line
  end

  File.open(target_file_path, 'w') do |f|
    f.puts repositioned_css
  end
end

#replace_pos(pos_match, line) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/world_flags/tools/resize_css.rb', line 39

def replace_pos pos_match, line
  pos_y = pos_match[1].to_i        
  new_pos_y = (pos_y * 0.75).to_i.to_s
  # puts "pos: #{pos_y} -> #{new_pos_y}"
  rest = line[10..-1]
  rest = rest.sub /#{pos_y}/, new_pos_y.to_s
  line[0..10] + rest
end