Class: ConvenientService::Support::DependencyContainer::Entities::Method

Inherits:
Object
  • Object
show all
Includes:
Copyable
Defined in:
lib/convenient_service/support/dependency_container/entities/method.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Copyable

#copy

Constructor Details

#initialize(slug:, scope:, body:, alias_slug: "") ⇒ void

Parameters:

  • slug (String, Symbol)
  • scope (:instance, :class)
  • body (Proc)
  • alias_slug (String, Symbol) (defaults to: "")


41
42
43
44
45
46
# File 'lib/convenient_service/support/dependency_container/entities/method.rb', line 41

def initialize(slug:, scope:, body:, alias_slug: "")
  @slug = slug
  @scope = scope
  @body = body
  @alias_slug = alias_slug
end

Instance Attribute Details

#alias_slugObject (readonly)

Returns the value of attribute alias_slug.



32
33
34
# File 'lib/convenient_service/support/dependency_container/entities/method.rb', line 32

def alias_slug
  @alias_slug
end

#bodyObject (readonly)

Returns the value of attribute body.



26
27
28
# File 'lib/convenient_service/support/dependency_container/entities/method.rb', line 26

def body
  @body
end

#scopeObject (readonly)

Returns the value of attribute scope.



20
21
22
# File 'lib/convenient_service/support/dependency_container/entities/method.rb', line 20

def scope
  @scope
end

#slugObject (readonly)

Returns the value of attribute slug.



14
15
16
# File 'lib/convenient_service/support/dependency_container/entities/method.rb', line 14

def slug
  @slug
end

Instance Method Details

#==(other) ⇒ Boolean?

Parameters:

  • other (Object)

    Can be any type.

Returns:

  • (Boolean, nil)


102
103
104
105
106
107
108
109
110
111
# File 'lib/convenient_service/support/dependency_container/entities/method.rb', line 102

def ==(other)
  return unless other.instance_of?(self.class)

  return false if slug != other.slug
  return false if scope != other.scope
  return false if body != other.body
  return false if alias_slug != other.alias_slug

  true
end

#define_in_module!(mod) ⇒ ConvenientService::Support::DependencyContainer::Entities::Method



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/convenient_service/support/dependency_container/entities/method.rb', line 66

def define_in_module!(mod)
  ##
  # NOTE: `innermost_namespace` is just `mod`, when `namespaces` are empty.
  #
  innermost_namespace =
    namespaces.reduce(mod) do |namespace, sub_namespace|
      already_defined_sub_namespace = namespace.namespaces.find_by(name: sub_namespace.name)

      ##
      # NOTE:
      #   - Reuses already defined namespace from previous "imports".
      #   - In contrast, same methods are always redefined.
      #
      next already_defined_sub_namespace if already_defined_sub_namespace

      namespace.namespaces << sub_namespace

      namespace.define_method(sub_namespace.name) { sub_namespace.body.call }

      sub_namespace
    end

  ##
  # NOTE:
  #   - Same methods are redefined.
  #   - In contrast, same namespaces are always reused.
  #
  innermost_namespace.define_method(name, &body)

  self
end

#nameSymbol

Returns:

  • (Symbol)


51
52
53
# File 'lib/convenient_service/support/dependency_container/entities/method.rb', line 51

def name
  @name ||= alias_slug_parts.last || slug_parts.last
end

#namespacesArray<ConvenientService::Support::DependencyContainer::Entities::Namespace>



58
59
60
# File 'lib/convenient_service/support/dependency_container/entities/method.rb', line 58

def namespaces
  @namespaces ||= (alias_slug_parts.any? ? alias_slug_parts : slug_parts).slice(0..-2).map { |part| Entities::Namespace.new(name: part) }
end

#to_argumentsConvenientService::Support::Arguments



123
124
125
126
127
128
129
130
# File 'lib/convenient_service/support/dependency_container/entities/method.rb', line 123

def to_arguments
  Support::Arguments.new(
    slug: slug,
    scope: scope,
    body: body,
    alias_slug: alias_slug
  )
end

#to_kwargsHash{Symbol => Object}

Returns:

  • (Hash{Symbol => Object})


116
117
118
# File 'lib/convenient_service/support/dependency_container/entities/method.rb', line 116

def to_kwargs
  to_arguments.kwargs
end