Class: EacRubyUtils::CommonConstructor::SuperArgs

Inherits:
Object
  • Object
show all
Defined in:
lib/eac_ruby_utils/common_constructor/super_args.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(class_initialize, args, object) ⇒ SuperArgs

Returns a new instance of SuperArgs.


11
12
13
14
15
# File 'lib/eac_ruby_utils/common_constructor/super_args.rb', line 11

def initialize(class_initialize, args, object)
  @class_initialize = class_initialize
  @args = args
  @object = object
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args


8
9
10
# File 'lib/eac_ruby_utils/common_constructor/super_args.rb', line 8

def args
  @args
end

#class_initializeObject (readonly)

Returns the value of attribute class_initialize


8
9
10
# File 'lib/eac_ruby_utils/common_constructor/super_args.rb', line 8

def class_initialize
  @class_initialize
end

#objectObject (readonly)

Returns the value of attribute object


8
9
10
# File 'lib/eac_ruby_utils/common_constructor/super_args.rb', line 8

def object
  @object
end

Instance Method Details

#auto_resultObject


17
18
19
20
21
22
23
24
# File 'lib/eac_ruby_utils/common_constructor/super_args.rb', line 17

def auto_result
  r = []
  sub_args.each do |name, value|
    i = super_arg_index(name)
    r[i] = value if i
  end
  r
end

#resultObject


26
27
28
# File 'lib/eac_ruby_utils/common_constructor/super_args.rb', line 26

def result
  result_from_options || auto_result
end

#result_from_optionsObject


30
31
32
33
34
# File 'lib/eac_ruby_utils/common_constructor/super_args.rb', line 30

def result_from_options
  return unless common_constructor.super_args

  object.instance_exec(&common_constructor.super_args)
end

#sub_argsObject


36
37
38
39
40
# File 'lib/eac_ruby_utils/common_constructor/super_args.rb', line 36

def sub_args
  common_constructor.args.each_with_index.map do |name, index|
    [name, args[index]]
  end.to_h
end

#super_arg_index(name) ⇒ Object


42
43
44
45
46
47
# File 'lib/eac_ruby_utils/common_constructor/super_args.rb', line 42

def super_arg_index(name)
  super_method.parameters.each_with_index do |arg, index|
    return index if arg[1] == name
  end
  nil
end

#super_methodObject


49
50
51
# File 'lib/eac_ruby_utils/common_constructor/super_args.rb', line 49

def super_method
  class_initialize.klass.superclass&.instance_method(:initialize)
end