Module: Elastic::Support::Command

Defined in:
lib/elastic/support/command.rb

Class Method Summary collapse

Class Method Details

.new(*_attributes) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/elastic/support/command.rb', line 3

def self.new(*_attributes)
  attr_names = []
  attr_defaults = {}

  _attributes.each do |att|
    if att.is_a? Hash
      attr_defaults.merge! att
      attr_names += att.keys
    else
      attr_names << att
    end
  end

  Struct.new(*attr_names) do
    def self.for(kwargs = {})
      new(kwargs).perform
    end

    def perform
    end

    define_method(:initialize) do |kwargs = {}|
      kwargs = attr_defaults.merge kwargs
      attr_values = attr_names.map { |a| kwargs[a] }
      super(*attr_values)
    end

    define_method(:perform) {}
  end
end