Class: EacRubyUtils::CommonConstructor

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

Defined Under Namespace

Classes: ClassInitialize, InstanceInitialize, SuperArgs

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &after_set_block) ⇒ CommonConstructor

Returns a new instance of CommonConstructor.


38
39
40
41
42
43
# File 'lib/eac_ruby_utils/common_constructor.rb', line 38

def initialize(*args, &after_set_block)
  args_processed = self.class.parse_args_options(args)
  @args = args_processed.args
  @options = args_processed.options
  @after_set_block = after_set_block
end

Instance Attribute Details

#after_set_blockObject (readonly)

Returns the value of attribute after_set_block


10
11
12
# File 'lib/eac_ruby_utils/common_constructor.rb', line 10

def after_set_block
  @after_set_block
end

#argsObject (readonly)

Returns the value of attribute args


10
11
12
# File 'lib/eac_ruby_utils/common_constructor.rb', line 10

def args
  @args
end

#optionsObject (readonly)

Returns the value of attribute options


10
11
12
# File 'lib/eac_ruby_utils/common_constructor.rb', line 10

def options
  @options
end

Class Method Details

.parse_args_options(args) ⇒ Object


13
14
15
16
17
18
19
20
21
22
23
# File 'lib/eac_ruby_utils/common_constructor.rb', line 13

def parse_args_options(args)
  result = ::OpenStruct.new(args: [], options: {})
  options_reached = false
  args.each do |arg|
    raise "Options reached and there is more arguments (Arguments: #{args})" if
    options_reached

    options_reached = parse_arg_options_process_arg(result, arg)
  end
  result
end

Instance Method Details

#args_countObject


45
46
47
# File 'lib/eac_ruby_utils/common_constructor.rb', line 45

def args_count
  (args_count_min..args_count_max)
end

#args_count_maxObject


53
54
55
# File 'lib/eac_ruby_utils/common_constructor.rb', line 53

def args_count_max
  args.count
end

#args_count_minObject


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

def args_count_min
  args.count - default_values.count
end

#default_valuesObject


57
58
59
# File 'lib/eac_ruby_utils/common_constructor.rb', line 57

def default_values
  options[:default] || []
end

#setup_class(klass) ⇒ Object


61
62
63
64
65
66
67
# File 'lib/eac_ruby_utils/common_constructor.rb', line 61

def setup_class(klass)
  setup_class_attr_readers(klass)
  setup_class_attr_writers(klass)
  setup_class_initialize(klass)

  klass
end

#setup_class_attr_readers(klass) ⇒ Object


69
70
71
72
# File 'lib/eac_ruby_utils/common_constructor.rb', line 69

def setup_class_attr_readers(klass)
  klass.send(:attr_reader, *args)
  klass.send(:public, *args)
end

#setup_class_attr_writers(klass) ⇒ Object


74
75
76
77
# File 'lib/eac_ruby_utils/common_constructor.rb', line 74

def setup_class_attr_writers(klass)
  klass.send(:attr_writer, *args)
  klass.send(:private, *args.map { |a| "#{a}=" })
end

#setup_class_initialize(klass) ⇒ Object


79
80
81
82
83
# File 'lib/eac_ruby_utils/common_constructor.rb', line 79

def setup_class_initialize(klass)
  klass.include(::ActiveSupport::Callbacks)
  klass.define_callbacks :initialize
  ::EacRubyUtils::CommonConstructor::ClassInitialize.new(self, klass).run
end

#super_argsObject


85
86
87
# File 'lib/eac_ruby_utils/common_constructor.rb', line 85

def super_args
  options[:super_args]
end