Skip to content

Commit

Permalink
Move wrapping array values to Composable.wrap
Browse files Browse the repository at this point in the history
  • Loading branch information
ismasan committed Sep 1, 2024
1 parent 388873c commit 7f2ffc2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 22 deletions.
11 changes: 11 additions & 0 deletions lib/plumb/composable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def self.included(base)
# Wrap an object in a Composable instance.
# Anything that includes Composable is a noop.
# A Hash is assumed to be a HashClass schema.
# An Array with zero or 1 element is assumed to be an ArrayClass.
# Any `#call(Result) => Result` interface is wrapped in a Step.
# Anything else is assumed to be something you want to match against via `#===`.
#
Expand All @@ -115,6 +116,16 @@ def self.wrap(callable)
callable
elsif callable.is_a?(::Hash)
HashClass.new(schema: callable)
elsif callable.is_a?(::Array)
element_type = case callable.size
when 0
Types::Any
when 1
callable.first
else
raise ArgumentError, '[element_type] syntax allows a single element type'
end
Types::Array[element_type]
elsif callable.respond_to?(:call)
Step.new(callable)
else
Expand Down
23 changes: 1 addition & 22 deletions lib/plumb/hash_class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,28 +140,7 @@ def _inspect

def wrap_keys_and_values(hash)
hash.each.with_object({}) do |(k, v), ret|
ret[Key.wrap(k)] = wrap_value(v)
end
end

def wrap_value(value)
case value
when ::Array
element_type = case value.size
when 0
Types::Any
when 1
value.first
else
raise ArgumentError, '[element_type] syntax allows a single element type'
end
Types::Array[element_type]
when ::Hash
schema wrap_keys_and_values(value)
when Callable
value
else #  leaf values
Composable.wrap(value)
ret[Key.wrap(k)] = Composable.wrap(v)
end
end

Expand Down

0 comments on commit 7f2ffc2

Please sign in to comment.