Skip to content

Commit

Permalink
take underscored image names into account while simplifying options p…
Browse files Browse the repository at this point in the history
…arsing

* takes advantage of `rpartition` to more efficiently (and clearly) split the options
* swaps where we `to_s` the hash keys in favor of symbolizing in fewer places
  • Loading branch information
agrobbin committed Jun 6, 2014
1 parent c36fe6f commit d3283c9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
15 changes: 7 additions & 8 deletions lib/spritely/options.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
require 'active_support/core_ext/hash/deep_merge'
require 'active_support/core_ext/hash/except'
require 'active_support/core_ext/hash/keys'
require 'active_support/core_ext/hash/slice'

module Spritely
class Options < Struct.new(:hash)
GLOBAL_OPTIONS = [:spacing, :position]
GLOBAL_OPTIONS = ['spacing', 'position']

def cache_key
stripped_hash.to_s
Expand All @@ -15,28 +16,26 @@ def inspect
end

def [](key)
options[key] || global_options
options[key.gsub('-', '_')] || global_options
end

private

def options
@options ||= stripped_hash.except(*GLOBAL_OPTIONS).inject({}) do |h, (key, value)|
split_key = key.to_s.split('_')
option = {split_key.pop.to_sym => value}
image = split_key.join('-')
image, _, option = key.rpartition('_')
h[image] ||= global_options.dup
h.deep_merge!(image => option)
h.deep_merge!(image => {option.to_sym => value})
end
end

def global_options
@global_options ||= stripped_hash.slice(*GLOBAL_OPTIONS)
@global_options ||= stripped_hash.slice(*GLOBAL_OPTIONS).symbolize_keys!
end

def stripped_hash
@stripped_hash ||= hash.inject({}) do |h, (key, sass_object)|
h.merge!(key.to_sym => sass_object.value)
h.merge!(key => sass_object.value)
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/spritely/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@

subject(:options) { Spritely::Options.new(hash) }

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}}}>") }
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) }
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}}}>") }
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) }

its(['some-new-image']) { should eq({spacing: 789, position: 'right', x: 123, y: 456}) }
its(['another-image']) { should eq({spacing: 901, repeat: true, position: 'left'}) }
its(['yet-another-image']) { should eq({spacing: 901, repeat: false, position: 'left'}) }

describe '#[]' do
it 'should fall back to an empty hash' do
expect(options[:unknown]).to eq({spacing: 901, position: 'left'})
expect(options['unknown']).to eq({spacing: 901, position: 'left'})
end
end
end

0 comments on commit d3283c9

Please sign in to comment.