Module: Ray::ResourceSet

Included in:
Font, FontSet, Image, ImageSet, Music, MusicSet, Sound, SoundSet
Defined in:
lib/ray/resource_set.rb

Overview

Mixin used to cache resources. It allows to register different block to create the resources using a regexp:

add_set(/^http://(.+)$/) do |url| # captures are passed to the block
  ...
end

Custom arguments can also be specified:

add_set(/^http://(.+)/) do |url, priority| # arguments are passed at the end
  ...
end

Notice you would usually use extend instead of include to use this module.

Instance Method Summary collapse

Instance Method Details

#[](value, *params) ⇒ Object

Returns The object corresponding to a given list of parameters.

Returns:

  • The object corresponding to a given list of parameters.



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ray/resource_set.rb', line 34

def [](value, *params)
  if params.size != required_argument_count
    raise(ArgumentError, "wrong number of arguments (%d for %d)" %
          [params.size, required_argument_count])
  end

  set_hash.each do |regexp, hash|
    return hash[([value] + params)] if regexp =~ value
  end

  missing_pattern(value, *params)
end

#add_set(regexp, &block) ⇒ Object

Registers a block for handling some resources.



20
21
22
23
24
25
26
27
# File 'lib/ray/resource_set.rb', line 20

def add_set(regexp, &block)
  set_hash[regexp] = Hash.new do |hash, args|
    regexp =~ args.first

    block_args = $~.captures + args[1..-1]
    hash[args] = block.call(*block_args)
  end
end

#missing_pattern(string, *args) ⇒ Object

Method called when a string isn’t matched by any pattern. Raises a NoPatternError excpetion by default.

Raises:



88
89
90
# File 'lib/ray/resource_set.rb', line 88

def missing_pattern(string, *args)
  raise NoPatternError, "#{string.inspect} not macthed by any pattern"
end

#need_argument_count(n) ⇒ Object

Sets the required argument count. The number of arguments passed to [] must be equal to this + 1 (the first argument isn’t counted and is required)

Defaulted to 0.



77
78
79
# File 'lib/ray/resource_set.rb', line 77

def need_argument_count(n)
  @set_argument_count = n
end

#reject! { ... } ⇒ Object Also known as: delete_if

Removes all the objects from the array but those for which block returned true.

Yields:

  • *arguments, object (See #select!)



66
67
68
69
# File 'lib/ray/resource_set.rb', line 66

def reject!(&block)
  return Enumerator.new(self, :reject!) unless block_given?
  select! { |*args| !block.call(*args) }
end

#required_argument_countObject

Returns the required argument count.



82
83
84
# File 'lib/ray/resource_set.rb', line 82

def required_argument_count
  @set_argument_count ||= 0
end

#select! {|*arguments, object| ... } ⇒ Object

Selects objects matching a condition.

Yields:

  • *arguments, object

Yield Parameters:

  • *arguments

    All the arguments used to access that object

  • object

    the actual resource



52
53
54
55
56
57
58
59
60
# File 'lib/ray/resource_set.rb', line 52

def select!(&block)
  return Enumerator.new(self, :select!) unless block_given?

  set_hash.each do |regexp, hash|
    hash.delete_if do |key, val|
      !block.call(*(key + [val]))
    end
  end
end

#set_hashObject



29
30
31
# File 'lib/ray/resource_set.rb', line 29

def set_hash
  (@set_hash ||= {})
end