class Unparser::Concord

A mixin to define a composition

Original code before vendoring and reduction from: github.com/mbj/concord.

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

rubocop:disable Lint/MissingSuper

# File lib/unparser/concord.rb, line 30
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 = names
  define_initialize
  define_readers
  define_equalizer
end

Private Instance Methods

define_equalizer() click to toggle source

Define equalizer

@return [undefined]

@api private

# File lib/unparser/concord.rb, line 48
def define_equalizer
  include(Equalizer.new(*names))
end
define_initialize() click to toggle source

Define initialize method

@return [undefined]

@api private

# File lib/unparser/concord.rb, line 72
def define_initialize
  ivars = instance_variable_names
  size = names.size

  define_method :initialize do |*args|
    args_size = args.size
    unless args_size.equal?(size)
      fail ArgumentError, "wrong number of arguments (#{args_size} for #{size})"
    end

    ivars.zip(args) { |ivar, arg| instance_variable_set(ivar, arg) }
  end
end
define_readers() click to toggle source

Define readers

@return [undefined]

@api private

# File lib/unparser/concord.rb, line 58
def define_readers
  attribute_names = names
  attr_reader(*attribute_names)

  protected(*attribute_names) if attribute_names.any?
end
instance_variable_names() click to toggle source

Return instance variable names

@return [String]

@api private

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