Class: Qrb::SetType

Inherits:
Type
  • Object
show all
Includes:
CollectionType
Defined in:
lib/qrb/type/set_type.rb

Overview

The Set type generator allows capturing an unordered set of values (i.e. with no duplicates). For example, a set of emails could be captured with:

Adresses = {Email}

This class allows capturing those set types, e.g.:

Email    = BuiltinType.new(String)
Adresses = SetType.new(Email)

A ruby Set of values is used as concrete representation for such sets:

R(Adresses) = Set[R(Email)] = Set[String]

Accordingly, the ‘dress` transformation function has the signature below. It expects it’s Alpha/Object argument to be a object responding to ‘each` (with the ruby idiomatic semantics that such a `each` returns an Enumerator when invoked without block).

dress :: Alpha  -> Adresses     throws TypeError
dress :: Object -> Set[String]  throws TypeError

Instance Attribute Summary

Attributes included from CollectionType

#elm_type

Instance Method Summary collapse

Methods included from CollectionType

#==, #hash, #initialize

Methods inherited from Type

#initialize, #name, #name=, #to_s

Instance Method Details

#default_nameObject



28
29
30
# File 'lib/qrb/type/set_type.rb', line 28

def default_name
  "{#{elm_type.name}}"
end

#dress(value, handler = DressHelper.new) ⇒ Object

Apply the element type’s ‘dress` transformation to each element of `value` (expected to respond to `each`). Return converted values in a ruby Set.



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/qrb/type/set_type.rb', line 39

def dress(value, handler = DressHelper.new)
  handler.failed!(self, value) unless value.respond_to?(:each)

  set = Set.new
  handler.iterate(value) do |elm, index|
    elm = elm_type.dress(elm, handler)
    handler.fail!("Duplicate value `#{elm}`") if set.include?(elm)
    set << elm
  end
  set
end

#include?(value) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/qrb/type/set_type.rb', line 32

def include?(value)
  value.is_a?(::Set) and value.all?{|v| elm_type.include?(v) }
end