Class: DataBindings::Bound::BoundArray

Inherits:
Array
  • Object
show all
Includes:
DataBindings::Bound
Defined in:
lib/data_bindings/bound.rb

Constant Summary

Constants included from DataBindings::Bound

NoBindingName, ValidationError

Instance Attribute Summary

Attributes included from DataBindings::Bound

#errors, #generator, #name, #source

Instance Method Summary collapse

Methods included from DataBindings::Bound

#calculate_validness, #cast_element, #pre_convert, #valid!, #valid?

Constructor Details

#initialize(generator, array_expected, source, name, opts, &blk) ⇒ BoundArray

Returns a new instance of BoundArray.

Raises:



261
262
263
264
# File 'lib/data_bindings/bound.rb', line 261

def initialize(generator, array_expected, source, name, opts, &blk)
  raise BindingMismatch unless array_expected
  init_bound(generator, source, name, opts, blk)
end

Instance Method Details

#all_elements(type = nil, &blk) ⇒ Object



310
311
312
# File 'lib/data_bindings/bound.rb', line 310

def all_elements(type = nil, &blk)
  elements(nil, type, &blk)
end

#copy_sourceObject



302
303
304
# File 'lib/data_bindings/bound.rb', line 302

def copy_source
  replace @source
end

#elements(size = nil, type = nil, opts = nil, &blk) ⇒ Object



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/data_bindings/bound.rb', line 277

def elements(size = nil, type = nil, opts = nil, &blk)
  if size.nil? || size.respond_to?(:to_int)
    size ||= source.size
    # consume all
    (@pos...(@pos+size)).each do |i|
      self[@pos] = cast_element(@pos, @from, type, opts, &blk)
      @pos += 1
    end
  elsif size.respond_to?(:min) && size.respond_to?(:max)
    original_pos = @pos
    while (@pos - original_pos) <= size.max
      begin
        opts ||= {}
        opts[:raise_on_error] = @pos >= size.min
        self[@pos] = cast_element(@pos, @from, type, opts, &blk)
      rescue ValidationError
        break
      end
      @pos += 1
    end
  else
    raise "Size isn't understood: #{size.inspect}"
  end
end

#enforce_strictnessObject



306
307
308
# File 'lib/data_bindings/bound.rb', line 306

def enforce_strictness
  @errors.base = "hasn't been fully matched" unless @pos.succ == source.size
end

#to_aryObject



266
267
268
269
270
# File 'lib/data_bindings/bound.rb', line 266

def to_ary
  self.inject([]) { |a, v|
    a << dump_val(v)
  }
end

#to_nativeObject



272
273
274
275
# File 'lib/data_bindings/bound.rb', line 272

def to_native
  valid!
  inject([]) {|a, el| a << (el.respond_to?(:to_native) ? el.to_native : el); a}
end