Module: Vissen::Output::Buffer

Extended by:
Forwardable
Included in:
PixelBuffer, VixelBuffer
Defined in:
lib/vissen/output/buffer.rb

Overview

Buffer

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#contextContext (readonly)

Returns the context of the buffer.

Returns:

  • (Context)

    the context of the buffer.



14
15
16
# File 'lib/vissen/output/buffer.rb', line 14

def context
  @context
end

#elementsObject (readonly)

Returns the elements at the buffer points.

Returns:

  • (Object)

    the elements at the buffer points.



17
18
19
# File 'lib/vissen/output/buffer.rb', line 17

def elements
  @elements
end

Instance Method Details

#[](*args) ⇒ Object

Context specific element accessor. Depends on ‘Context#index_from` to transform `args` into an index.

Parameters:

  • args (see Context#index_from)

    .

Returns:

  • (Object)

    the element at the given index.



56
57
58
# File 'lib/vissen/output/buffer.rb', line 56

def [](*args)
  @elements[@context.index_from(*args)]
end

#each_with_positionsee Context#each_position

Iterates over each element in the buffer and yields the element along with its x and y coordinates.

Returns:

  • (see Context#each_position)

    .



64
65
66
67
# File 'lib/vissen/output/buffer.rb', line 64

def each_with_position
  return to_enum(__callee__) unless block_given?
  @context.each_position { |i, x, y| yield @elements[i], x, y }
end

#freezeself

Prevents the context and element array from being changed.

Returns:

  • (self)


46
47
48
49
# File 'lib/vissen/output/buffer.rb', line 46

def freeze
  @elements.freeze
  super
end

#initialize(context, elements_klass = nil, &block) ⇒ Object

The grid is setup with a grid context as well as a class to places instances of in every grid point.

Parameters:

  • context (Context)

    the context in which the buffer exists.

  • elements_klass (Class) (defaults to: nil)

    the class to use when allocating elements.

  • block (Proc)

    the block to use instead of ‘elements_klass` when allocating element objects.

Raises:

  • (ArgumentError)

    if both an element class and a block are given.



32
33
34
35
36
37
38
39
40
41
# File 'lib/vissen/output/buffer.rb', line 32

def initialize(context, elements_klass = nil, &block)
  @context  = context
  @elements =
    if block_given?
      raise ArgumentError if elements_klass
      context.alloc_points(&block).freeze
    else
      context.alloc_points(elements_klass).freeze
    end
end

#share_context?(other) ⇒ true, false Also known as: ===

Two buffers are considered equal if they share the same context.

Parameters:

Returns:

  • (true, false)

    true if the other object share the same context.



73
74
75
76
77
# File 'lib/vissen/output/buffer.rb', line 73

def share_context?(other)
  @context == other.context
rescue NoMethodError
  false
end