Class: Ruby::Rego::ObjectValue

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

Overview

Represents an object value.

Constant Summary collapse

TYPE_NAME =
"object"

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(pairs) ⇒ ObjectValue

Create an object value.

Parameters:

  • pairs (Hash<Object, Object>)

    object pairs



337
338
339
340
# File 'lib/ruby/rego/value.rb', line 337

def initialize(pairs)
  @values = normalize_pairs(pairs)
  super(@values)
end

Instance Method Details

#fetch(key) ⇒ Value

Fetch a value by key.

Parameters:

  • key (Object)

    object key

Returns:

  • (Value)

    value or undefined



394
395
396
397
398
399
400
# File 'lib/ruby/rego/value.rb', line 394

def fetch(key)
  return @values[key] if @values.key?(key)
  return fetch_numeric(key) if key.is_a?(Numeric)
  return fetch_by_symbol_key(key) if key.is_a?(Symbol)

  UndefinedValue.new
end

#fetch_reference(key) ⇒ Value

Resolve a reference for an object.

Parameters:

  • key (Object)

    object key

Returns:

  • (Value)

    value or undefined



413
414
415
# File 'lib/ruby/rego/value.rb', line 413

def fetch_reference(key)
  fetch(key)
end

#to_rubyHash<Object, Object>

Convert the object back to Ruby.

Returns:

  • (Hash<Object, Object>)


425
426
427
# File 'lib/ruby/rego/value.rb', line 425

def to_ruby
  @values.transform_values(&:to_ruby)
end