Class: Ruby::Rego::SetValue

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

Overview

Represents a set value.

Constant Summary collapse

TYPE_NAME =
"set"

Instance Attribute Summary

Attributes inherited from Value

#value

Instance Method Summary collapse

Methods inherited from Value

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

Constructor Details

#initialize(elements) ⇒ SetValue

Create a set value.

Parameters:

  • elements (Set<Object>, Array<Object>)

    set elements



443
444
445
446
447
# File 'lib/ruby/rego/value.rb', line 443

def initialize(elements)
  collection = elements.is_a?(Set) ? elements.to_a : Array(elements)
  @elements = Set.new(collection.map { |element| Value.from_ruby(element) })
  super(@elements)
end

Instance Method Details

#include?(value) ⇒ Boolean

Check whether the set includes a value.

Parameters:

  • value (Object)

    value to check

Returns:

  • (Boolean)


453
454
455
# File 'lib/ruby/rego/value.rb', line 453

def include?(value)
  @elements.include?(Value.from_ruby(value))
end

#to_rubySet<Object>

Convert the set back to Ruby.

Returns:

  • (Set<Object>)


460
461
462
# File 'lib/ruby/rego/value.rb', line 460

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