module ArrayOfHashes
Public Class Methods
included(klass)
click to toggle source
creates a singleton method on the included class which avoids having to put ‘required_keys’ into the included classes initialize method
# File lib/minigame/array_of_hashes.rb, line 23 def self.included(klass) class << klass attr_accessor :keys def required_keys(*keys) # force single keys to be an array keys = [keys].flatten keys.each do |x| raise 'Not a symbol' if !keys[0].is_a? Symbol end @keys=keys end end end
new(*array_of_hashes)
click to toggle source
# File lib/minigame/array_of_hashes.rb, line 4 def initialize(*array_of_hashes) array_of_hashes = [array_of_hashes].flatten @keys ||=[] # checks to see if required_keys was called # as a singleton method on the included class @keys = self.class.keys if self.class.keys check_array(array_of_hashes) @array_of_hashes=array_of_hashes end
Public Instance Methods
<<(val)
click to toggle source
# File lib/minigame/array_of_hashes.rb, line 63 def <<(val) check_keys(val) @array_of_hashes << val end
check_array(array_of_hashes)
click to toggle source
# File lib/minigame/array_of_hashes.rb, line 51 def check_array(array_of_hashes) raise 'Not an array' if !array_of_hashes.is_a? Array array_of_hashes.each{|x|check_keys(x)} end
check_keys(val)
click to toggle source
# File lib/minigame/array_of_hashes.rb, line 56 def check_keys(val) raise "Not a Hash" if val.class != Hash @keys.each do |x| raise "#{x.to_s.capitalize} required" if !val.keys.include?(x) end end
each(&block)
click to toggle source
# File lib/minigame/array_of_hashes.rb, line 14 def each(&block) @array_of_hashes.each do |matchup| block.call(matchup) end end
required_keys(*keys)
click to toggle source
required keys available on the instance
# File lib/minigame/array_of_hashes.rb, line 38 def required_keys(*keys) # force single keys to be an array keys = [keys].flatten keys.each do |x| raise 'Not a symbol' if !keys[0].is_a? Symbol end @keys = keys end
required_keys=(keys)
click to toggle source
# File lib/minigame/array_of_hashes.rb, line 47 def required_keys=(keys) required_keys(keys) end