Class: Kin::Sprites::IconSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/kin/sprites.rb

Overview

A simple class which represents a list of icons (wrapper around an array).

Instance Method Summary collapse

Constructor Details

#initialize(icons) ⇒ IconSet

Creates a new IconSet instance.

Parameters:

  • icons (Array<String>)

    An array of source icon files.



24
25
26
# File 'lib/kin/sprites.rb', line 24

def initialize(icons)
  @icons = icons.uniq
end

Instance Method Details

#[](*slice) ⇒ Set

Returns a slice of the set. Note: This returns a Set, not an IconSet.

Parameters:

  • slice (Numeric, Range)

    The element or range of elements to return.

Returns:

  • (Set)


80
81
82
# File 'lib/kin/sprites.rb', line 80

def [](*slice)
  @icons[*slice]
end

#each {|String| ... } ⇒ Object

Loops through, and yields, each icon in turn.

Yields:



59
60
61
# File 'lib/kin/sprites.rb', line 59

def each
  @icons.each { |i| yield i }
end

#hashString

Returns a hash for the icon list. Assuming the list contains the same icons, and in the same order, the hash will always be the same.

Returns:



90
91
92
# File 'lib/kin/sprites.rb', line 90

def hash
  Digest::SHA256.hexdigest(@icons.join("|"))
end

#lengthInteger

Returns the number of icons in this set.

Returns:

  • (Integer)


68
69
70
# File 'lib/kin/sprites.rb', line 68

def length
  @icons.length
end

#location_of(icon) ⇒ Integer

Returns the vertical offset of a given icon in a sprite.

Examples:

list = IconSet.new(%w( one two three ))
list.location_of('one')   # => 0
list.location_of('two')   # => 40
list.location_of('three') # => 80

Parameters:

  • The (String)

    name of an icon which appears in the list.

Returns:

  • (Integer)

Raises:

  • (ArgumentError)

    Raises an ArgumentError if the given icon does not appear in the list.



46
47
48
49
50
51
52
# File 'lib/kin/sprites.rb', line 46

def location_of(icon)
  unless @icons.include?(icon)
    raise ArgumentError, "Icon does not appear in the list: #{icon}"
  end

  @icons.index(icon) * 40
end