class Concord

A mixin to define a composition

Constants

MAX_NR_OF_OBJECTS

The maximum number of objects the hosting class is composed of

Attributes

names[R]

Return names

@return [Enumerable<Symbol>]

@api private

Public Class Methods

new(*names) click to toggle source

Initialize object

@return [undefined]

@api private

# File lib/concord.rb, line 27
def initialize(*names)
  if names.length > MAX_NR_OF_OBJECTS
    fail "Composition of more than #{MAX_NR_OF_OBJECTS} objects is not allowed"
  end

  @names, @module = names, Module.new
  define_initialize
  define_readers
  define_equalizer
end

Private Instance Methods

define_equalizer() click to toggle source

Define equalizer

@return [undefined]

@api private

# File lib/concord.rb, line 54
def define_equalizer
  @module.send(:include, Equalizer.new(*@names))
end
define_initialize() click to toggle source

Define initialize method

@return [undefined]

@api private

rubocop:disable MethodLength

# File lib/concord.rb, line 80
def define_initialize
  ivars, size = instance_variable_names, names.size
  @module.class_eval do
    define_method :initialize do |*args|
      args_size = args.size
      if args_size != size
        fail ArgumentError, "wrong number of arguments (#{args_size} for #{size})"
      end
      ivars.zip(args) { |ivar, arg| instance_variable_set(ivar, arg) }
    end
    private :initialize
  end
end
define_readers() click to toggle source

Define readers

@return [undefined]

@api private

# File lib/concord.rb, line 64
def define_readers
  attribute_names = names
  @module.class_eval do
    attr_reader(*attribute_names)
    protected(*attribute_names)
  end
end
included(descendant) click to toggle source

Hook run when module is included

@return [undefined]

@api private

# File lib/concord.rb, line 44
def included(descendant)
  descendant.send(:include, @module)
end
instance_variable_names() click to toggle source

Return instance variable names

@return [String]

@api private

# File lib/concord.rb, line 100
def instance_variable_names
  names.map { |name| "@#{name}" }
end