Class: Finitio::SetType

Inherits:
Type
  • Object
show all
Includes:
CollectionType
Defined in:
lib/finitio/type/set_type.rb,
lib/finitio/generation/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

Constant Summary

Constants included from Metadata

Metadata::EMPTY_METADATA

Instance Attribute Summary

Attributes included from CollectionType

#elm_type

Instance Method Summary collapse

Methods included from CollectionType

#==, #hash, #initialize, #resolve_proxies, #suppremum, #unconstrained

Methods inherited from Type

#==, #anonymous?, #initialize, #name, #name=, #named?, #resolve_proxies, #suppremum, #to_s, #unconstrained

Methods included from Metadata

#metadata, #metadata=, #metadata?

Instance Method Details

#default_nameObject



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

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.



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/finitio/type/set_type.rb', line 43

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

#generate_data(generator, world = nil) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/finitio/generation/set_type.rb', line 4

def generate_data(generator, world = nil)
  coll = []
  generator.collection_size.times do
    coll << generator.call(elm_type, world)
  end
  coll.uniq
end

#include?(value) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/finitio/type/set_type.rb', line 36

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

#representatorObject



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

def representator
  [elmType].to_set
end