Class: Plumb::HashMap::FilteredHashMap

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Composable

#>>, #[], #as_node, #build, #check, #defer, #generate, included, #invalid, #invoke, #match, #metadata, #not, #pipeline, #policy, #static, #to_json_schema, #to_s, #transform, #value, wrap, #|

Methods included from Callable

#parse, #resolve

Constructor Details

#initialize(key_type, value_type) ⇒ FilteredHashMap

Returns a new instance of FilteredHashMap.



49
50
51
52
53
54
# File 'lib/plumb/hash_map.rb', line 49

def initialize(key_type, value_type)
  @key_type = key_type
  @value_type = value_type
  @children = [key_type, value_type].freeze
  freeze
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



47
48
49
# File 'lib/plumb/hash_map.rb', line 47

def children
  @children
end

Instance Method Details

#call(result) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/plumb/hash_map.rb', line 56

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

  hash = result.value.each.with_object({}) do |(key, value), memo|
    key_r = @key_type.resolve(key)
    value_r = @value_type.resolve(value)
    memo[key_r.value] = value_r.value if key_r.valid? && value_r.valid?
  end

  result.valid(hash)
end