Class: ConvenientService::Feature::Plugins::CanHaveEntries::Commands::DefineEntry

Inherits:
Support::Command
  • Object
show all
Defined in:
lib/convenient_service/feature/plugins/can_have_entries/commands/define_entry.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Support::Command

[], call

Constructor Details

#initialize(feature_class:, name:, body:) ⇒ DefineEntry

Returns a new instance of DefineEntry.

Parameters:

  • feature_class (Class)
  • name (String, Symbol)
  • body (Proc, nil)


32
33
34
35
36
# File 'lib/convenient_service/feature/plugins/can_have_entries/commands/define_entry.rb', line 32

def initialize(feature_class:, name:, body:)
  @feature_class = feature_class
  @name = name
  @body = body
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



25
26
27
# File 'lib/convenient_service/feature/plugins/can_have_entries/commands/define_entry.rb', line 25

def body
  @body
end

#feature_classObject (readonly)

Returns the value of attribute feature_class.



13
14
15
# File 'lib/convenient_service/feature/plugins/can_have_entries/commands/define_entry.rb', line 13

def feature_class
  @feature_class
end

#nameObject (readonly)

Returns the value of attribute name.



19
20
21
# File 'lib/convenient_service/feature/plugins/can_have_entries/commands/define_entry.rb', line 19

def name
  @name
end

Instance Method Details

#callString, Symbol

Returns:

  • (String, Symbol)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/convenient_service/feature/plugins/can_have_entries/commands/define_entry.rb', line 41

def call
  ##
  # NOTE: Just `feature_class.define_singleton_method` does NOT create a closure for `name`.
  # That is why `feature_class.class_exec` wrapper is required.
  #
  feature_class.class_exec(name) do |name|
    define_singleton_method(name) { |*args, **kwargs, &block| new.entry(name, *args, **kwargs, &block) }
  end

  if body
    feature_class.define_method(name, &body)
  else
    feature_class.define_method(name) { |*args, **kwargs, &block| ::ConvenientService.raise ::ConvenientService::Feature::Plugins::CanHaveEntries::Exceptions::NotDefinedEntryMethod.new(name: __method__, feature: self) }
  end

  ##
  # NOTE: Just `feature_class.instance_proxy_class.define_method` does NOT create a closure for `name`.
  # That is why `feature_class.instance_proxy_class.class_exec` wrapper is required.
  #
  feature_class.instance_proxy_class.class_exec(name) do |name|
    define_method(name) do |*args, **kwargs, &block|
      instance_proxy_target.entry(name, *args, **kwargs, &block)
    end
  end

  feature_class.instance_proxy_class.define_method(:entry) do |*args, **kwargs, &block|
    instance_proxy_target.entry(*args, **kwargs, &block)
  end

  name
end