Skip to content

Commit

Permalink
Fix Types::Data.== and .==
Browse files Browse the repository at this point in the history
  • Loading branch information
ismasan committed Sep 25, 2024
1 parent 4fb11b4 commit 5f202c5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
37 changes: 21 additions & 16 deletions lib/plumb/composable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,26 @@ def inspect = name.to_s
def node_name = self.class.name.split('::').last.to_sym
end

# Override #=== and #== for Composable instances.
# but only when included in classes, not extended.
module Equality
# `#===` equality. So that Plumb steps can be used in case statements and pattern matching.
# @param other [Object]
# @return [Boolean]
def ===(other)
case other
when Composable
other == self
else
resolve(other).valid?
end
end

def ==(other)
other.is_a?(self.class) && other.respond_to?(:children) && other.children == children
end
end

#  Composable mixes in composition methods to classes.
# such as #>>, #|, #not, and others.
# Any Composable class can participate in Plumb compositions.
Expand All @@ -95,6 +115,7 @@ module Composable
# not extending classes with it.
def self.included(base)
base.send(:include, Naming)
base.send(:include, Equality)
end

# Wrap an object in a Composable instance.
Expand Down Expand Up @@ -304,22 +325,6 @@ def policy(*args, &blk)
end
end

# `#===` equality. So that Plumb steps can be used in case statements and pattern matching.
# @param other [Object]
# @return [Boolean]
def ===(other)
case other
when Composable
other == self
else
resolve(other).valid?
end
end

def ==(other)
other.is_a?(self.class) && other.respond_to?(:children) && other.children == children
end

# Visitors expect a #node_name and #children interface.
# @return [Array<Composable>]
def children = BLANK_ARRAY
Expand Down
8 changes: 8 additions & 0 deletions spec/data_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,12 @@ class DifferentClass
expect(instance.thing).to be_a(Types::DifferentClass)
expect(instance.thing).to be_a(Types::DifferentClass::Thing)
end

specify 'Data.===(instance)' do
class_a = Types::Data[name: String]
class_b = Types::Data[name: String]
a = class_a.new(name: 'Joe')
expect(class_a === a).to be true
expect(class_b === a).to be false
end
end

0 comments on commit 5f202c5

Please sign in to comment.