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.



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

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



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

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

#resultObject



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

def result
  result_from_options || auto_result
end

#result_from_optionsObject



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

def result_from_options
  return unless common_constructor.super_args

  object.instance_exec(&common_constructor.super_args)
end

#sub_argsObject



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

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

#super_arg_index(name) ⇒ Object



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

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



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

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