Class: Plumb::InterfaceClass
- Inherits:
-
Object
- Object
- Plumb::InterfaceClass
- Includes:
- Composable
- Defined in:
- lib/plumb/interface_class.rb
Instance Attribute Summary collapse
-
#method_names ⇒ Object
readonly
Returns the value of attribute method_names.
Instance Method Summary collapse
-
#&(other) ⇒ InterfaceClass
Produce a new Interface with the intersection of two interfaces.
-
#+(other) ⇒ InterfaceClass
Merge two interfaces into a new one with the method names of both.
- #==(other) ⇒ Object
- #call(result) ⇒ Object
-
#initialize(method_names = []) ⇒ InterfaceClass
constructor
A new instance of InterfaceClass.
- #of(*args) ⇒ Object (also: #[])
-
#subtype_of?(other) ⇒ Boolean
Interface <= Interface: self is a subtype of a looser interface — one that requires a subset of self's methods (more methods = narrower type).
-
#supertype_of?(other) ⇒ Boolean
An Interface is a supertype of any type whose underlying Ruby class(es) define all of its methods — the duck-typing rule, driven by the interface (the right side of
a <= self).
Methods included from Composable
#/, #>>, #absorb_input, #absorb_output, #accepted_type, #as_node, #build, #check, #children, #defer, #fusable_step?, #fuse_with, #generate, #idempotent?, included, #input_type, #invalid, #invoke, #match, #metadata, #not, #output_type, #pipeline, #policy, resolve_operand, #static, #subtype_identity, #to_json_schema, #to_mermaid, #to_plumb_type, #to_s, #transform, #value, #value_preserving?, #where, #with, wrap, #|
Methods included from Callable
Constructor Details
#initialize(method_names = []) ⇒ InterfaceClass
Returns a new instance of InterfaceClass.
11 12 13 14 |
# File 'lib/plumb/interface_class.rb', line 11 def initialize(method_names = []) @method_names = method_names freeze end |
Instance Attribute Details
#method_names ⇒ Object (readonly)
Returns the value of attribute method_names.
9 10 11 |
# File 'lib/plumb/interface_class.rb', line 9 def method_names @method_names end |
Instance Method Details
#&(other) ⇒ InterfaceClass
Produce a new Interface with the intersection of two interfaces
72 73 74 75 76 |
# File 'lib/plumb/interface_class.rb', line 72 def &(other) return super unless other.is_a?(self.class) self.class.new(method_names & other.method_names) end |
#+(other) ⇒ InterfaceClass
Merge two interfaces into a new one with the method names of both
58 59 60 61 62 |
# File 'lib/plumb/interface_class.rb', line 58 def +(other) raise ArgumentError, "expected another Types::Interface, but got #{other.inspect}" unless other.is_a?(self.class) self.class.new((method_names + other.method_names).uniq) end |
#==(other) ⇒ Object
16 17 18 |
# File 'lib/plumb/interface_class.rb', line 16 def ==(other) other.is_a?(self.class) && other.method_names == method_names end |
#call(result) ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/plumb/interface_class.rb', line 78 def call(result) obj = result.value missing_methods = @method_names.reject { |m| obj.respond_to?(m) } return result.invalid!(errors: "Invalid #{self.name}. Missing methods: #{missing_methods.join(', ')}") if missing_methods.any? result end |
#of(*args) ⇒ Object Also known as: []
39 40 41 42 43 44 45 46 |
# File 'lib/plumb/interface_class.rb', line 39 def of(*args) case args in Array => symbols if symbols.all? { |s| s.is_a?(::Symbol) } self.class.new(symbols) else raise ::ArgumentError, "unexpected value to Types::Interface#of #{args.inspect}" end end |
#subtype_of?(other) ⇒ Boolean
Interface <= Interface: self is a subtype of a looser interface — one that requires a subset of self's methods (more methods = narrower type).
22 23 24 25 26 |
# File 'lib/plumb/interface_class.rb', line 22 def subtype_of?(other) return (other.method_names - method_names).empty? if other.is_a?(self.class) super end |
#supertype_of?(other) ⇒ Boolean
An Interface is a supertype of any type whose underlying Ruby class(es)
define all of its methods — the duck-typing rule, driven by the interface
(the right side of a <= self). Consulted by Plumb::Subtyping.subtype? via
the #supertype_of? hook, so eg. Types::String <= Types::Interface[:to_s].
32 33 34 35 36 37 |
# File 'lib/plumb/interface_class.rb', line 32 def supertype_of?(other) classes = Plumb.resolve_base_types(other) classes.any? && classes.all? do |klass| klass.is_a?(::Module) && method_names.all? { |m| klass.method_defined?(m) } end end |