Class: Plumb::ArrayClass

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

Direct Known Subclasses

ConcurrentArrayClass

Defined Under Namespace

Classes: ConcurrentArrayClass

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(element_type: Types::Any) ⇒ ArrayClass

Returns a new instance of ArrayClass.



14
15
16
17
18
19
# File 'lib/plumb/array_class.rb', line 14

def initialize(element_type: Types::Any)
  @element_type = Composable.wrap(element_type)
  @children = [@element_type].freeze

  freeze
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



12
13
14
# File 'lib/plumb/array_class.rb', line 12

def children
  @children
end

Instance Method Details

#call(result) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/plumb/array_class.rb', line 45

def call(result)
  return result.invalid(errors: 'is not an Array') unless ::Array === result.value

  values, errors = map_array_elements(result)
  return result.valid(values) unless errors.any?

  result.invalid(values, errors:)
end

#concurrentObject



27
28
29
# File 'lib/plumb/array_class.rb', line 27

def concurrent
  ConcurrentArrayClass.new(element_type:)
end

#filteredObject



35
36
37
38
39
40
41
42
43
# File 'lib/plumb/array_class.rb', line 35

def filtered
  MatchClass.new(::Array) >> Step.new(nil, "Array[#{element_type}].filtered") do |result|
    arr = result.value.each.with_object([]) do |e, memo|
      r = element_type.resolve(e)
      memo << r.value if r.valid?
    end
    result.valid(arr)
  end
end

#of(element_type) ⇒ Object Also known as: []



21
22
23
# File 'lib/plumb/array_class.rb', line 21

def of(element_type)
  self.class.new(element_type:)
end

#streamObject



31
32
33
# File 'lib/plumb/array_class.rb', line 31

def stream
  StreamClass.new(element_type:)
end