Class: I8::Struct::Vector

Inherits:
Vector show all
Includes:
I8::Struct, NRSER::Props::Immutable::Vector
Defined in:
lib/nrser/labs/i8.rb

Constant Summary

Constants included from NRSER::Props::Immutable::Vector

NRSER::Props::Immutable::Vector::STORAGE

Class Method Summary collapse

Methods included from NRSER::Props::Immutable::Vector

included, #initialize

Methods inherited from Hamster::Vector

#as_json, #to_mutable, #to_yaml

Methods included from NRSER::Ext::Tree

#each_branch, #leaves, #map_branches, #map_leaves, #map_tree

Class Method Details

.define(*prop_defs, &body) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/nrser/labs/i8.rb', line 140

def self.define *prop_defs, &body
  # Unwrap `[name: type]` format
  prop_defs.map! { |prop_def|
    if  ::Array === prop_def &&
        prop_def.length == 1 &&
        ::Hash === prop_def[0]
      prop_def[0]
    else
      prop_def
    end
  }
  
  # Check we have a list of pairs with label keys
  t.array( t.pair( key: t.label ) ).check! prop_defs
  
  Class.new( I8::Struct::Vector ) do
    prop_defs.each_with_index do |pair, index|
      name, settings = pair.first
      
      kwds = t.match settings,
        t.type, ->( type ) {{ type: type }},
        t.hash_, settings
      
      prop name, **kwds, index: index
    end
    
    class_exec &body if body
  end
end

.new(*args, **kwds, &block) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/nrser/labs/i8.rb', line 171

def self.new *args, **kwds, &block
  if self == I8::Struct::Vector
    unless kwds.empty?
      raise NRSER::ArgumentError.new \
        "Can not supply keyword args",
        args: args,
        kwds: kwds
    end

    define *args, &block
  else
    # NOTE  This is... weird. Just doing the normal
    #       
    #           super( *args, **kwds, &block )
    #       
    #       results in `*args` becoming `[*args, {}]` up the super chain
    #       when `kwds` is empty.
    #       
    #       I can't say I can understand it, but I seem to be able to fix
    #       it.
    # 
    if kwds.empty?
      super( *args, &block )
    else
      super( *args, **kwds, &block )
    end
  end
end