spritely 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b90bec681ab9efe494b5e246a9e7b036bd9e5998
4
- data.tar.gz: 424ccf4da0ca02416f9f9f7cb9a63ec86ab1fe89
3
+ metadata.gz: 7abbb12ca655564072d814659941ccbeaf8693f6
4
+ data.tar.gz: a69e81ff56a194d7b65c1f2b21d704151435a7d1
5
5
  SHA512:
6
- metadata.gz: 4e836ffa7fd997b44b44d0f81d71373a581f1dab757c9c2e3aa006d74096a524bb6853ebd3b273d44fe801153c9c0fd8fa64dd307d37261bc8cdc6304e659ea1
7
- data.tar.gz: 63ffcfa28376d8bbd936dcfda0063677112e2b9b772ab56abf86600f5db41ee05ad8564f316b41a1f7e55dc38c4d749ba4f424fa3343cc29726417aeb4504411
6
+ metadata.gz: 33b650c6227ad170a8a6b4b97981cb74da9581c85595b8f5d3d39940505f457e3fc88218f35a01d702792d284a3ac2af136134a92ec19c6ebf76236e67fdca9a
7
+ data.tar.gz: 4e83cde6681229d832785381b4d86a61c9a49f7902912554238df0cfccb07d9036740b2e7af00055f1c4b91073bceec26795ad7f6c7cbf7673c323341d77a938
@@ -3,13 +3,14 @@ require 'spritely/image'
3
3
  module Spritely
4
4
  class ImageSet
5
5
  attr_accessor :top
6
- attr_reader :path, :options, :data, :width, :height
6
+ attr_reader :path, :options, :data, :width, :height, :left
7
7
 
8
8
  def initialize(path, options)
9
9
  @path = path
10
10
  @options = options
11
11
  @data = File.read(path)
12
12
  @width, @height = data[0x10..0x18].unpack('NN')
13
+ @left = 0
13
14
  end
14
15
 
15
16
  def name
@@ -20,10 +21,6 @@ module Spritely
20
21
  @images ||= []
21
22
  end
22
23
 
23
- def left
24
- 0
25
- end
26
-
27
24
  def outer_height
28
25
  height + spacing
29
26
  end
@@ -36,13 +33,19 @@ module Spritely
36
33
  !!options[:repeat]
37
34
  end
38
35
 
36
+ def right?
37
+ options[:position] == 'right'
38
+ end
39
+
39
40
  def position_in!(collection_width)
40
41
  if repeated?
41
- left = 0
42
- while left < collection_width
43
- add_image!(left)
44
- left += width
42
+ left_position = 0
43
+ while left_position < collection_width
44
+ add_image!(left_position)
45
+ left_position += width
45
46
  end
47
+ elsif right?
48
+ add_image!(@left = collection_width - width)
46
49
  else
47
50
  add_image!(0)
48
51
  end
@@ -50,10 +53,10 @@ module Spritely
50
53
 
51
54
  private
52
55
 
53
- def add_image!(left)
56
+ def add_image!(left_position)
54
57
  images << Image.new(data).tap do |image|
55
58
  image.top = top
56
- image.left = left
59
+ image.left = left_position
57
60
  end
58
61
  end
59
62
  end
@@ -4,7 +4,7 @@ require 'active_support/core_ext/hash/slice'
4
4
 
5
5
  module Spritely
6
6
  class Options < Struct.new(:hash)
7
- GLOBAL_OPTIONS = [:spacing]
7
+ GLOBAL_OPTIONS = [:spacing, :position]
8
8
 
9
9
  def cache_key
10
10
  stripped_hash.to_s
@@ -23,8 +23,10 @@ module Spritely
23
23
  def options
24
24
  @options ||= stripped_hash.except(*GLOBAL_OPTIONS).inject({}) do |h, (key, value)|
25
25
  split_key = key.to_s.split('_')
26
- option = global_options.merge(split_key.pop.to_sym => value)
27
- h.deep_merge!(split_key.join('-') => option)
26
+ option = {split_key.pop.to_sym => value}
27
+ image = split_key.join('-')
28
+ h[image] ||= global_options.dup
29
+ h.deep_merge!(image => option)
28
30
  end
29
31
  end
30
32
 
@@ -22,7 +22,7 @@ module Spritely
22
22
  def spritely_position(sprite_map, image_name)
23
23
  image = find_image(sprite_map, image_name)
24
24
 
25
- x = Sass::Script::Number.new(image.left, image.left == 0 ? [] : ['px'])
25
+ x = Sass::Script::Number.new(-image.left, image.left == 0 ? [] : ['px'])
26
26
  y = Sass::Script::Number.new(-image.top, image.top == 0 ? [] : ['px'])
27
27
 
28
28
  Sass::Script::List.new([x, y], :space)
@@ -1,3 +1,3 @@
1
1
  module Spritely
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
Binary file
@@ -2,6 +2,7 @@ $application-sprite: spritely-map("application/*.png",
2
2
  $background-repeat: true,
3
3
  $football-spacing: 100px,
4
4
  $mario-spacing: 10px,
5
+ $mario-position: right,
5
6
  $spacing: 5px
6
7
  );
7
8
 
@@ -18,7 +18,7 @@ describe 'Stylesheet generation', :integration do
18
18
  it { should include(<<-CSS.strip_heredoc
19
19
  #mario {
20
20
  background-image: url(/assets/sprites/application.png);
21
- background-position: 0 -728px;
21
+ background-position: -150px -728px;
22
22
  width: 200px;
23
23
  height: 214px;
24
24
  }
@@ -3,7 +3,7 @@ require 'ostruct'
3
3
 
4
4
  describe Spritely::ImageSet do
5
5
  let(:path) { "#{__dir__}/../fixtures/test/foo.png" }
6
- let(:options) { {repeat: true, spacing: 10} }
6
+ let(:options) { {repeat: true, spacing: 10, position: 'right'} }
7
7
 
8
8
  subject { Spritely::ImageSet.new(path, options) }
9
9
 
@@ -45,6 +45,22 @@ describe Spritely::ImageSet do
45
45
  end
46
46
  end
47
47
 
48
+ describe '#right?' do
49
+ it { should be_right }
50
+
51
+ context 'position option is passed as left' do
52
+ let(:options) { {position: 'left'} }
53
+
54
+ it { should_not be_right }
55
+ end
56
+
57
+ context 'position option is not passed' do
58
+ let(:options) { {} }
59
+
60
+ it { should_not be_right }
61
+ end
62
+ end
63
+
48
64
  describe '#position_in!' do
49
65
  class ImageDouble
50
66
  attr_accessor :top, :left
@@ -67,6 +83,36 @@ describe Spritely::ImageSet do
67
83
  expect(second_image.top).to eq(123)
68
84
  expect(second_image.left).to eq(1)
69
85
  end
86
+
87
+ context 'it is also positioned to the right' do
88
+ let(:options) { {position: 'right', repeat: true} }
89
+
90
+ its(:left) { should eq(0) }
91
+
92
+ it 'should ignore the position option' do
93
+ expect(first_image.top).to eq(123)
94
+ expect(first_image.left).to eq(0)
95
+ expect(second_image.top).to eq(123)
96
+ expect(second_image.left).to eq(1)
97
+ end
98
+ end
99
+ end
100
+
101
+ context 'the image is positioned to the right' do
102
+ let(:options) { {position: 'right'} }
103
+ let(:image) { ImageDouble.new }
104
+
105
+ before do
106
+ allow(Spritely::Image).to receive(:new).with(File.read(path)).and_return(image)
107
+ subject.position_in!(100)
108
+ end
109
+
110
+ its(:left) { should eq(99) }
111
+
112
+ it 'should set the position of the image' do
113
+ expect(image.top).to eq(123)
114
+ expect(image.left).to eq(99)
115
+ end
70
116
  end
71
117
 
72
118
  context 'the image is not repeated' do
@@ -2,26 +2,28 @@ require 'spec_helper'
2
2
 
3
3
  describe Spritely::Options do
4
4
  let(:hash) { {
5
+ 'some_new_image_spacing' => Sass::Script::Number.new(789),
5
6
  'some_new_image_x' => Sass::Script::Number.new(123),
6
7
  'some_new_image_y' => Sass::Script::Number.new(456),
7
- 'some_new_image_spacing' => Sass::Script::Number.new(789),
8
+ 'some_new_image_position' => Sass::Script::String.new('right'),
8
9
  'another_image_repeat' => Sass::Script::Bool.new(true),
9
10
  'yet_another_image_repeat' => Sass::Script::Bool.new(false),
10
- 'spacing' => Sass::Script::Number.new(901)
11
+ 'spacing' => Sass::Script::Number.new(901),
12
+ 'position' => Sass::Script::String.new('left')
11
13
  } }
12
14
 
13
15
  subject(:options) { Spritely::Options.new(hash) }
14
16
 
15
- its(:inspect) { should eq("#<Spritely::Options global_options=#{{spacing: 901}} options=#{{'some-new-image' => {spacing: 789, x: 123, y: 456}, 'another-image' => {spacing: 901, repeat: true}, 'yet-another-image' => {spacing: 901, repeat: false}}}>") }
16
- its(:cache_key) { should eq({some_new_image_x: 123, some_new_image_y: 456, some_new_image_spacing: 789, another_image_repeat: true, yet_another_image_repeat: false, spacing: 901}.to_s) }
17
+ its(:inspect) { should eq("#<Spritely::Options global_options=#{{spacing: 901, position: 'left'}} options=#{{'some-new-image' => {spacing: 789, position: 'right', x: 123, y: 456}, 'another-image' => {spacing: 901, position: 'left', repeat: true}, 'yet-another-image' => {spacing: 901, position: 'left', repeat: false}}}>") }
18
+ its(:cache_key) { should eq({some_new_image_spacing: 789, some_new_image_x: 123, some_new_image_y: 456, some_new_image_position: 'right', another_image_repeat: true, yet_another_image_repeat: false, spacing: 901, position: 'left'}.to_s) }
17
19
 
18
- its(['some-new-image']) { should eq({spacing: 789, x: 123, y: 456}) }
19
- its(['another-image']) { should eq({spacing: 901, repeat: true}) }
20
- its(['yet-another-image']) { should eq({spacing: 901, repeat: false}) }
20
+ its(['some-new-image']) { should eq({spacing: 789, position: 'right', x: 123, y: 456}) }
21
+ its(['another-image']) { should eq({spacing: 901, repeat: true, position: 'left'}) }
22
+ its(['yet-another-image']) { should eq({spacing: 901, repeat: false, position: 'left'}) }
21
23
 
22
24
  describe '#[]' do
23
25
  it 'should fall back to an empty hash' do
24
- expect(options[:unknown]).to eq({spacing: 901})
26
+ expect(options[:unknown]).to eq({spacing: 901, position: 'left'})
25
27
  end
26
28
  end
27
29
  end
@@ -42,18 +42,18 @@ describe Spritely::SassFunctions do
42
42
  end
43
43
 
44
44
  describe '#spritely_position' do
45
- let(:image) { double(left: 0, top: 12) }
45
+ let(:image) { double(left: 10, top: 12) }
46
46
 
47
47
  subject { evaluate("spritely-position(spritely-map('test/*.png'), 'bar')") }
48
48
 
49
49
  include_examples "a sprite function that checks image existence"
50
50
 
51
- it { should eq('0 -12px') }
51
+ it { should eq('-10px -12px') }
52
52
 
53
- context 'the left is not 0' do
54
- let(:image) { double(left: 10, top: 12) }
53
+ context 'the positions are both 0' do
54
+ let(:image) { double(left: 0, top: 0) }
55
55
 
56
- it { should eq('10px -12px') }
56
+ it { should eq('0 0') }
57
57
  end
58
58
  end
59
59
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spritely
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Robbin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-03 00:00:00.000000000 Z
11
+ date: 2014-06-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: chunky_png