Class: Object
- Inherits:
- BasicObject
- Extended by:
- GollyUtils::AttrDeclarative
- Defined in:
- lib/golly-utils/ruby_ext/deep_dup.rb,
lib/golly-utils/ruby_ext/classes_and_types.rb
Defined Under Namespace
Classes: TypeValidationError
Instance Method Summary collapse
-
#deep_dup ⇒ Object
Creates a deep copy of the object.
-
#superclasses ⇒ Array<Class>
Returns the class hierarchy of a given instance or class.
-
#validate_type!(name = nil, *valid_classes) ⇒ self
Validates the type of the current object.
Methods included from GollyUtils::AttrDeclarative
Instance Method Details
#deep_dup ⇒ Object
Creates a deep copy of the object. Where supported (arrays and hashes by default), object state will be duplicated and used, rather than the original and duplicate objects sharing the same state.
4 5 6 |
# File 'lib/golly-utils/ruby_ext/deep_dup.rb', line 4 def deep_dup dup end |
#superclasses ⇒ Array<Class>
Returns the class hierarchy of a given instance or class.
45 46 47 48 49 50 51 52 53 |
# File 'lib/golly-utils/ruby_ext/classes_and_types.rb', line 45 def superclasses if self == BasicObject [self] elsif self.is_a? Class [self] + self.superclass.superclasses else self.class.superclasses end end |
#validate_type!(name = nil, *valid_classes) ⇒ self
Validates the type of the current object.
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/golly-utils/ruby_ext/classes_and_types.rb', line 77 def validate_type!(*args) name= args.first.is_a?(String) || args.first.is_a?(Symbol) ? args.shift : nil classes= args.map{|a| RUBY_PRIMATIVE_CLASSES[a] || a } raise "You must specify at least one valid class." if classes.empty? unless classes.any?{|c| self.is_a? c } for_name= name ? " for #{name}" : '' raise TypeValidationError, "Invalid type#{for_name}: #{self.class}\nValid types are: #{classes.map(&:to_s).sort.join ', '}." end self end |