Module: I8::Struct
- Defined in:
- lib/nrser/labs/i8/struct.rb,
lib/nrser/labs/i8/struct/hash.rb,
lib/nrser/labs/i8/struct/vector.rb
Overview
‘I8::Struct` is an immutable rendition of the Ruby standard lib `::Struct` using the NRSER::Props system.
It’s intended to help make it quick and easy to create little immutable propertied objects, with all the type-checking and serialization stuff that comes with NRSER::Props.
‘Struct` itself is a module with a Struct.new method that creates new subclasses of either Hash or Vector, which themselves are respective subclasses of Hash and Vector, and include NRSER::Props::Immutable::Hash and NRSER::Props::Immutable::Vector.
### Identifying Classes Built by ‘I8::Struct`
Both Hash and Vector also include ‘I8::Struct` itself for the convenience of shit like:
Point = I8::Struct.new x: t.int, y: t.int
Point.included_modules.include? I8::Struct
# => true
p = Point.new x: 1, y: 2
p.is_a? I8::Struct
# => true
‘I8::Struct` has only class (static) methods, so no functionality is actually added from `include I8::Struct`.
### Gotchas
The code below *doesn’t* work how you might want or expect it:
Point = I8::Struct.new x: t.int, y: t.int
Point.is_a? I8::Struct
# => false
Opposed to how Class works:
C = Class.new
C.is_a? Class
# => true
However, this is the same as how the stdlib’s [::Struct][] behaves:
S = ::Struct.new :x
S.is_a? ::Struct
# => false
[::Struct]: ruby-doc.org/core/Struct.html
Defined Under Namespace
Class Method Summary collapse
Class Method Details
.new(*vector_prop_defs, **hash_prop_defs, &class_body) ⇒ Object
148 149 150 151 152 153 154 155 156 157 |
# File 'lib/nrser/labs/i8/struct.rb', line 148 def self.new *vector_prop_defs, **hash_prop_defs, &class_body check_new_args! vector_prop_defs, hash_prop_defs if !vector_prop_defs.empty? raise NotImplementedError.new "I8::Struct::Vector is TODO, sorry" # I8::Struct::Vector.new *vector_prop_defs, &class_body else I8::Struct::Hash.new **hash_prop_defs, &class_body end end |