Class: ActiveAssets::ActiveSprites::Sprite

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

Defined Under Namespace

Modules: Orientation Classes: OrientationInvalid, ValidationError

Constant Summary collapse

REQUIRED_PROPS =
[:path, :stylesheet_path, :orientation]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSprite

Returns a new instance of Sprite.



26
27
28
29
30
31
32
# File 'lib/active_assets/active_sprites/sprite.rb', line 26

def initialize
  # Ordered Hash?
  @sprite_pieces = ActiveSupport::OrderedHash.new do |sprite_pieces, path|
    raise SpritePiece::ValidationError.new(nil, [:path]) if path.blank?
    sprite_pieces[path] = SpritePiece.new
  end
end

Instance Attribute Details

#matte_colorObject

Returns the value of attribute matte_color.



24
25
26
# File 'lib/active_assets/active_sprites/sprite.rb', line 24

def matte_color
  @matte_color
end

#nameObject (readonly)

Returns the value of attribute name.



24
25
26
# File 'lib/active_assets/active_sprites/sprite.rb', line 24

def name
  @name
end

#orientationObject (readonly)

Returns the value of attribute orientation.



24
25
26
# File 'lib/active_assets/active_sprites/sprite.rb', line 24

def orientation
  @orientation
end

#pathObject (readonly)

Returns the value of attribute path.



24
25
26
# File 'lib/active_assets/active_sprites/sprite.rb', line 24

def path
  @path
end

#qualityObject

Returns the value of attribute quality.



24
25
26
# File 'lib/active_assets/active_sprites/sprite.rb', line 24

def quality
  @quality
end

#stylesheet_pathObject (readonly)

Returns the value of attribute stylesheet_path.



24
25
26
# File 'lib/active_assets/active_sprites/sprite.rb', line 24

def stylesheet_path
  @stylesheet_path
end

#urlObject

Returns the value of attribute url.



24
25
26
# File 'lib/active_assets/active_sprites/sprite.rb', line 24

def url
  @url
end

Instance Method Details

#[](path) ⇒ Object



38
39
40
41
# File 'lib/active_assets/active_sprites/sprite.rb', line 38

def [](path)
  return nil unless @sprite_pieces.has_key?(path)
  @sprite_pieces[path]
end

#configure(sprite_path, stylesheet_path, options = {}, &blk) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/active_assets/active_sprites/sprite.rb', line 47

def configure(sprite_path, stylesheet_path, options = {}, &blk)
  @path ||= sprite_path
  @name = options.delete(:as) || sprite_path
  @stylesheet_path = stylesheet_path
  @orientation = options.delete(:orientation) || :vertical
  options.each {|k,v| send("#{k}=",v)}
  valid!
  instance_eval(&blk) if block_given?
  self
end

#has_sprite_piece_with_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/active_assets/active_sprites/sprite.rb', line 34

def has_sprite_piece_with_path?(path)
  @sprite_pieces.has_key?(path)
end

#sprite_piece(options, &blk) ⇒ Object Also known as: sp, _



58
59
60
61
62
63
# File 'lib/active_assets/active_sprites/sprite.rb', line 58

def sprite_piece(options, &blk)
  path, css_selector = options.find {|k, v| k.is_a?(String)}
  options.delete(path)
  mapping = SpritePiece::Mapping.new(path, css_selector)
  @sprite_pieces[path].configure(mapping, options, &blk)
end

#sprite_piecesObject



43
44
45
# File 'lib/active_assets/active_sprites/sprite.rb', line 43

def sprite_pieces
  @sprite_pieces.values
end

#valid!Object



78
79
80
81
# File 'lib/active_assets/active_sprites/sprite.rb', line 78

def valid!
  e = validation_error
  raise e if e
end

#valid?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/active_assets/active_sprites/sprite.rb', line 74

def valid?
  !validation_error
end

#validation_errorObject



67
68
69
70
71
72
# File 'lib/active_assets/active_sprites/sprite.rb', line 67

def validation_error
  missing_fields = REQUIRED_PROPS.reject {|prop| send(prop).present?}
  return OrientationInvalid.new(self, orientation) if orientation && ![:vertical, :horizontal].include?(orientation.to_sym)
  return if missing_fields.empty?
  ValidationError.new(self, missing_fields)
end