Class: Ruby::Rego::ArrayValue

Inherits:
Value
  • Object
show all
Defined in:
lib/ruby/rego/value.rb

Overview

Represents an array value.

Constant Summary collapse

TYPE_NAME =
"array"

Instance Attribute Summary

Attributes inherited from Value

#value

Instance Method Summary collapse

Methods inherited from Value

#==, #canonical, canonicalize, canonicalize_each, canonicalize_float, from_ruby, #hash, #object_key, #truthy?, #type_name, #undefined?

Constructor Details

#initialize(elements) ⇒ ArrayValue

Create an array value.

Parameters:

  • elements (Array<Object>)

    elements to wrap



295
296
297
298
# File 'lib/ruby/rego/value.rb', line 295

def initialize(elements)
  @elements = elements.map { |element| Value.from_ruby(element) }
  super(@elements)
end

Instance Method Details

#fetch_index(index) ⇒ Value

Fetch an element by index.

Parameters:

  • index (Integer)

    array index

Returns:

  • (Value)

    element or undefined



304
305
306
307
308
# File 'lib/ruby/rego/value.rb', line 304

def fetch_index(index)
  return UndefinedValue.new unless index.is_a?(Integer)

  @elements[index] || UndefinedValue.new
end

#fetch_reference(key) ⇒ Value

Resolve a reference for an array.

Parameters:

  • key (Object)

    index value

Returns:

  • (Value)

    element or undefined



314
315
316
# File 'lib/ruby/rego/value.rb', line 314

def fetch_reference(key)
  fetch_index(key)
end

#to_rubyArray<Object>

Convert the array back to Ruby.

Returns:

  • (Array<Object>)


321
322
323
# File 'lib/ruby/rego/value.rb', line 321

def to_ruby
  @elements.map(&:to_ruby)
end