Class: Plumb::InterfaceClass

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Composable

#>>, #as_node, #build, #check, #children, #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(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_namesObject (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) ⇒ 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



31
32
33
34
35
36
37
# File 'lib/plumb/interface_class.rb', line 31

def call(result)
  obj = result.value
  missing_methods = @method_names.reject { |m| obj.respond_to?(m) }
  return result.invalid(errors: "missing methods: #{missing_methods.join(', ')}") if missing_methods.any?

  result
end

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



20
21
22
23
24
25
26
27
# File 'lib/plumb/interface_class.rb', line 20

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