Module: JustBackgammon::Common
- Defined in:
- lib/just_backgammon/common.rb
Overview
Common
Mixin that allows the class to have a custom load method It allows initializing by arrays, hashes or objects with the same class.
Instance Method Summary collapse
-
#load(argument) ⇒ Array<Object>, Object
Method that initializing an object by arrays, hashes or objects with the same class.
Instance Method Details
#load(argument) ⇒ Array<Object>, Object
Method that initializing an object by arrays, hashes or objects with the same class. Returns the object or array of objects. Will raise error if elements of array are not all the same Class. Will raise error if argument is not Hash, Array or the same Class.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/just_backgammon/common.rb', line 18 def load(argument) case argument when Hash self.new(argument) when Array case when argument.all? { |o| o.instance_of?(Hash) } argument.map { |o| self.new(o) } when argument.all? { |o| o.instance_of?(self) } argument else raise ArgumentError, "elements of array must have the same class" end when self argument else raise ArgumentError, "argument needs to be a Hash, Array or #{self}" end end |