Module: Finitio::HashBasedType
- Included in:
- MultiTupleType, TupleType
- Defined in:
- lib/finitio/type/hash_based_type.rb
Instance Method Summary collapse
-
#dress(value, handler = DressHelper.new) ⇒ Object
Convert ‘value` (supposed to be Hash) to a Tuple, by checking attributes and applying `dress` on them in turn.
- #include?(value) ⇒ Boolean
- #representator ⇒ Object
Instance Method Details
#dress(value, handler = DressHelper.new) ⇒ Object
Convert ‘value` (supposed to be Hash) to a Tuple, by checking attributes and applying `dress` on them in turn. Raise an error if any attribute is missing or unrecognized, as well as if any sub transformation fails.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/finitio/type/hash_based_type.rb', line 23 def dress(value, handler = DressHelper.new) handler.failed!(self, value) unless looks_a_tuple?(value) # Check for extra attributes unless heading.allow_extra? or (extra = extra_attrs(value, true)).empty? handler.fail!("Unrecognized attribute `#{extra.first}`") end # Check for missing attributes unless (missing = missing_attrs(value, true)).empty? handler.fail!("Missing attribute `#{missing.first}`") end # Uped values, i.e. tuple under construction uped = {} # Up each attribute in turn now. Fail on missing ones. heading.each do |attribute| present = true val = attribute.fetch_on(value){ present = false } next unless present handler.deeper(attribute.name) do uped[attribute.name] = attribute.type.dress(val, handler) end end uped end |
#include?(value) ⇒ Boolean
12 13 14 15 16 17 18 |
# File 'lib/finitio/type/hash_based_type.rb', line 12 def include?(value) value.is_a?(Hash) && valid_attrs?(value) && heading.all?{|a| value.has_key?(a.name) ? a.type.include?(value[a.name]) : true } end |
#representator ⇒ Object
4 5 6 7 8 9 10 |
# File 'lib/finitio/type/hash_based_type.rb', line 4 def representator rep = {} heading.each do |attr| rep[attr.name] = rep[attr.type.representator] end rep end |