Class: Interactify::Wiring

Inherits:
Object
  • Object
show all
Defined in:
lib/interactify/wiring.rb,
lib/interactify/wiring/files.rb,
lib/interactify/wiring/constants.rb,
lib/interactify/wiring/error_context.rb,
lib/interactify/wiring/callable_representation.rb

Defined Under Namespace

Classes: CallableRepresentation, Constants, ErrorContext, Files

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root:, ignore: []) ⇒ Wiring

Returns a new instance of Wiring.



13
14
15
16
# File 'lib/interactify/wiring.rb', line 13

def initialize(root:, ignore: [])
  @root = root.to_s.gsub(%r{/$}, "")
  @ignore = ignore
end

Instance Attribute Details

#ignoreObject (readonly)

Returns the value of attribute ignore.



11
12
13
# File 'lib/interactify/wiring.rb', line 11

def ignore
  @ignore
end

#rootObject (readonly)

Returns the value of attribute root.



11
12
13
# File 'lib/interactify/wiring.rb', line 11

def root
  @root
end

Instance Method Details

#constantsObject



76
77
78
79
80
81
82
# File 'lib/interactify/wiring.rb', line 76

def constants
  @constants ||= Constants.new(
    root:,
    organizer_files:,
    interactor_files:
  )
end

#each_error(all_errors) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/interactify/wiring.rb', line 39

def each_error(all_errors)
  all_errors.each do |organizer, error_context|
    next if ignore_klass?(ignore, organizer.klass)

    error_context.missing_keys.each do |interactor, missing|
      next if ignore_klass?(ignore, interactor.klass)

      yield missing, interactor, organizer
    end
  end
end

#filesObject



86
87
88
# File 'lib/interactify/wiring.rb', line 86

def files
  @files ||= Files.new(root:)
end

#format_error(missing, interactor, organizer, formatted_errors) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/interactify/wiring.rb', line 51

def format_error(missing, interactor, organizer, formatted_errors)
  formatted_errors << <<~ERROR
    Missing keys: #{missing.to_a.map(&:to_sym).map(&:inspect).join(', ')}
     expected in: #{interactor.klass}
       called by: #{organizer.klass}
  ERROR
end

#format_errors(all_errors) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/interactify/wiring.rb', line 29

def format_errors(all_errors)
  formatted_errors = []

  each_error(all_errors) do |missing, interactor, organizer|
    format_error(missing, interactor, organizer, formatted_errors)
  end

  formatted_errors.join("\n\n")
end

#ignore_klass?(ignore, klass) ⇒ Boolean

Returns:

  • (Boolean)


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

def ignore_klass?(ignore, klass)
  case ignore
  when Array
    ignore.any? { ignore_klass?(_1, klass) }
  when Regexp
    klass.to_s =~ ignore
  when String
    klass.to_s[ignore]
  when Proc
    ignore.call(klass)
  when Class
    klass <= ignore
  end
end

#validate_appObject



18
19
20
21
22
23
24
25
26
27
# File 'lib/interactify/wiring.rb', line 18

def validate_app
  errors = organizers.each_with_object({}) do |organizer, all_errors|
    next if ignore_klass?(ignore, organizer.klass)

    errors = organizer.validate_callable
    all_errors[organizer] = errors
  end

  format_errors(errors)
end