Class: Plumb::HashMap

Inherits:
Object
  • Object
show all
Includes:
Steppable
Defined in:
lib/plumb/hash_map.rb

Defined Under Namespace

Classes: FilteredHashMap

Instance Attribute Summary collapse

Attributes included from Steppable

#name

Instance Method Summary collapse

Methods included from Steppable

#===, #>>, #[], #as_node, #build, #check, #defer, #freeze, included, #inspect, #invalid, #invoke, #match, #meta, #node_name, #not, #pipeline, #policy, #to_s, #transform, #value, wrap, #|

Methods included from Callable

#metadata, #parse, #resolve

Constructor Details

#initialize(key_type, value_type) ⇒ HashMap



11
12
13
14
15
# File 'lib/plumb/hash_map.rb', line 11

def initialize(key_type, value_type)
  @key_type = key_type
  @value_type = value_type
  freeze
end

Instance Attribute Details

#key_typeObject (readonly)

Returns the value of attribute key_type.



9
10
11
# File 'lib/plumb/hash_map.rb', line 9

def key_type
  @key_type
end

#value_typeObject (readonly)

Returns the value of attribute value_type.



9
10
11
# File 'lib/plumb/hash_map.rb', line 9

def value_type
  @value_type
end

Instance Method Details

#call(result) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/plumb/hash_map.rb', line 17

def call(result)
  return result.invalid(errors: 'must be a Hash') unless result.value.is_a?(::Hash)

  failed = result.value.lazy.filter_map do |key, value|
    key_r = @key_type.resolve(key)
    value_r = @value_type.resolve(value)
    if !key_r.valid?
      [:key, key, key_r]
    elsif !value_r.valid?
      [:value, value, value_r]
    end
  end
  if (first = failed.next)
    field, val, halt = failed.first
    result.invalid(errors: "#{field} #{val.inspect} #{halt.errors}")
  end
rescue StopIteration
  result
end

#filteredObject



37
38
39
# File 'lib/plumb/hash_map.rb', line 37

def filtered
  FilteredHashMap.new(key_type, value_type)
end