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)


37
38
39
40
41
# File 'lib/convenient_service/feature/plugins/can_have_entries/commands/define_entry.rb', line 37

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.



30
31
32
# File 'lib/convenient_service/feature/plugins/can_have_entries/commands/define_entry.rb', line 30

def body
  @body
end

#feature_classObject (readonly)

Returns the value of attribute feature_class.



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

def feature_class
  @feature_class
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#callString, Symbol

Returns:

  • (String, Symbol)


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
72
73
74
75
76
# File 'lib/convenient_service/feature/plugins/can_have_entries/commands/define_entry.rb', line 46

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| trigger(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.trigger(name, *args, **kwargs, &block)
    end
  end

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

  name
end