Module: Snow::Swizzle
Instance Method Summary collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/snow-math/swizzle.rb', line 11 def method_missing(sym, *args) chars = sym.to_s if chars =~ self.class.class_variable_get(:@@SWIZZLE_CHARS) mapping = self.class.class_variable_get(:@@SWIZZLE_MAPPING) arg_indices = chars.each_char.map { |char| index = mapping[char] if index.nil? raise ArgumentError, "No index mapping for swizzle character #{char} found" end index } swizzle_klass = mapping[chars.length] if swizzle_klass.nil? raise ArgumentError, "No swizzle class defined for #{chars.length} components" end self.class.class_exec(arg_indices, swizzle_klass) { |indices, klass| define_method(sym) { klass.new(indices.map { |index| self.fetch(index) }) } } return self.send(sym) end super sym, *args end |