Class: Interface::PortEntity

Inherits:
Object
  • Object
show all
Defined in:
lib/interface/port_entity.rb

Constant Summary collapse

N_A =
'N/A'.freeze
LIM =
('-' * 48).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, adapter) ⇒ PortEntity

Returns a new instance of PortEntity.



8
9
10
# File 'lib/interface/port_entity.rb', line 8

def initialize(name, adapter)
  @name, @adapter = name, adapter
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/interface/port_entity.rb', line 6

def name
  @name
end

Instance Method Details

#after_callObject



57
# File 'lib/interface/port_entity.rb', line 57

def after_call; end

#before_callObject



56
# File 'lib/interface/port_entity.rb', line 56

def before_call; end

#call(*args, &block) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/interface/port_entity.rb', line 29

def call(*args, &block)
  if @handler.nil?
    fail(::Interface::Errors::HandlerMissingError.new("Handler is undefined"))
  end

  if !@contract.nil?
    fail(::Interface::Errors::InvalidInputError.new("Empty argument list doesn not comply with the Contract")) if args.empty?

    errors = @contract.call(*args).errors
    fail(::Interface::Errors::InvalidInputError.new(errors)) if errors.any?
  end

  _callee.call(*args, &block)
end

#contract(&validation_schema) ⇒ Object



48
49
50
# File 'lib/interface/port_entity.rb', line 48

def contract(&validation_schema)
  @contract = validation_schema
end

#describe(text) ⇒ Object



12
13
14
# File 'lib/interface/port_entity.rb', line 12

def describe(text)
  @description = text
end

#docObject



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/interface/port_entity.rb', line 60

def doc
  puts <<-DOC
#{LIM}
Name:\t#{ name }
Desc:\t#{        @description    || N_A}
Responsible:\t#{ @handler || N_A}
Accepts:\t#{     @arguments      || N_A}
Returns:
\tsuccess:\t#{   @returns && @returns.fetch(:success, N_A) || N_A }
\tfailure:\t#{   @returns && @returns.fetch(:failure, N_A) || N_A }
#{LIM}
  DOC
end

#handler(klass) ⇒ Object



44
45
46
# File 'lib/interface/port_entity.rb', line 44

def handler(klass)
  @handler = klass
end

#proxy(obj, options = {}) ⇒ Object



24
25
26
27
# File 'lib/interface/port_entity.rb', line 24

def proxy(obj, options={})
  return nil unless @proxy_factory
  @proxy_factory.call(struct: @struct, contract: @contract, handler: @handler).new(obj, options)
end

#proxy_factory(klass) ⇒ Object



20
21
22
# File 'lib/interface/port_entity.rb', line 20

def proxy_factory(klass)
  @proxy_factory = klass
end

#returns(klass) ⇒ Object



52
53
54
# File 'lib/interface/port_entity.rb', line 52

def returns(klass)
  @adapter = klass
end

#struct(*fields) ⇒ Object



16
17
18
# File 'lib/interface/port_entity.rb', line 16

def struct(*fields)
  @struct = fields
end

#wrap_callObject



58
# File 'lib/interface/port_entity.rb', line 58

def wrap_call; end