Class: Qrb::SetType
- 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
Instance Method Summary collapse
- #default_name ⇒ Object
-
#dress(value, handler = DressHelper.new) ⇒ Object
Apply the element type’s ‘dress` transformation to each element of `value` (expected to respond to `each`).
- #include?(value) ⇒ Boolean
Methods included from CollectionType
Methods inherited from Type
#initialize, #name, #name=, #to_s
Instance Method Details
#default_name ⇒ Object
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
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 |