Class: Eco::API::UseCases

Inherits:
Object show all
Extended by:
Common::ClassAutoLoader
Includes:
Enumerable
Defined in:
lib/eco/api/usecases.rb,
lib/eco/api/usecases/cli.rb,
lib/eco/api/usecases/base_io.rb,
lib/eco/api/usecases/cli/dsl.rb,
lib/eco/api/usecases/default.rb,
lib/eco/api/usecases/graphql.rb,
lib/eco/api/usecases/use_case.rb,
lib/eco/api/usecases/base_case.rb,
lib/eco/api/usecases/ooze_cases.rb,
lib/eco/api/usecases/use_case_io.rb,
lib/eco/api/usecases/default/meta.rb,
lib/eco/api/usecases/ooze_samples.rb,
lib/eco/api/usecases/default/utils.rb,
lib/eco/api/usecases/default_cases.rb,
lib/eco/api/usecases/default/people.rb,
lib/eco/api/usecases/use_case_chain.rb,
lib/eco/api/usecases/default/locations.rb,
lib/eco/api/usecases/ooze_samples/helpers.rb,
lib/eco/api/usecases/default_cases/samples.rb,
lib/eco/api/usecases/ooze_samples/helpers/filters.rb,
lib/eco/api/usecases/ooze_samples/helpers/creatable.rb,
lib/eco/api/usecases/ooze_samples/helpers/rescuable.rb,
lib/eco/api/usecases/ooze_samples/helpers/shortcuts.rb,
lib/eco/api/usecases/ooze_samples/helpers_migration.rb,
lib/eco/api/usecases/ooze_samples/helpers/ooze_handlers.rb,
lib/eco/api/usecases/ooze_samples/helpers/exportable_ooze.rb,
lib/eco/api/usecases/ooze_samples/helpers/exportable_register.rb

Overview

Autoloading children is done on init and each time usecases it's queried

Direct Known Subclasses

Error::Handlers, Policies, Default, DefaultCases

Defined Under Namespace

Modules: GraphQL Classes: AmbiguousCaseReference, BaseCase, BaseIO, Cli, Default, DefaultCases, OozeCases, OozeSamples, UnknownCase, UseCase, UseCaseChain, UseCaseIO

Instance Method Summary collapse

Methods included from Common::ClassAutoLoader

_autoload_namespace, autoload_children, autoload_class?, autoload_namespace, autoload_namespace_ignore, autoloaded_children, autoloaded_class, autoloaded_namespaces, autoloads_children_of, known_class!, known_classes, new_classes, unloaded_children

Methods included from Common::ClassHelpers

#class_resolver, #descendants, #descendants?, #inheritable_attrs, #inheritable_class_vars, #inherited, #instance_variable_name, #new_class, #resolve_class, #to_constant

Constructor Details

#initializeUseCases

Returns a new instance of UseCases.



28
29
30
31
32
33
# File 'lib/eco/api/usecases.rb', line 28

def initialize
  @usecases      = {}
  @cache_init    = false
  @cases_by_name = {}
  init_caches
end

Instance Method Details

#add(usecase) ⇒ Object

Adds usecase to the Collection. Before it tries to auto-load pending classes of auto_loaded_class.

Parameters:

Raises:

  • (ArgumentError)


70
71
72
73
74
75
76
77
78
79
80
# File 'lib/eco/api/usecases.rb', line 70

def add(usecase)
  msg = "Expected Eco::API::UseCases::UseCase object. Given: #{usecase.class}"
  raise ArgumentError, msg if !usecase || !usecase.is_a?(Eco::API::UseCases::UseCase)

  name = usecase.name
  type = usecase.type
  puts "Warning: overriding '#{type}' case #{name}" if self.defined?(name, type: type)
  to_h[key(name, type)] = usecase
  @cache_init = false
  usecase
end

#case(name, type: nil) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/eco/api/usecases.rb', line 115

def case(name, type: nil)
  if type && (target_case = to_h[key(name, type)])
    return target_case
  elsif type
    raise UseCases::UnknownCase.new(case_name: name, type: type)
  end
  raise UseCases::UnknownCase.new(case_name: name, type: type) unless (cases = by_name[name])
  raise UseCases::AmbiguousCaseReference.new(case_name: name)  if cases.length > 1

  cases.first
end

#define(name, type:, &block) ⇒ Eco::API::UseCases::UseCase

Returns the name usecase of type type with the defined block.

Returns:



62
63
64
65
66
# File 'lib/eco/api/usecases.rb', line 62

def define(name, type:, &block)
  Eco::API::UseCases::UseCase.new(name, type: type, root: self, &block).tap do |usecase|
    add(usecase)
  end
end

#defined?(name, type: nil) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
# File 'lib/eco/api/usecases.rb', line 52

def defined?(name, type: nil)
  return to_h.key?(key(name, type)) if type
  name?(name)
end

#dupEco::API::UseCases

Returns a copy of instance object of self.

Returns:



83
84
85
86
# File 'lib/eco/api/usecases.rb', line 83

def dup
  init_caches
  self.class.new.merge(self)
end

#each(&block) ⇒ Object



43
44
45
46
# File 'lib/eco/api/usecases.rb', line 43

def each(&block)
  return to_enum(:each) unless block
  items.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/eco/api/usecases.rb', line 39

def empty?
  count.zero?
end

#itemsObject



48
49
50
# File 'lib/eco/api/usecases.rb', line 48

def items
  to_h.values
end

#lengthObject



35
36
37
# File 'lib/eco/api/usecases.rb', line 35

def length
  count
end

#merge(cases) ⇒ Eco::API::UseCases

Merges cases overriding self for exisint parsers

Parameters:

Returns:



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/eco/api/usecases.rb', line 91

def merge(cases)
  return self unless cases

  msg = "Expected a Eco::API::UseCases object. Given #{cases.class}"
  raise msg unless cases.is_a?(Eco::API::UseCases)

  to_h.merge!(cases.to_h).transform_values do |usecase|
    usecase.root = self
  end

  @cache_init = false
  self
end

#name?(name) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/eco/api/usecases.rb', line 57

def name?(name)
  !!by_name[name]
end

#namesObject



105
106
107
# File 'lib/eco/api/usecases.rb', line 105

def names
  by_name.keys
end

#types(name) ⇒ Object



109
110
111
112
113
# File 'lib/eco/api/usecases.rb', line 109

def types(name)
  return nil unless name?(name)

  by_name[name].map(&:type)
end