lib/ray/dsl/matcher.rb in ray-0.1.0.pre1 vs lib/ray/dsl/matcher.rb in ray-0.1.0
- old
+ new
@@ -3,11 +3,11 @@
# allowing you to use them when you call on.
module Matchers
module_function
# @return [DSL::Matcher] An anonymous matcher, using your block to
- # know if the argument matches.
+ # know if the argument matches.
#
# @example
# on :foo, where { |i| i > 10 } do |i|
# puts "#{i} is greater than 10!"
# end
@@ -41,18 +41,22 @@
alias :=== :match?
end
end
- # Describes a new matcher.
+ # Describes a new matcher. It will be defined as a module function in
+ # {Ray::DSL::Matchers}.
#
- # @param [Symbol] name The name you'll use to call your matcher
- # @param [Proc] create_block a block called with the arguments of your matcher
- # method, and returning the block that will be used
- # to check if the condition is matched.
+ # @param [Symbol] name The name you'll use to create your matcher.
+
+ # @yield The block is called when creating the matcher.
+ # @yieldparam *args Arguments passed to the matcher.
+ # @yieldreturn [Proc] A proc returning taking a single argument and returning
+ # true when its argument should be matched by the matcher.
+ #
# @example
- # Ray.describe_matcher(:match, :string) do |regex|
+ # Ray.describe_matcher(:match) do |regex|
# lambda { |str| str =~ regex }
# end
def describe_matcher(name, &create_block)
Matchers.module_eval do
define_method(name) do |*args|
@@ -122,56 +126,65 @@
describe_matcher(:colliding_with) do |*args|
rect = args.size > 1 ? Ray::Rect.new(*args) : args.first.to_rect
lambda { |o| o.collide? rect }
end
- KEYS = Ray::Event.constants.inject({}) do |hash, const|
- if const =~ /^KEY_(.+)$/
- hash[$1.downcase.to_sym] = [Ray::Event.const_get(const)]
+ Keys = {}
+
+ Ray::Event.constants.each do |const|
+ if const =~ /^Key(.+)$/
+ Keys[$1.downcase.to_sym] = [Ray::Event.const_get(const)]
end
+ end
- hash
+ Ray::Event.constants.each do |const|
+ if const =~ /^Key(?:Num|Kp)(\d+)$/
+ (Keys[$1.to_sym] ||= []) << Ray::Event.const_get(const)
+ end
end
- KEYS[:number] = Ray::Event.constants.select { |c| c =~ /^KEY_\d$/ }.map do |c|
+ Keys[:number] = Ray::Event.constants.grep(/^KeyNum\d$/).map do |c|
Ray::Event.const_get(c)
end
- KEYS[:number] |= Ray::Event.constants.select { |c| c =~ /^KEY_KP\d$/ }.map do |c|
+ Keys[:number] |= Ray::Event.constants.grep(/^KeyKp\d$/).map do |c|
Ray::Event.const_get(c)
end
- KEYS[:letter] = Ray::Event.constants.select { |c| c =~ /^KEY_[a-z]$/ }.map do |c|
+ Keys[:letter] = Ray::Event.constants.grep(/^Key[A-Z]$/).map do |c|
Ray::Event.const_get(c)
end
- KEYS[:function] = Ray::Event.constants.select { |c| c =~ /^KEY_F\d$/ }.map do |c|
+ Keys[:function] = Ray::Event.constants.grep(/^KeyF\d+$/).map do |c|
Ray::Event.const_get(c)
end
- KEYS[:mod] = [Ray::Event::KEY_RSHIFT, Ray::Event::KEY_LSHIFT,
- Ray::Event::KEY_RCTRL, Ray::Event::KEY_LCTRL,
- Ray::Event::KEY_RALT, Ray::Event::KEY_LALT,
- Ray::Event::KEY_RSUPER, Ray::Event::KEY_LSUPER]
+ Keys[:mod] = [Ray::Event::KeyRShift, Ray::Event::KeyLShift,
+ Ray::Event::KeyRControl, Ray::Event::KeyLControl,
+ Ray::Event::KeyRMeta, Ray::Event::KeyLMeta,
+ Ray::Event::KeyRSuper, Ray::Event::KeyLSuper]
- KEYS[:arrow] = [Ray::Event::KEY_UP, Ray::Event::KEY_DOWN,
- Ray::Event::KEY_LEFT, Ray::Event::KEY_RIGHT]
+ Keys[:arrow] = [Ray::Event::KeyUp, Ray::Event::KeyDown,
+ Ray::Event::KeyLeft, Ray::Event::KeyRight]
- MOD = Ray::Event.constants.inject({}) do |hash, const|
- if const =~ /^KMOD_(.+)$/
- hash[$1.downcase.to_sym] = [Ray::Event.const_get(const)]
- end
+ Keys[:+] = [Ray::Event::KeyPlus]
+ Keys[:-] = [Ray::Event::KeyMinus]
- hash
+ Mod = {}
+
+ Ray::Event.constants.each do |const|
+ if const =~ /^Mod(.+)$/
+ Mod[$1.downcase.to_sym] = [Ray::Event.const_get(const)]
+ end
end
class Key
def initialize(name)
@symbol = name.to_sym
end
def to_a
- KEYS[@symbol]
+ Keys[@symbol]
end
def to_sym
@symbol
end