Class: Linner::Sprite

Inherits:
Object
  • Object
show all
Defined in:
lib/linner/sprite.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(images) ⇒ Sprite

Returns a new instance of Sprite.



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/linner/sprite.rb', line 18

def initialize(images)
  @images = images.sort do |a, b|
    diff = [b.width, b.height].max <=> [a.width, a.height].max
    diff = [b.width, b.height].min <=> [a.width, a.height].min if diff.zero?
    diff = b.height <=> a.height if diff.zero?
    diff = b.width <=> a.width if diff.zero?
    diff
  end

  @root = { :x => 0, :y => 0, :w => @images.first.width, :h => @images.first.height}
end

Instance Attribute Details

#imagesObject

Returns the value of attribute images.



16
17
18
# File 'lib/linner/sprite.rb', line 16

def images
  @images
end

#rootObject

Returns the value of attribute root.



16
17
18
# File 'lib/linner/sprite.rb', line 16

def root
  @root
end

Instance Method Details

#generate_style(config, name) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/linner/sprite.rb', line 43

def generate_style(config, name)
  selector = config["selector"] || ".icon-"
  url = config['url'] || config['path']
  @images.inject("") do |style, image|
    logical_path = Asset.new(image.path).logical_path
    selector_with_pseudo_class = logical_path.chomp(File.extname(logical_path))
      .gsub("/", "-")
      .gsub("_active", ".active")
      .gsub("_hover", ":hover")
      .gsub("_", "-")
    style <<
"#{selector}#{selector_with_pseudo_class} {
  width: #{image.width}px;
  height: #{image.height}px;
  background: url(#{File.join url, name}) -#{image.left}px -#{image.top}px no-repeat;
}
"
  end
end

#pack!Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/linner/sprite.rb', line 30

def pack!
  @images.each do |image|
    if block = find_block(@root, image)
      place_image(block, image)
      split_block(block, image)
    else
      @root = expand_root(@root, image)
      redo
    end
  end
  self
end