Class: RailsSprite::SpriteUtil

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_sprite/sprite_util.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ SpriteUtil

:css_extend => “”,

:root_path => '.',
:scope_name => "rails_xxx",
:recipe_path => "icons/16x16",
:file_extend => '.png',
:spacing => 10,
:image_to_folder => "app/assets/images",
:image_source_folder => 'app/assets/images/rails_xxx/sprite_sources',
:stylesheet_to => "app/assets/stylesheets/rails_xxx/sprite/icons/16x16.css.scss.erb",
:image_to_file_path => "rails_xxx/sprite/icons/16x16.png"


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rails_sprite/sprite_util.rb', line 23

def initialize options={}
  @root_path                = options.fetch(:root_path, ::Rails.root)

  @scope_name               = options.fetch(:scope_name, '')
  @scope_name               = "#{scope_name}/" unless scope_name.end_with?('/')

  @recipe_path              = options.fetch(:recipe_path, '')
  @recipe_path              = recipe_path.chomp('/') if recipe_path.end_with?('/')

  @file_extend              = options.fetch(:file_extend, '.png').downcase
  @css_extend               = options.fetch(:css_extend, ".css.scss.erb")

  @file_extend_name = file_extend.gsub('.', '')

  @spacing                  = options.fetch(:spacing, 10)
  @stylesheet_scope_name    = "#{scope_name}sprite/#{recipe_path}#{css_extend}"
  @stylesheet_to            = options.fetch(:stylesheet_to, "#{root_path}/app/assets/stylesheets/#{stylesheet_scope_name}")

  @image_source_folder      = options.fetch(
    :image_source_folder, "#{root_path}/app/assets/images/#{scope_name}sprite_sources/#{recipe_path}"
  )

  @image_to_folder          = options.fetch(:image_to_folder, "#{root_path}/app/assets/images")
  @image_scope_name         = "#{scope_name}sprite/#{recipe_path}#{file_extend}"
  @image_to_file_path       = options.fetch(:image_to_file_path, "#{image_to_folder}/#{image_scope_name}")

  @scope_str                = scope_name.chomp('/').gsub('/', '-')
  @recipe_str               = recipe_path.chomp('/').gsub('/', '-')

  @css_class_shared         = options.fetch(:css_class_shared, "rs-#{@scope_str}")
  @css_class_prefix         = options.fetch(:css_class_prefix, "rs-#{@scope_str}-#{@recipe_str}-")

  @zoom                     = options.fetch(:zoom, 1).to_i
end

Instance Attribute Details

#css_class_prefixObject (readonly)

Returns the value of attribute css_class_prefix.



5
6
7
# File 'lib/rails_sprite/sprite_util.rb', line 5

def css_class_prefix
  @css_class_prefix
end

#css_class_sharedObject (readonly)

Returns the value of attribute css_class_shared.



5
6
7
# File 'lib/rails_sprite/sprite_util.rb', line 5

def css_class_shared
  @css_class_shared
end

#css_extendObject (readonly)

Returns the value of attribute css_extend.



5
6
7
# File 'lib/rails_sprite/sprite_util.rb', line 5

def css_extend
  @css_extend
end

#file_extendObject (readonly)

Returns the value of attribute file_extend.



5
6
7
# File 'lib/rails_sprite/sprite_util.rb', line 5

def file_extend
  @file_extend
end

#file_extend_nameObject (readonly)

Returns the value of attribute file_extend_name.



5
6
7
# File 'lib/rails_sprite/sprite_util.rb', line 5

def file_extend_name
  @file_extend_name
end

#image_scope_nameObject (readonly)

Returns the value of attribute image_scope_name.



5
6
7
# File 'lib/rails_sprite/sprite_util.rb', line 5

def image_scope_name
  @image_scope_name
end

#image_source_folderObject (readonly)

Returns the value of attribute image_source_folder.



5
6
7
# File 'lib/rails_sprite/sprite_util.rb', line 5

def image_source_folder
  @image_source_folder
end

#image_to_file_pathObject (readonly)

Returns the value of attribute image_to_file_path.



5
6
7
# File 'lib/rails_sprite/sprite_util.rb', line 5

def image_to_file_path
  @image_to_file_path
end

#image_to_folderObject (readonly)

Returns the value of attribute image_to_folder.



5
6
7
# File 'lib/rails_sprite/sprite_util.rb', line 5

def image_to_folder
  @image_to_folder
end

#recipe_pathObject (readonly)

Returns the value of attribute recipe_path.



5
6
7
# File 'lib/rails_sprite/sprite_util.rb', line 5

def recipe_path
  @recipe_path
end

#root_pathObject (readonly)

Returns the value of attribute root_path.



5
6
7
# File 'lib/rails_sprite/sprite_util.rb', line 5

def root_path
  @root_path
end

#scope_nameObject (readonly)

Returns the value of attribute scope_name.



5
6
7
# File 'lib/rails_sprite/sprite_util.rb', line 5

def scope_name
  @scope_name
end

#spacingObject (readonly)

Returns the value of attribute spacing.



5
6
7
# File 'lib/rails_sprite/sprite_util.rb', line 5

def spacing
  @spacing
end

#stylesheet_scope_nameObject (readonly)

Returns the value of attribute stylesheet_scope_name.



5
6
7
# File 'lib/rails_sprite/sprite_util.rb', line 5

def stylesheet_scope_name
  @stylesheet_scope_name
end

#stylesheet_toObject (readonly)

Returns the value of attribute stylesheet_to.



5
6
7
# File 'lib/rails_sprite/sprite_util.rb', line 5

def stylesheet_to
  @stylesheet_to
end

#zoomObject (readonly)

Returns the value of attribute zoom.



5
6
7
# File 'lib/rails_sprite/sprite_util.rb', line 5

def zoom
  @zoom
end

Instance Method Details

#performObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/rails_sprite/sprite_util.rb', line 58

def perform
  file_infos = []

  #  puts "image_source_folder: #{image_source_folder}"
  #  puts "image_to_file_path: #{image_to_file_path}"
  #  puts "stylesheet_to: #{stylesheet_to}"

  counter = 0

  x = 0
  y = 0
  max_w = 0
  max_h = 0

  Dir.entries( image_source_folder ).each do |file_name|
    if file_name != '.' && file_name != '..' && file_name.end_with?(file_extend)
      file_path = "#{image_source_folder}/#{file_name}"

      if ::File.file?(file_path)
        file_name_split = file_name.split('.')
        file_name_split.pop
        file_purename = file_name_split.join('.')

        file_info = {
          :filepath => file_path,
          :filename => file_name,
          :file_purename => file_purename,
          :idx => counter
        }.merge(
          _library.load( file_path )
        )

        file_info[:x] = x
        file_info[:y] = y

        y += (spacing + file_info[:height])

        max_w = [max_w, file_info[:width]].max
        max_h = y

        file_infos << file_info

        counter += 1
      end
    end
  end

  _composite_images( :file_infos => file_infos, :max_w => max_w, :max_h => max_h )
  _composite_css( file_infos )
end