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: "")


46
47
48
49
50
51
# File 'lib/convenient_service/support/dependency_container/entities/method.rb', line 46

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.



37
38
39
# File 'lib/convenient_service/support/dependency_container/entities/method.rb', line 37

def alias_slug
  @alias_slug
end

#bodyObject (readonly)

Returns the value of attribute body.



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

def body
  @body
end

#scopeObject (readonly)

Returns the value of attribute scope.



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

def scope
  @scope
end

#slugObject (readonly)

Returns the value of attribute slug.



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

def slug
  @slug
end

Instance Method Details

#==(other) ⇒ Boolean?

Parameters:

  • other (Object)

    Can be any type.

Returns:

  • (Boolean, nil)


107
108
109
110
111
112
113
114
115
116
# File 'lib/convenient_service/support/dependency_container/entities/method.rb', line 107

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



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
97
98
99
100
101
# File 'lib/convenient_service/support/dependency_container/entities/method.rb', line 71

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)


56
57
58
# File 'lib/convenient_service/support/dependency_container/entities/method.rb', line 56

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

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



63
64
65
# File 'lib/convenient_service/support/dependency_container/entities/method.rb', line 63

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



128
129
130
131
132
133
134
135
# File 'lib/convenient_service/support/dependency_container/entities/method.rb', line 128

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})


121
122
123
# File 'lib/convenient_service/support/dependency_container/entities/method.rb', line 121

def to_kwargs
  to_arguments.kwargs
end