Class: EacRubyUtils::CommonConstructor

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

Defined Under Namespace

Classes: Initialize

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.


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

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


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

def after_set_block
  @after_set_block
end

#argsObject (readonly)

Returns the value of attribute args


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

def args
  @args
end

#optionsObject (readonly)

Returns the value of attribute options


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

def options
  @options
end

Class Method Details

.parse_args_options(args) ⇒ Object


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

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


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

def args_count
  (args_count_min..args_count_max)
end

#args_count_maxObject


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

def args_count_max
  args.count
end

#args_count_minObject


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

def args_count_min
  args.count - default_values.count
end

#default_valuesObject


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

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

#setup_class(klass) ⇒ Object


60
61
62
63
64
# File 'lib/eac_ruby_utils/common_constructor.rb', line 60

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

#setup_class_attr_readers(klass) ⇒ Object


66
67
68
69
# File 'lib/eac_ruby_utils/common_constructor.rb', line 66

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

#setup_class_attr_writers(klass) ⇒ Object


71
72
73
74
# File 'lib/eac_ruby_utils/common_constructor.rb', line 71

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

#setup_class_initialize(klass) ⇒ Object


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

def setup_class_initialize(klass)
  common_constructor = self
  klass.include(::ActiveSupport::Callbacks)
  klass.define_callbacks :initialize
  klass.send(:define_method, :initialize) do |*args|
    Initialize.new(common_constructor, args, self).run
  end
end